user3184735
user3184735

Reputation: 31

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?

Upvotes: 4

Views: 7740

Answers (1)

Alexis Vera
Alexis Vera

Reputation: 357

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.

Upvotes: 1

Related Questions