Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

How to output fragmented mp4 with ffmpeg?

ffmpeg -i infile.avi out.mp4 outputs non-fragmented MP4.

How do I obtain fragmented mp4?

Update A fragmented mp4 file is internally divided into several back-to-back chunks or MPEG-4 movie fragments. Each chunk has its own moof atom - so there are several moof atoms interleaved in the file instead of a single moov at the end as in the case of an unfragmented mp4. This makes it easier to stream over slow networks where buffering is involved

There are several tools like mp4box that convert a normal mp4 to a fragmented one. Unfortunately we cannot use something like this

ffmpeg <options to output mp4> | mp4box

since ffmpeg does not produce seekable output while producing mp4 containers.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should do the trick:

ffmpeg -re -i infile.ext -g 52 
-c:a aac -b:a 64k -c:v libx264 -b:v 448k 
-f mp4 -movflags frag_keyframe+empty_moov 
output.mp4
  • frag_keyframe causes fragmented output,
  • empty_moov will cause output to be 100% fragmented; without this the first fragment will be muxed as a short movie (using moov) followed by the rest of the media in fragments,
  • -re is useful when live streaming (use input media frame rate), do not use it if you are creating a file,
  • -g 52 forces (at least) every 52nd frame to be a keyframe

To calculate a healthy keyframe interval please see the paragraphs about fragment sizes in the docs of my streaming server. - You can also consider using WebM which is a free alternative to H.264 (and has better support on some platforms than fragmented mp4).

Important note: FFMpeg's muxer will set the Duration in both tkhd and mdhd atoms to 0xffffffff for each track. This causes problems in some players (for example Quicktime will not play such files). You should find a tool and change it to zero (0x00000000).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...