Streaming converted movie with mp4 container in NodeJS, movie playing very fast

Mustafa picture Mustafa · Jan 14, 2014 · Viewed 7.3k times · Source

I have used stream-transcoder module to convert a file make it a stream. So the file is not stored, it is on the fly.

app.get("/video", function(req,res){
    res.writeHead(200, {'Content-Type': 'video/mp4'});
    var src = "movie.avi";

    var Transcoder = require('stream-transcoder');
    var stream = fs.createReadStream(src);
    new Transcoder(stream)
        .maxSize(1280, 720)
        .videoCodec('h264')
        .videoBitrate(800 * 1000)
        .fps(25)
        .sampleRate(44100)
        .channels(2)
        .audioBitrate(128 * 1000)
        .format('mp4')
        .on('finish', function() {
            console.log("finished");
        })
        .stream().pipe(res);
});

It works nicely, it is fast, but too fast, the audio is played at the same speed, however the video does not respect the frame rate, whatever is recieved from ffmpeg is immeidately shown, fastly. Additionally, it does not show the total time, I believe it is the problem. I need to somehow specify the length, framerate, but I could not find enough information on that. I thought the stream recieved from ffmpeg should contain that. And I could not find respective headers for that in HTTP.

Here are the flags that this stream-transcoder module uses for MP4:

[ '-i',
  '-',
  '-vf',
  'scale=min(trunc(1280/hsub)*hsub\\,trunc(a*720/hsub)*hsub):min(trunc(720/vsub)*vsub\\,trunc(1280/a/vsub)*vsub)',
  '-vcodec',
  'h264',
  '-b:v',
  800000,
  '-r',
  25,
  '-ar',
  44100,
  '-ac',
  2,
  '-ab',
  128000,
  '-f',
  'mp4',
  '-movflags',
  'frag_keyframe+faststart',
  'pipe:1' ]

When I use VP8 encoder and WebM, it works nicely, the time is displayed, video plays normal speed.

Answer

SurfinColin picture SurfinColin · Jan 20, 2014

I believe you need to add "-re" flag to the ffmpeg command when streaming from a file on the fly like you are describing.

Check out there guide here: FFmpeg Streaming Guide

Here is the direct ffmpeg command for streaming a file to a location.

ffmpeg -re -i 'myfile.mp4' -vcodec libx264 -b:v 950k -r 24 -s 1024x576 -ar 44100 -ac 2 -ab 64k -f flv 'rtmp://mywowzaserver.com/live/mp4:streamName'