简体   繁体   English

如何使用 fluent-ffmpeg 流式传输 mp4 文件?

[英]How to stream mp4 file with fluent-ffmpeg?

I am trying to stream a video file with fluent-ffmpeg.我正在尝试使用 fluent-ffmpeg 流式传输视频文件。 But i could't do it.但我做不到。 Here is my code这是我的代码

var filePath = null;
filePath     = "video.mp4";

var stat  = fs.statSync(filePath);

var range        = req.headers.range;
var parts        = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend   = parts[1];

var start     = parseInt(partialstart, 10);
var end       = partialend ? parseInt(partialend, 10) : total-1;
var chunksize = (end-start)+1;

var file = fs.createReadStream(filePath, {start: start, end: end});

res.writeHead(206, {
 'Content-Range  ': 'bytes ' + start + '-' + end + '/' + total,
 'Accept-Ranges'  : 'bytes',
 'Content-Length' : chunksize,
 'Content-Type'   : 'video/mp4'
});

ffmpeg(file)
.videoCodec('libx264')
.withAudioCodec('aac')
.format('mp4')
.videoFilters({
 filter: 'drawtext',
 options: {
  fontsize:20,
  fontfile: 'public/fonts/Roboto-Black.ttf',
  text: "USERNAME",
  x:10,
  y:10,
  fontcolor:"red"
 }})
 .outputOptions(['-frag_duration 100','-movflags frag_keyframe+faststart','-pix_fmt yuv420p'])
 .output(res,{ end:true })
 .on('error', function(err, stdout, stderr) {
  console.log('an error happened: ' + err.message + stdout + stderr);
 })
 .run();

When i run this code block, video not playing and throws an error:当我运行此代码块时,视频无法播放并引发错误:

an error happened: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input

when i do not use stream as input, video is playing in Chrome but after a little time, video player throws error.当我不使用流作为输入时,视频正在 Chrome 中播放,但过了一会儿,视频播放器抛出错误。

Is there any way that i can show text while playing video with ffmpeg or without it?有什么方法可以在使用 ffmpeg 或不使用 ffmpeg 播放视频时显示文本?

you need to enable fragmentation with the 'movflags' option.您需要使用 'movflags' 选项启用碎片。

.outputOptions(['-movflags frag_keyframe + empty_moov'])

Having an empty moov atom means it doesn't need to seek and thus works with a pipe.有一个空的 moov 原子意味着它不需要寻找,因此可以使用管道。

You can use this to show the ffmpeg transcripting progress :您可以使用它来显示 ffmpeg 转录进度:

.on('progress', function(progress) {
    console.log(progress);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM