转换后无法重定向视频流(mkv 到 mp4)



我目前正在实现一个学校项目,该项目的目标是使用torrent-stream(带有磁力链接(的流媒体视频网站(如Netflix(。我正在使用 NodeJS 作为流部分。

我的问题是:当我尝试同时流式传输和转换(使用 ffmpeg(视频时,我无法将流重定向到 HTML 5 播放器。我认为这是因为我只是不知道转换后的文件的最终大小是多少。 在浏览器的控制台中,我有以下消息:net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

我试图把它放在标题中:Transfer-Encoding: chunked而不是内容长度 我指定流(转换前(运行良好

这是我的代码:

getTorrentFile.then(function (file) {
res.setHeader('Content-Type', 'video/mp4');
res.setHeader('Content-Length', file.length);
const ranges = parseRange(file.length, '15' /* variable à comprendre */, { combine: true });
console.log(ranges);
if (ranges === -1) {
// 416 Requested Range Not Satisfiable
console.log('416')
res.statusCode = 416;
return res.end();
} else if (ranges === -2 || ranges.type !== 'bytes' || ranges.length > 1) {
// 200 OK requested range malformed or multiple ranges requested, stream ent'ire video
if (req.method !== 'GET') return res.end();
console.log('200')
stream = file.createReadStream()
ffmpeg(stream)
.videoCodec('libx264')
.audioCodec('aac')
.output(res)
.output('./video/' + film + '_s' + season + '_e' + episode + '.mp4')
.outputFormat('mp4')
.outputOptions('-movflags frag_keyframe+empty_moov')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('progress', function(progress) {
console.log('Processing: ' + progress.targetSize + 'kb done');
})
.on('end', function() {
console.log('Processing finished !');
})
.addOutputOption('-acodec')
.run()

对不起,如果我不太清楚,如果您需要更多信息,请问我一些问题:)

感谢您的帮助,再见:)

Mp4 不能像那样流式传输。您必须等到文件完成才能流式传输。如果要动态转换代码,请使用可以支持该格式的格式,例如 hls。

最新更新