简体   繁体   中英

How to stream mp4 file with fluent-ffmpeg?

I am trying to stream a video file with 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.

Is there any way that i can show text while playing video with ffmpeg or without it?

you need to enable fragmentation with the 'movflags' option.

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

Having an empty moov atom means it doesn't need to seek and thus works with a pipe.

You can use this to show the ffmpeg transcripting progress :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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