This commit is contained in:
CN-JS-HuiBai
2026-04-02 22:17:24 +08:00
parent 504d02c0fa
commit dac755127a
2 changed files with 73 additions and 16 deletions

View File

@@ -120,6 +120,29 @@ const probeFile = (filePath) => {
});
};
const stopActiveTranscode = (progressKey) => {
const activeCommand = transcodeProcesses.get(progressKey);
if (!activeCommand) {
return;
}
try {
if (typeof activeCommand.removeAllListeners === 'function') {
activeCommand.removeAllListeners('progress');
activeCommand.removeAllListeners('stderr');
activeCommand.removeAllListeners('end');
activeCommand.removeAllListeners('error');
}
if (typeof activeCommand.kill === 'function') {
activeCommand.kill('SIGKILL');
}
} catch (killError) {
console.warn(`Failed to kill transcode process for ${progressKey}:`, killError);
} finally {
transcodeProcesses.delete(progressKey);
}
};
const extractS3Credentials = (req) => {
const query = req.query || {};
const username = req.headers['x-s3-username'] || req.body?.username || query.username || query.accessKeyId || '';
@@ -145,6 +168,9 @@ wss.on('connection', (ws) => {
const currentProgress = progressMap[progressKey];
if (currentProgress) {
if (typeof currentProgress.duration === 'number' && currentProgress.duration > 0) {
ws.send(JSON.stringify({ type: 'duration', key: message.key, duration: currentProgress.duration }));
}
ws.send(JSON.stringify({ type: 'progress', key: message.key, progress: currentProgress }));
if (currentProgress.status === 'finished' && currentProgress.mp4Url) {
ws.send(JSON.stringify({ type: 'ready', key: message.key, mp4Url: currentProgress.mp4Url }));
@@ -260,20 +286,10 @@ app.post('/api/stop-transcode', (req, res) => {
}
const progressKey = getProgressKey(key);
const command = transcodeProcesses.get(progressKey);
if (!command) {
if (!transcodeProcesses.has(progressKey)) {
return res.status(404).json({ error: 'No active transcode found for this key' });
}
try {
if (typeof command.kill === 'function') {
command.kill('SIGKILL');
}
} catch (killError) {
console.warn('Failed to kill transcode process:', killError);
}
transcodeProcesses.delete(progressKey);
stopActiveTranscode(progressKey);
progressMap[progressKey] = {
status: 'cancelled',
percent: 0,
@@ -323,6 +339,8 @@ app.get('/api/stream', async (req, res) => {
const s3Client = createS3Client(auth);
try {
stopActiveTranscode(progressKey);
let totalBytes = 0;
let downloadedBytes = 0;
@@ -392,6 +410,10 @@ app.get('/api/stream', async (req, res) => {
try {
const metadata = await probeFile(tmpInputPath);
const duration = metadata.format?.duration || 0;
progressMap[progressKey] = {
...(progressMap[progressKey] || {}),
duration: parseFloat(duration) || 0
};
broadcastWs(progressKey, { type: 'duration', key, duration: parseFloat(duration) });
} catch (probeErr) {
console.error('Probe failed:', probeErr);
@@ -425,14 +447,23 @@ app.get('/api/stream', async (req, res) => {
ffmpegCommand
.on('progress', (progress) => {
const effectiveDuration = Math.max(0, (progressMap[progressKey]?.duration || 0) - startSeconds);
const timemarkSeconds = ffmpeg.timemarkToSeconds(progress.timemark || '0');
const absoluteSeconds = startSeconds + (isFinite(timemarkSeconds) ? timemarkSeconds : 0);
const percent = effectiveDuration > 0
? Math.min(Math.max(Math.round((absoluteSeconds / (progressMap[progressKey]?.duration || effectiveDuration)) * 100), 0), 100)
: Math.min(Math.max(Math.round(progress.percent || 0), 0), 100);
const progressState = {
status: 'transcoding',
percent: Math.min(Math.max(Math.round(progress.percent || 0), 0), 100),
percent,
frame: progress.frames || null,
fps: progress.currentFps || null,
bitrate: progress.currentKbps || null,
timemark: progress.timemark || null,
details: `Streaming transcode ${Math.min(Math.max(Math.round(progress.percent || 0), 0), 100)}%`,
absoluteSeconds,
duration: progressMap[progressKey]?.duration || null,
startSeconds,
details: `Streaming transcode ${percent}%`,
mp4Url: null
};
progressMap[progressKey] = progressState;
@@ -446,6 +477,8 @@ app.get('/api/stream', async (req, res) => {
progressMap[progressKey] = {
status: 'finished',
percent: 100,
duration: progressMap[progressKey]?.duration || null,
startSeconds,
details: 'Streaming transcode complete',
mp4Url: null
};
@@ -456,6 +489,8 @@ app.get('/api/stream', async (req, res) => {
const failedState = {
status: 'failed',
percent: progressMap[progressKey]?.percent || 0,
duration: progressMap[progressKey]?.duration || null,
startSeconds,
details: err.message || 'Streaming transcode failed',
mp4Url: null
};
@@ -477,6 +512,9 @@ app.get('/api/stream', async (req, res) => {
ffmpegCommand.kill('SIGKILL');
} catch (_) {}
}
if (transcodeProcesses.get(progressKey) === ffmpegCommand) {
transcodeProcesses.delete(progressKey);
}
});
startStream(videoCodec);