It’s been awhile I’ve posted an article on this blog, and, it’s with a great pleasure I share with you what I’ve just learnt… How to stream an mp4 video while encoding it, directly with ffmpeg just like Youtube or Vimeo?
First, we’ll have to understand why an mp4 video has to be completely loaded by the browser in order to play it.
1. MOOV ATOM, What’s that?
MOOV ATOM, is literaly an autonomous unit of metadata related to the video file, that contains informations about:
– The video duration
– The video display characteristics
– Some additional video track informations
With mp4 files, the MOOV ATOM is placed by default at the end of the file, so, until your video is completely loaded, it is impossible to know its duration and some other essential informations, this is why mp4 videos are not natively streamable on the fly while encoding.
But, this is not the case for “flv, ogg, webm, mpeg-ts” video files, that are called SOTF (Stream-Oriented Transport Format).
2. How to encode mp4 and move MOOV ATOM details to the beginning?
This is done in just one command with the “-movflags faststart” attributes, juste like this.
ffmpeg -i input.avi -c:v libx264 -crf 22 -c:a libfaac -movflags faststart output.mp4
3. Now, how to stream our video while encoding?
Ok, now we have a streamable mp4 file, what we need is to live streaming it while encoding.
You have many ways to solve this, like using JWPlayer and FFMPEG.
I will not explain again what has been perfectly explained on another website, so I recommand you to see this great tutorial about video live streaming with FFServer and FFmpeg.
Or this great tutorial too.
Hope it helped you.