How to use ffmpeg for streaming mp4 via websocket

curtiss picture curtiss · Aug 5, 2015 · Viewed 7.4k times · Source

I've written a sample in nodejs which streams some input to the client via websocket connection in mp4 format. On the client side, the mp4 packages are added to a MediaSourceBuffer.

This runs fine, but only if the client gets the stream from the beginning with the first package. So another client can't play the current Stream, because he won't get the Stream from the beginning.

I tried (try&error) to save the first package ffmpeg sends and send this at the beginning of a new connection, then the current stream. Then the MediaSourceBuffer breaks because of encoding error..

Here is the ffmpeg command :

-i someInput -g 59 
-vcodec libx264 -profile:v baseline 
-f mp4 -movflags empty_moov+omit_tfhd_offset+frag_keyframe+default_base_moof
-reset_timestamps 1
-

The part "empty_moov+omit_tfhd_offset+frag_keyframe+default_base_moof" should make the Streampackages independent in putting the moovatom at the beginning of each part and sizing the parts in 59 frames each by keyframe, so I don't get it why I can't view the Stream beginning after the start.

Answer

szatmary picture szatmary · Aug 5, 2015

The output of that command is not a 'stream' per se. It is series of concatenated fragments. Each fragments must be received in its entirety. If a partial fragment is received it will confuse the parser to the point where it can not identify the start of the next fragment. In addition, the first fragment output is called an initialization fragment. This initialization fragment must be sent to the client first. After that any fragment can be played. Hence it must be cached by the server.