[FFmpeg-user] Creating MP4 fragments in separate files

Tom Distler tom.distler at gmail.com
Thu May 9 18:46:24 CEST 2013

[I originally sent this as a unsubscribed user, but it's been awaiting
moderator approval for 24hrs... sorry for any unintended duplicates]


I'm attempting to use ffmpeg to connect to an RTSP h.264 source and store
the video stream as a series of MP4 fragment files. The purpose is to serve
up the fragments to a web browser (Chrome) via the MediaSource API. I want
ffmpeg to continually write a new mp4 fragment file for each 4 seconds of
video with an incrementing file number... i.e. segment01.m4s,
segment02.m4s, etc.

I need the first segment file to contain the "moov' box with no 'mdat'
media data. Each subsequent fragment file should have a 'moof' box and an
'mdat' box containing the video for that video fragment.

I've tried 2 different approaches (to no avail):

(1)  ffmpeg -i rtsp://<ip>/stream1 -codec copy -map 0 -f segment
-segment_time 4 -segment_format mp4 segment%d.m4s

This creates separate mp4 files for every 4 seconds of video, but each file
is stand-alone... i.e. each one has it's own 'moov' box... they aren't
fragments of the same stream, but rather independent streams.

(2) ffmpeg -re -i rtsp://<ip>/stream1 -codec copy -f mp4 -movflags
frag_keyframe+empty_moov -frag_duration 4 camera.mp4

This creates a single mp4 file, with a single 'moov' box and multiple
'moof' boxes for each 4 second interval... the problem is they're all in
the same file.

Any help would be greatly appreciated... even if there's a separate tool I
need to run the data through (I've tried using MP4Box w/ ffmpeg-generated
MPEG2-TS segments, but I get PTS errors).


More information about the ffmpeg-user mailing list