How to Extract Video Metadata in Python

Learn how to extract video or audio file metadata in Python with the help of ffmpeg framework and its ffmpeg-python wrapper.
  · 2 min read · Updated may 2024 · Python for Multimedia

Get a head start on your coding projects with our Python Code Generator. Perfect for those times when you need a quick solution. Don't wait, try it today!

There are many reasons why you want to include the metadata of a video or any media file in your Python application. Video metadata is all available information about a video file, such as width, height, codec type, fps, duration, and many more.

In this quick tutorial, you will learn how you can extract video or audio metadata in Python using FFmpeg.

To make everything work properly, you need to install FFmpeg. Use this link to get it installed in your environment. Once you have it installed, you need to install the Python wrapper:

$ pip install ffmpeg-python

There are a lot of Python wrappers of FFmpeg. However, ffmpeg-python seems to work well for both simple and complex usage.

Below is the code responsible for extracting the metadata:

import ffmpeg
import sys
from pprint import pprint # for printing Python dictionaries in a human-readable way

# read the audio/video file from the command line arguments
media_file = sys.argv[1]
# uses ffprobe command to extract all possible metadata from the media file
pprint(ffmpeg.probe(media_file)["streams"])

We're getting the media file path from the command-line arguments, so we don't have to modify the code whenever we want to extract the metadata of a new media file.

The ffmpeg.probe() method uses the ffprobe command under the hood. We also use pprint instead of print, so it'll print the Python dictionary in a human-readable way. I'm going to run it on a video file:

$ python extract_media_metadata.py zoo.mp4

Output:

[{'avg_frame_rate': '24/1',
  'bit_rate': '234798',
  'bits_per_raw_sample': '8',
  'chroma_location': 'left',
  'closed_captions': 0,
  'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
  'codec_name': 'h264',
  'codec_tag': '0x31637661',
  'codec_tag_string': 'avc1',
  'codec_time_base': '1/48',
  'codec_type': 'video',
  'coded_height': 240,
  'coded_width': 320,
  'display_aspect_ratio': '4:3',
  'disposition': {'attached_pic': 0,
                  'clean_effects': 0,
                  'comment': 0,
                  'default': 1,
                  'dub': 0,
                  'forced': 0,
                  'hearing_impaired': 0,
                  'karaoke': 0,
                  'lyrics': 0,
                  'original': 0,
                  'timed_thumbnails': 0,
                  'visual_impaired': 0},
  'duration': '18.958333',
  'duration_ts': 232960,
  'has_b_frames': 2,
  'height': 240,
  'index': 0,
  'is_avc': 'true',
  'level': 13,
  'nal_length_size': '4',
  'nb_frames': '455',
  'pix_fmt': 'yuv420p',
  'profile': 'High',
  'r_frame_rate': '24/1',
  'refs': 1,
  'sample_aspect_ratio': '1:1',
  'start_pts': 0,
  'start_time': '0.000000',
  'tags': {'handler_name': 'VideoHandler', 'language': 'eng'},
  'time_base': '1/12288',
  'width': 320},
 {'avg_frame_rate': '0/0',
  'bit_rate': '69528',
  'bits_per_sample': 0,
  'channel_layout': 'mono',
  'channels': 1,
  'codec_long_name': 'AAC (Advanced Audio Coding)',
  'codec_name': 'aac',
  'codec_tag': '0x6134706d',
  'codec_tag_string': 'mp4a',
  'codec_time_base': '1/48000',
  'codec_type': 'audio',
  'disposition': {'attached_pic': 0,
                  'clean_effects': 0,
                  'comment': 0,
                  'default': 1,
                  'dub': 0,
                  'forced': 0,
                  'hearing_impaired': 0,
                  'karaoke': 0,
                  'lyrics': 0,
                  'original': 0,
                  'timed_thumbnails': 0,
                  'visual_impaired': 0},
  'duration': '18.943000',
  'duration_ts': 909264,
  'index': 1,
  'max_bit_rate': '69528',
  'nb_frames': '889',
  'profile': 'LC',
  'r_frame_rate': '0/0',
  'sample_fmt': 'fltp',
  'sample_rate': '48000',
  'start_pts': 0,
  'start_time': '0.000000',
  'tags': {'handler_name': 'SoundHandler', 'language': 'eng'},
  'time_base': '1/48000'}]

That's a lot of information including the duration in seconds, sample rate, codec information, and a lot more. Below is a run on an MP3 file:

$ python extract_media_metadata.py zoo.mp3

Output:

[{'avg_frame_rate': '0/0',
  'bit_rate': '64000',
  'bits_per_sample': 0,
  'channel_layout': 'mono',
  'channels': 1,
  'codec_long_name': 'MP3 (MPEG audio layer 3)',
  'codec_name': 'mp3',
  'codec_tag': '0x0000',
  'codec_tag_string': '[0][0][0][0]',
  'codec_time_base': '1/48000',
  'codec_type': 'audio',
  'disposition': {'attached_pic': 0,
                  'clean_effects': 0,
                  'comment': 0,
                  'default': 0,
                  'dub': 0,
                  'forced': 0,
                  'hearing_impaired': 0,
                  'karaoke': 0,
                  'lyrics': 0,
                  'original': 0,
                  'timed_thumbnails': 0,
                  'visual_impaired': 0},
  'duration': '18.984000',
  'duration_ts': 267902208,
  'index': 0,
  'r_frame_rate': '0/0',
  'sample_fmt': 'fltp',
  'sample_rate': '48000',
  'start_pts': 324870,
  'start_time': '0.023021',
  'time_base': '1/14112000'}]

That's it! I hope this quick tutorial helped you extract the metadata of any media file.

Related: How to Extract Audio from Video in Python

Happy coding ♥

Loved the article? You'll love our Code Converter even more! It's your secret weapon for effortless coding. Give it a whirl!

View Full Code Understand My Code
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!