我想在浏览器上播放 AC3 音频视频。为此,我决定使用 fluent-ffmpeg 实时转换视频并进行流式传输。它可以很好地用作直播/管道,但您甚至无法返回视频。
app.get('/video', function (req, res) {
var path = 'show.mkv';
ffmpeg(path)
.outputOptions(arr)
.on('end', function () {
console.log('file has been converted succesfully');
})
.on('error', function (err) {
console.log('an error happened: ' + err.message);
})
.pipe(res);
});
所以我需要为转换建立一种缓冲区,也就是让用户在视频中来回走动。我找到了一些完全符合我需要的代码,尽管它没有转换:
app.get('/video', function(req, res) {
const path = 'assets/sample.mp4'
const stat = fs.statSync(path)
const fileSize = stat.size
const range = req.headers.range
if (range) {
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize-1
const chunksize = (end-start)+1
const file = fs.createReadStream(path, {start, end})
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
}
res.writeHead(206, head)
file.pipe(res)
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
}
res.writeHead(200, head)
fs.createReadStream(path).pipe(res)
}
})
(来自https://github.com/daspinola/video-stream-sample)
我一直在尝试使 fluent-ffmpeg 与缓冲一起工作,但没有成功,而且我几乎不知道该怎么做。如果无法缓冲,是否有类似的替代方案可用于预转换视频?已经谢谢了。