新增编码方式选择
This commit is contained in:
32
server.js
32
server.js
@@ -64,17 +64,20 @@ app.get('/api/videos', async (req, res) => {
|
||||
|
||||
// Endpoint to transcode S3 video streaming to HLS
|
||||
app.post('/api/transcode', async (req, res) => {
|
||||
const { key } = req.body;
|
||||
const { key, codec } = req.body;
|
||||
|
||||
if (!key) {
|
||||
return res.status(400).json({ error: 'Video key is required' });
|
||||
}
|
||||
|
||||
const safeCodec = codec === 'h265' ? 'h265' : 'h264';
|
||||
const videoCodec = safeCodec === 'h265' ? 'libx265' : 'libx264';
|
||||
|
||||
try {
|
||||
const safeKeyName = key.replace(/[^a-zA-Z0-9_\-]/g, '');
|
||||
const outputDir = path.join(__dirname, 'public', 'hls', safeKeyName);
|
||||
const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_'));
|
||||
const outputDir = path.join(__dirname, 'public', 'hls', ...safeKeySegments);
|
||||
const m3u8Path = path.join(outputDir, 'index.m3u8');
|
||||
const hlsUrl = `/hls/${safeKeyName}/index.m3u8`;
|
||||
const hlsUrl = `/hls/${safeKeySegments.join('/')}/index.m3u8`;
|
||||
|
||||
// If it already exists, just return the URL
|
||||
if (fs.existsSync(m3u8Path)) {
|
||||
@@ -94,15 +97,18 @@ app.post('/api/transcode', async (req, res) => {
|
||||
const s3Stream = response.Body;
|
||||
|
||||
// Triggers fluent-ffmpeg to transcode to HLS
|
||||
console.log(`Starting transcoding for ${key}`);
|
||||
console.log(`Starting transcoding for ${key} with codec ${videoCodec}`);
|
||||
|
||||
ffmpeg(s3Stream)
|
||||
// Use hardware acceleration if available (optional for boilerplate)
|
||||
.videoCodec(videoCodec)
|
||||
.audioCodec('aac')
|
||||
.outputOptions([
|
||||
'-codec:v copy', // Use copy if it's already h264, else change to libx264
|
||||
'-codec:a aac',
|
||||
'-hls_time 10', // 10 second segments
|
||||
'-hls_list_size 0', // keep all segments in playlist
|
||||
'-preset fast',
|
||||
'-crf 23',
|
||||
'-hls_time 6',
|
||||
'-hls_list_size 0',
|
||||
'-hls_allow_cache 1',
|
||||
'-hls_flags independent_segments',
|
||||
'-f hls'
|
||||
])
|
||||
.output(m3u8Path)
|
||||
@@ -128,12 +134,12 @@ app.get('/api/status', (req, res) => {
|
||||
const { key } = req.query;
|
||||
if (!key) return res.status(400).json({ error: 'Key is required' });
|
||||
|
||||
const safeKeyName = key.replace(/[^a-zA-Z0-9_\-]/g, '');
|
||||
const m3u8Path = path.join(__dirname, 'public', 'hls', safeKeyName, 'index.m3u8');
|
||||
const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_'));
|
||||
const m3u8Path = path.join(__dirname, 'public', 'hls', ...safeKeySegments, 'index.m3u8');
|
||||
|
||||
// Check if the playlist file exists
|
||||
if (fs.existsSync(m3u8Path)) {
|
||||
res.json({ ready: true, hlsUrl: `/hls/${safeKeyName}/index.m3u8` });
|
||||
res.json({ ready: true, hlsUrl: `/hls/${safeKeySegments.join('/')}/index.m3u8` });
|
||||
} else {
|
||||
res.json({ ready: false });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user