0

I am trying to convert an MP3 file to a fragmented MP4 like this:

ffmpeg -i input.mp3 -strict experimental -acodec aac -b:a 256k -f mp4 \
       -movflags faststart+frag_keyframe+empty_moov+separate_moof output.mp4 

However, using Bento4 I can see that there is just one giant mdat object instead of a series of those:

[ftyp] size=8+24
  major_brand = isom
  minor_version = 200
  compatible_brand = isom
  compatible_brand = iso2
  compatible_brand = iso6
  compatible_brand = mp41
[moov] size=8+701
  ...
[moof] size=8+62364
  ...
[mdat] size=8+5794679
[mfra] size=8+59
  [tfra] size=12+31, version=1
    track_ID = 1
    length_size_of_traf_num = 0
    length_size_of_trun_num = 0
    length_size_of_sample_num = 0
  [mfro] size=12+4
    mfra_size = 67

I think what I want is this:

enter image description here

(source)

But I can't seem to be able to get this from ffmpeg.

I found some other options here like

$ ffmpeg -h muxer=ismv
...
-frag_duration     <int>   E.... Maximum fragment duration
-min_frag_duration <int>   E.... Minimum fragment duration
-frag_size         <int>   E.... Maximum fragment size

but playing around with these didn't change the output.

How can I create fragments of a specific sice e.g. ~5 seconds each?

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

2

Audio streams have no keyframes which is what MP4 muxer relies on by default to demarcate fragment boundaries. You will have to set a fragment duration.

ffmpeg -i input.mp3 -c:a aac -b:a 256k -f mp4 -movflags +empty_moov+separate_moof -frag_duration 10M output.mp4

Note:

  1. faststart is applicable for regular, non-fragmented output
  2. frag_keyframe tells ffmpeg to start fragments at video keyframes but you don't have a video stream.
  3. -strict experimental hasn't been required for the native AAC encoder since 2016.
  4. frag_duration expects a value in microseconds. 10M = 10 million = 10 seconds.
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • Unfortunately the result of this is not playable under Chrome. I managed to get something playable using `faststart+frag_every_frame+empty_moov+separate_moof` but here I am not able to control the `frag_duration`. It seems that the argument gets ignored in this case. – Stefan Falk Oct 25 '20 at 06:37
  • Works here in Chrome 86 and FF 82. Share full log. – Gyan Oct 25 '20 at 10:37
  • Are you using MSE SourceBuffer in segment mode? – Stefan Falk Oct 25 '20 at 12:36
  • I'm just loading a local file in these browsers. – Gyan Oct 25 '20 at 16:29
  • Yeah, that's the thing. If you do that even an MP3 will work on Firefox. But it doesn't if you use this sh*t they call Media Source Extention (MSE) .. I need it in order to partially load audio files instead of downloading the entire file befor playing. And every browser does it's own cr*p to eff things up. Sorry for the rant it's just no fun anymore .. – Stefan Falk Oct 25 '20 at 16:43
  • You should raise an issue at Chrome on whether this is an inherent limitation of MSE in Chrome. – Gyan Oct 25 '20 at 16:58
  • It's not a bug though. It's just how the browsers implement it. And I don't even think it's a coincidence that they do it differently than other browsers.. – Stefan Falk Oct 25 '20 at 19:06
  • do you know an option that will ensure that the duration of the track gets stored in the fragmented MP4 as well? Is I use this file reader, it says that the duration is undefined http://download.tsi.telecom-paristech.fr/gpac/mp4box.js/filereader.html – Stefan Falk Nov 14 '20 at 08:55
  • Which specific field? – Gyan Nov 14 '20 at 09:15
  • If I look at the File Overview, the duration is 00:00 (or undefined). I'm still not sure how I can seek inside an fMP4. In fact, I am not even sure if it really _has_ to be fragmented in my case. All I want to support is that the user can listen to the audio right away without having to download the entire file. For this to work there should be no need for more fragments than one, right? – Stefan Falk Nov 14 '20 at 09:20
  • At least technically but then there is MSEs `SourceBuffer` .. – Stefan Falk Nov 14 '20 at 09:21