diff --git a/public/index.html b/public/index.html index db4ec5b..8c258b7 100644 --- a/public/index.html +++ b/public/index.html @@ -62,7 +62,7 @@
Generating HLS segments, please wait.
+Generating MP4 file, please wait.
Timeout waiting for HLS segments.
`; + transcodingOverlay.innerHTML = `Timeout waiting for MP4 file.
`; } } catch (err) { console.error("Poll Error:", err); @@ -292,7 +292,7 @@ document.addEventListener('DOMContentLoaded', () => { }; // Initialize MP4 Player - const playHlsStream = (url) => { + const playMp4Stream = (url) => { transcodingOverlay.classList.add('hidden'); videoPlayer.classList.remove('hidden'); playBtn.classList.add('hidden'); diff --git a/server.js b/server.js index 95baf27..139e1a8 100644 --- a/server.js +++ b/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 });