2011-12-23 103 views
33

ffmpeg -i infile.avi out.mp4輸出非分段的MP4。如何用ffmpeg輸出碎片化的mp4?

如何獲取零碎的mp4?

更新 分段mp4文件在內部被分成若干背對背的塊或MPEG-4電影片段。每個塊都有自己的moof原子 - 所以在文件中有幾個moof原子交織在一起,而不是單個moov,就像未分割的mp4一樣。這使得在涉及緩衝的慢速網絡上更容易流式傳輸

有幾種工具,如mp4box可將普通mp4轉換爲分段的mp4。不幸的是,我們不能使用這樣的東西

ffmpeg <options to output mp4> | mp4box 

因爲ffmpeg在生產mp4容器時不產生可查找的輸出。

+2

您可能想要添加一些關於」片段化mp4「的信息,以便讓那些可能熟悉ffmpeg但不支持片段化mp4s的人更易於訪問此問題。 – blahdiblah 2012-01-10 00:55:23

+0

好點!我已編輯我的問題 – 2012-01-11 06:04:01

+0

@SaptarshiBiswas您是否在不使用mp3box的情況下解決此問題?你能分享解決方案嗎?謝謝 – abrahab 2012-06-15 17:46:39

回答

40

這應該做的伎倆:

ffmpeg -re -i infile.ext -g 52 \ 
-strict experimental -acodec aac -ab 64k -vcodec libx264 -vb 448k \ 
-f mp4 -movflags frag_keyframe+empty_moov \ 
output.mp4 
  • frag_keyframe原因分段輸出,
  • empty_moov將導致輸出爲100%片段化;如果沒有這個,第一個片段將作爲短片被複用(使用moov),然後是其餘片段,
  • -re當實況流(媒體比特率輸出)時很有用,如果您要創建一個文件,
  • -g 52力(至少)每52幀是一個關鍵幀

要計算一個健康的關鍵幀間隔,請看到我的流媒體服務器的文檔有關fragment sizes的段落。 - 您也可以考慮使用WebM,它是H.264的免費替代品(並且在一些平臺上支持比分段的MP4更好)。

重要提示: FFmpeg的流合併器將設置時間兩個tkhdmdhd原子爲0xffffffff爲每個軌道。這會導致一些玩家出現問題(例如Quicktime不會播放這些文件)。您應該找到一個工具並將其更改爲零(0x00000000)。

+3

貌似-movflags + faststart選項最近已添加到FFmpeg中... – rogerdpack 2012-10-17 16:06:46

+1

如果您遇到以上命令的問題,則可能需要重新安裝ffmpeg。通過brew安裝所有選項: ** brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc - -with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-工具** – 2014-04-23 22:07:02

+1

是否可以將輸出流式傳輸到HTML5而不是將其保存到文件中?用http協議? – astralmaster 2015-08-11 09:16:06

2

ffmpeg -h(但不包括手冊頁)有以下幾點:

mp4 muxer AVOptions: 
-movflags   <flags> E.... MOV muxer flags 
    rtphint     E.... Add RTP hint tracks 
-moov_size   <int> E.... maximum moov size so it can be placed at the 
           beginning 
-frag_size   <int> E.... maximum fragment size 
-frag_duration  <int> E.... maximum fragment duration 
-rtpflags   <flags> E.... RTP muxer flags 
    latm     E.... Use MP4A-LATM packetization instead of 
           MPEG4-GENERIC for AAC 
-skip_iods   <int> E.... Skip writing iods atom. 
-iods_audio_profile <int> E.... iods audio profile atom. 
-iods_video_profile <int> E.... iods video profile atom. 

我不知道如何識別一個支離破碎的MP4,如果我看見一個人,但它看起來像的ffmpeg確實有一些(不良記錄)支持他們。

+2

回覆:「我不知道如何識別一個零碎的mp4,如果我看到一個」:)他們看起來很像。 :)在'moov'前面有一個'mvex'原子讓你知道它會被分割。各種軌道子原子中的實際數據信息將非常少。將會有頂級的'moof''mdat'對,這是實際數據所在的地方。最後可能會有一個'mfra'原子。 – 2014-06-17 22:59:44

+0

'mp4box -info file'將指示MP4是否被分割。 – Mulvya 2017-12-29 06:49:05

4

更新:考慮一個分段的MP4作爲ISMV(平滑流)文件。自2011年1月27日起,新版本FFMPEG 0.10可以複製到此格式。

ismv muxer AVOptions: 
-movflags   <flags> E.... MOV muxer flags 
    rtphint     E.... Add RTP hint tracks 
    empty_moov    E.... Make the initial moov atom empty (not supported by QuickTime) 
    frag_keyframe   E.... Fragment at video keyframes 
    separate_moof   E.... Write separate moof/mdat atoms for each track 
    frag_custom    E.... Flush fragments on caller requests 
    isml     E.... Create a live smooth streaming feed (for pushing to a publishing point) 
-moov_size   <int> E.... maximum moov size so it can be placed at the begin 
-rtpflags   <flags> E.... RTP muxer flags 
    latm     E.... Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC 
    rfc2190     E.... Use RFC 2190 packetization instead of RFC 4629 for H.263 
    skip_rtcp    E.... Don't send RTCP sender reports 
-skip_iods   <int> E.... Skip writing iods atom. 
-iods_audio_profile <int> E.... iods audio profile atom. 
-iods_video_profile <int> E.... iods video profile atom. 
-frag_duration  <int> E.... Maximum fragment duration 
-min_frag_duration <int> E.... Minimum fragment duration 
-frag_size   <int> E.... Maximum fragment size 
-ism_lookahead  <int> E.... Number of lookahead entries for ISM files 
0

也許這會有所幫助。在下面的示例中,ffmpeg獲取RTMP饋送的COPY,然後使用ffmpeg,它會在fMP4中創建一個可被IIS或Azure入口點接受的HTTP輸出。

注意:原始編碼器設置爲2秒的關鍵幀間隔。

「的ffmpeg -i RTMP:// IP服務器:1935 /名/ streamkey -vcodec副本複印件-acodec -Nal-HRD CBR -movflags ISML + frag_keyframe + separate_moof -f ismv切入點http://url /入口點.isml/Streams(feed1)「