我确信这个问题已经被问了一百万次,但我对以下脚本有疑问。它在 Safari 浏览器中运行良好,但在 Firefox 或 Chrome 中则不行。
app.get('/stream', async (req, res) => {
const bucket = storage.bucket("REMOVED");
const file = bucket.file("syndication/2023/09/1696116750736video_Kelly_finished.mp4");
const stat = await file.getMetadata();
const fileSize = stat[0].size;
res.set({
"Content-Length": fileSize.toString(),
"Accept-Ranges": "bytes",
Connection: "Keep-Alive",
"Keep-Alive": "timeout=2, max=100",
"Content-Type": "video/mp4",
"X-Accel-Buffering": "no"
});
// Pipe the video stream to the response
const stream = file.createReadStream();
stream.pipe(res)
});
我想知道是否有人知道如何传输视频文件。
当它是 mp3 时,效果很好。
当我这样编码时
app.get('/stream', async (req, res) => {
const fileStream = storage.bucket("REMOVED").file("syndication/2023/09/1696116750736video_Kelly_finished.mp4");
const [metadata] = await fileStream.getMetadata();
const fileSize = metadata.size;
// Set appropriate headers to allow the audio to play in the browser and track streaming duration
res.set('Content-Type', 'video/mp4');
res.set('Content-Length', fileSize.toString()); // Convert fileSize to a string
res.set('Accept-Ranges', 'bytes');
//res.set('Cache-Control', 'public, max-age=3600'); // Cache the file for 1 hour (adjust as needed)
fileStream.createReadStream().pipe(res);
});
它适用于 Microsoft Edge(Chrome),但不适用于 Firefox
问题是所使用的编解码器不应使用任何 h.264 或 h.265
我不确定为什么有些 mp4 可以工作,但我知道当我用
编码视频时ffmpeg -i $INPUT_VIDEO -c:v libx264 -profile:v main -preset medium -b:v 1M -c:a aac -pix_fmt yuv420p $OUTPUT_VIDEO
成功了。