Skip to content

Instantly share code, notes, and snippets.

@jimkang
Last active October 16, 2023 13:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/f23ce12c359c7465e83f to your computer and use it in GitHub Desktop.
Save jimkang/f23ce12c359c7465e83f to your computer and use it in GitHub Desktop.
How to get the video codec the MIME types that MediaSource SourceBuffers require

When you call addSourceBuffer on a MediaSource, you need to pass in a string that is the MIME type for the codec. If this isn't correct, the video won't play. (You can also pass this to the MediaSource's isTypeSupported function, though there seems to be a gap between what it thinks it can play and what it will play.)

The string looks like this:

video/mp4; codecs="avc1.42E01E, mp4a.40.2"

The values required in that string can be obtained by running the mp4file tool from mp4v2 on a video file, like so:

 mp4file --dump movie.mp4

This Stack Overflow answer from mark4o explains how to read the very verbose output from mp4file. Basically, it comes down to:

video/mp4; codecs="avc1.<profile indication><profile compatibility><level indication>, 
mp4a.<object type>.<decoder-specific info bits>"

Where:

  • profile indication = mp4file --dump movie.mp4 | grep AVCProfileIndication (Extract the hex value)
  • profile compatibility = mp4file --dump movie.mp4 | grep profile_compatibility (Extract the hex value)
  • level indication = mp4file --dump movie.mp4 | grep AVCLevelIndication (Extract the hex value)
  • object type = mp4file --dump movie.mp4 | grep objectTypeId (Extract the hex value)
  • decoder-specific info bits = Look for info under decSpecificInfo, then convert each hex digit into binary, take the first five binary digits (starting from the left), then convert that value to decimal.
@guites
Copy link

guites commented Mar 23, 2022

this stackoverflow post gives a great alternative to getting individual hex values, by using the MP4Box tool:

MP4Box -info video.mp4 2>&1 | grep RFC6381 | awk '{print $4}' | paste -sd , -

expected output:

avc1.4D002A,mp4a.40.2

So you can just throw into the mediaSource.addSourceBuffer(mimeCodec) expected format by

raw_codec=$(MP4Box -info video.mp4 2>&1 | grep RFC6381 | awk '{print $4}' | paste -sd , -);
echo "video/mp4; codecs="$raw_codec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment