修复ffmpeg转码的错误提示,改为等待MP4文件生成完成后再播放。
This commit is contained in:
23
server.js
23
server.js
@@ -200,12 +200,27 @@ app.post('/api/transcode', async (req, res) => {
|
||||
progressMap[progressKey] = progressState;
|
||||
broadcastWs(progressKey, { type: 'progress', key: progressKey, progress: progressState });
|
||||
})
|
||||
.on('stderr', (stderrLine) => {
|
||||
console.log(`ffmpeg stderr: ${stderrLine}`);
|
||||
})
|
||||
.on('end', () => {
|
||||
console.log(`Finished transcoding ${key} to MP4`);
|
||||
const progressState = { status: 'finished', percent: 100, details: 'Transcoding complete', mp4Url };
|
||||
let progressState;
|
||||
try {
|
||||
const stats = fs.statSync(mp4Path);
|
||||
if (!stats.isFile() || stats.size === 0) {
|
||||
throw new Error('Output MP4 is empty or missing');
|
||||
}
|
||||
progressState = { status: 'finished', percent: 100, details: 'Transcoding complete', mp4Url };
|
||||
} catch (verifyError) {
|
||||
console.error(`Output verification failed for ${mp4Path}:`, verifyError);
|
||||
progressState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: `Output verification failed: ${verifyError.message}`, mp4Url };
|
||||
}
|
||||
progressMap[progressKey] = progressState;
|
||||
broadcastWs(progressKey, { type: 'progress', key: progressKey, progress: progressState });
|
||||
broadcastWs(progressKey, { type: 'ready', key: progressKey, mp4Url });
|
||||
if (progressState.status === 'finished') {
|
||||
broadcastWs(progressKey, { type: 'ready', key: progressKey, mp4Url });
|
||||
}
|
||||
})
|
||||
.on('error', (err) => {
|
||||
console.error(`Error transcoding ${key}:`, err);
|
||||
@@ -234,8 +249,8 @@ app.get('/api/status', (req, res) => {
|
||||
const mp4Path = path.join(__dirname, 'public', 'mp4', ...safeKeySegments, 'video.mp4');
|
||||
const progress = progressMap[progressKey] || null;
|
||||
|
||||
// Check if the MP4 file exists
|
||||
if (fs.existsSync(mp4Path)) {
|
||||
const outputReady = fs.existsSync(mp4Path) && (!progress || progress.status !== 'failed');
|
||||
if (outputReady) {
|
||||
res.json({ ready: true, mp4Url: `/mp4/${safeKeySegments.join('/')}/video.mp4`, progress });
|
||||
} else {
|
||||
res.json({ ready: false, progress });
|
||||
|
||||
Reference in New Issue
Block a user