0

It seems I want to convert audios, which I want to stream on my website, to audio/mp4; codecs="mp4a.40.2".

Using ffmpeg-cli-wrapper, I am converting my uploaded audio files with this command here:

ffmpeg -i /tmp/input.any -acodec aac -b:a 256000 /tmp/output.aac

On the client I am creating a SourceBuffer like this:

this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/mp4; codecs="mp4a.40.2"');

The errors are:

Chrome:

NotSupportedError: Failed to load because no supported source was found.

Firefox:

NotSupportedError: The media resource indicated by the src attribute or assigned media provider object was not suitable.

Here comes the fun part:

If I create the SourceBuffer using audio/aac as mime-type:

this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/aac');

the audio gets played correctly on Chrome but Firefox says:

MediaSource.addSourceBuffer: Type not supported in MediaSource

Update

After changing the command to

ffmpeg -i /tmp/input.any -acodec aac -b:a 256000 /tmp/output.mp4
                                                             ^^^ 

Chrome/Firefox do not give an error when using audio/mp4; codecs="mp4a.40.2", but the audio is not being played.


See

Stefan Falk
  • 18,764
  • 34
  • 144
  • 286

1 Answers1

4

In your ffmpeg command, use the extension .m4a (or .mp4), not .aac:

ffmpeg -i /tmp/input.any -acodec aac -b:a 256000 /tmp/output.m4a

The .aac extension is used for AAC in the ADTS container, which is not a format that is widely used or supported on the web. The .m4a or .mp4 extension is used for AAC in the MP4 container, which is far more common.

Or, if for some reason you really want the file to be named output.aac but yet want it to contain AAC in MP4, then you can specify the output format instead of having ffmpeg guess the format that you want based on the output file extension:

ffmpeg -i /tmp/input.any -acodec aac -b:a 256000 -f mp4 /tmp/output.aac

But if you use a non-standard extension like that then you may also need to update your web server configuration so that it will report the correct media type.

mark4o
  • 52,963
  • 16
  • 81
  • 99
  • So the command that I used now was: `ffmpeg -y -v quiet -i /tmp/input -acodec aac -b:a 256000 /tmp/output.m4a` and the mime-type I use for the `MediaSource` is `audio/mp4; codecs="mp4a.40.2"` but on Chrome, the audio reply does not work. :/ – Stefan Falk Oct 18 '20 at 12:56
  • It's not crashing or giving any errors. It just does not start playing the audio .. – Stefan Falk Oct 18 '20 at 12:57
  • As a quick hack, you can probably add `-movflags faststart` to the `ffmpeg` command - this will put the moov box (this is the bit that holds the sample metadata) the beginning of the file and you might get playback. But really you probably want to be using fragmented mp4 with MSE, because you can't just add byteranges of sample data when seeking in the same way you can with MP3. Fragments relate to specific time ranges, and you append entire MP4 fragments for the bit you are interested in. `ffmpeg` can probably be made to output these, or you can investigate `MP4box` or `bento` – Anonymous Coward Oct 19 '20 at 13:09
  • @AnonymousCoward Um... okay I guess once I've figured out how to create a fragmented mp4, the mime-type I am using won't work anymore, right? – Stefan Falk Oct 19 '20 at 16:37
  • Managed to create an fMP4 but now, of course, I cannot stream this thing ... – Stefan Falk Oct 19 '20 at 18:11
  • I created another question for this issue https://stackoverflow.com/questions/64433422/how-to-append-fmp4-chunks-to-sourcebuffer – Stefan Falk Oct 19 '20 at 18:50