新增转码进度显示功能,优化视频标题显示,添加编码器选择面板。

This commit is contained in:
CN-JS-HuiBai
2026-04-02 17:41:50 +08:00
parent eb6068cbb8
commit edf39d0ae4
4 changed files with 84 additions and 4 deletions

View File

@@ -28,6 +28,7 @@ const s3Client = new S3Client({
});
const BUCKET_NAME = process.env.S3_BUCKET_NAME;
const progressMap = {};
// Endpoint to list videos in the bucket
app.get('/api/videos', async (req, res) => {
@@ -82,9 +83,12 @@ app.post('/api/transcode', async (req, res) => {
try {
const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_'));
const progressKey = safeKeySegments.join('/');
const outputDir = path.join(__dirname, 'public', 'hls', ...safeKeySegments);
const m3u8Path = path.join(outputDir, 'index.m3u8');
const hlsUrl = `/hls/${safeKeySegments.join('/')}/index.m3u8`;
const hlsUrl = `/hls/${progressKey}/index.m3u8`;
progressMap[progressKey] = { status: 'pending', percent: 0, details: 'Waiting for ffmpeg to start' };
// If it already exists, just return the URL
if (fs.existsSync(m3u8Path)) {
@@ -119,11 +123,24 @@ app.post('/api/transcode', async (req, res) => {
'-f hls'
])
.output(m3u8Path)
.on('progress', (progress) => {
progressMap[progressKey] = {
status: 'transcoding',
percent: Math.min(Math.max(Math.round(progress.percent || 0), 0), 100),
frame: progress.frames || null,
fps: progress.currentFps || null,
bitrate: progress.currentKbps || null,
timemark: progress.timemark || null,
details: `Transcoding... ${Math.min(Math.max(Math.round(progress.percent || 0), 0), 100)}%`
};
})
.on('end', () => {
console.log(`Finished transcoding ${key} to HLS`);
progressMap[progressKey] = { status: 'finished', percent: 100, details: 'Transcoding complete' };
})
.on('error', (err) => {
console.error(`Error transcoding ${key}:`, err);
progressMap[progressKey] = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: err.message || 'Transcoding failed' };
})
.run();
@@ -142,13 +159,15 @@ app.get('/api/status', (req, res) => {
if (!key) return res.status(400).json({ error: 'Key is required' });
const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_'));
const progressKey = safeKeySegments.join('/');
const m3u8Path = path.join(__dirname, 'public', 'hls', ...safeKeySegments, 'index.m3u8');
const progress = progressMap[progressKey] || null;
// Check if the playlist file exists
if (fs.existsSync(m3u8Path)) {
res.json({ ready: true, hlsUrl: `/hls/${safeKeySegments.join('/')}/index.m3u8` });
res.json({ ready: true, hlsUrl: `/hls/${safeKeySegments.join('/')}/index.m3u8`, progress });
} else {
res.json({ ready: false });
res.json({ ready: false, progress });
}
});