修复ffmpeg转码的错误提示,改为等待MP4文件生成完成后再播放。

This commit is contained in:
CN-JS-HuiBai
2026-04-02 18:04:41 +08:00
parent 7dc1b3f486
commit 46190ace2a
3 changed files with 26 additions and 11 deletions

View File

@@ -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 });