How do I determine the video codec used in a file
Asked Answered
P

1

6

The HTML5 tag exposes a canPlayType() method to JavaScript. It takes a MIME type for the media to be played, and returns an indication as to whether the browser thinks it can actually play the media.

Unfortunately, most of the common media types ('video/mp4', for example) are container types, and the media type itself isn't enough to determine whether the content can actually be played. In response to this, RFC 4281 defines a "codecs" parameter that can be added to the MIME type to identify the specific codec.

So, for example, a type of "video/3gpp2; codecs='mp4v.20.9, mp4a.E1'" is (according to the RFC) "MPEG-4 Visual Simple Profile Level 0 plus 13K voice".

I've found a variety of places that tell me what values to use if I know the codec, but I often find myself in the position of receiving a video file with unknown provenance -- all I know is that it's got an .mp4 filetype.

How can I determine from the actual file what the correct "codecs" value is for canPlayType()?

Picasso answered 26/1, 2015 at 23:27 Comment(4)
Are you trying to do this in browser?Ardis
No. I want a procedure I can use offline to determine the correct value that a script running in the browser can use to determine whether the browser can play the video.Picasso
Any luck on this question? I'm having the same issue now.Ostracon
No. I ended up running ffprobe and mapping some of the "profile" fields for the streams to hex values.Picasso
A
0

Here's a small Python snippet that'll give you the answer for MP4-files containing h.264 video:

import re
import sys

with open(sys.argv[1], 'rb') as f:
    match = re.search(b'\x61\x76\x63\x43\x01', f.read())
    if not match:
        print('No avc1-bytestring found. File is likely not an .mp4-file containing h.264 video.')
        exit()
    f.seek(0)
    f.seek(match.end())
    print('avc1.' + f.read(3).hex())

If you save that to mp4-codec-reader.py you can get the h.264 codec like so:

python mp4-codec-reader.py <YOUR_FILE.mp4>

Note that this doesn't work h.265 (avc2) and other codecs. Other container formats may or may not work.

Artichoke answered 9/2, 2022 at 10:49 Comment(1)
Partial credit goes to: https://mcmap.net/q/1919668/-python-regex-search-for-hexadecimal-bytesArtichoke

© 2022 - 2024 — McMap. All rights reserved.