2

I'd like to be able to open a given file, and see that "This is a MP4 file", or "This is a webm" file, or even "This does not appear to be a valid video"

I saw the FFmpeg wrapper, at https://code.google.com/p/pyffmpeg/, but I didn't see any sort of get_codec function inside of it.

Thoughts?

  • [How to find the mime type of a file in python?](http://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-file-in-python) contains several techniques for doing this, so that answer will answer this as well (even if it may not have been obvious from the title). – Lennart Regebro Jan 11 '12 at 09:59
  • 1
    Getting the mime type of an AVI/MKV will return its a AVI/MKV, not the internal data stream, as AVI/MKV and many other formats are containers for the compressed streams. – Geoffrey Jan 11 '12 at 10:03
  • 1
    @Geoffrey: Well, the word "format" is fuzzy. webm is a container, and so is mp4. But then he writes "get_codec", which is something different. – Lennart Regebro Jan 11 '12 at 14:29
  • @Colin: Can you clarify if you mean the container or the codec? – Lennart Regebro Jan 11 '12 at 14:29
  • @LennartRegebro - Thanks. Most mimetype magic won't work, since they're all being saved without any filename information to go by. I need to get inside the file itself to extract it. Really, knowing the specific codec, such as Divxv4 is really useful. –  Jan 17 '12 at 23:56
  • @ColinDavis: Python-magic does go inside the file. But it will not return the codec, just the file type. – Lennart Regebro Jan 18 '12 at 06:41

3 Answers3

4

Take a look at Hachoir. It 'extracts metadata from multimedia files'.

Here's their example of metadata extraction from an AVI file:

$ hachoir-metadata pacte_des_gnous.avi
Common:
- Duration: 4 min 25 sec
- Comment: Has audio/video index (248.9 KB)
- MIME type: video/x-msvideo
- Endian: Little endian
Video stream:
- Image width: 600
- Image height: 480
- Bits/pixel: 24
- Compression: DivX v4 (fourcc:"divx")
- Frame rate: 30.0
Audio stream:
- Channel: stereo
- Sample rate: 22.1 KHz
- Compression: MPEG Layer 3
Scott Griffiths
  • 21,438
  • 8
  • 55
  • 85
  • Another option is python-magic (the libmagic wrapper), which supports almost every file type known to man. For more exhaustive metadata, you can also look into python-mediainfo, a wrapper for MediaInfo which can provide exhaustive metadata for almost all video formats (and others, besides). – Dakota Feb 08 '15 at 05:14
3

My python is a bit rusty, but a quick look over the module turns these up.

To get the Codec ID:

Track.CodecCtx.codec_id

To get the Codec itself (AVCodec):

Track.Codec

AVCodec contains the codec name.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
0

You can use the python equivalent to the unix file utility via python-magic: Is there a python-equivalent of the unix "file" utility?

jlhasson
  • 1,516
  • 1
  • 15
  • 23