允许拉取进度条

This commit is contained in:
CN-JS-HuiBai
2026-04-02 22:05:30 +08:00
parent fd199fdde7
commit 504d02c0fa
3 changed files with 202 additions and 15 deletions

View File

@@ -111,6 +111,15 @@ const shouldRetryWithSoftware = (message) => {
return /Cannot load libcuda\.so\.1|Could not open encoder before EOF|Error while opening encoder|Operation not permitted|Invalid argument/i.test(message);
};
const probeFile = (filePath) => {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(filePath, (err, metadata) => {
if (err) reject(err);
else resolve(metadata);
});
});
};
const extractS3Credentials = (req) => {
const query = req.query || {};
const username = req.headers['x-s3-username'] || req.body?.username || query.username || query.accessKeyId || '';
@@ -284,6 +293,7 @@ app.get('/api/stream', async (req, res) => {
const key = req.query.key;
const codec = req.query.codec;
const encoder = req.query.encoder;
const startSeconds = parseFloat(req.query.ss) || 0;
if (!bucket) {
return res.status(400).json({ error: 'Bucket name is required' });
@@ -378,6 +388,15 @@ app.get('/api/stream', async (req, res) => {
broadcastWs(progressKey, { type: 'progress', key, progress: progressMap[progressKey] });
}
// Probe file for duration and broadcast to clients
try {
const metadata = await probeFile(tmpInputPath);
const duration = metadata.format?.duration || 0;
broadcastWs(progressKey, { type: 'duration', key, duration: parseFloat(duration) });
} catch (probeErr) {
console.error('Probe failed:', probeErr);
}
res.setHeader('Content-Type', 'video/mp4');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
@@ -392,6 +411,10 @@ app.get('/api/stream', async (req, res) => {
.outputOptions(streamingOptions)
.format('mp4');
if (startSeconds > 0) {
ffmpegCommand.inputOptions(['-ss', startSeconds.toString()]);
}
if (/_vaapi$/.test(encoderName)) {
ffmpegCommand
.inputOptions(['-vaapi_device', '/dev/dri/renderD128'])