GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • The Python Code
  • Tutorials
  • Ethical Hacking
  • Machine Learning
  • General Python Tutorials
  • Web Scraping
  • Finance
  • Computer Vision
  • Python Standard Library
  • Application Programming Interfaces
  • Database
  • Game Development
  • Web Programming
  • Digital Forensics

This view

  • Original page
  • New address

GeezerVison is showing thepythoncode.com — How to Extract Video Metadata in Python - The Python Code

Kickstart your coding journey with our Python Code Assistant. An AI-powered assistant that's always ready to help. Don't miss out!

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 ♥

Save time and energy with our Python Code Generator. Why start from scratch when you can generate? Give it a try!

View Full Code Transform My Code
Sharing is caring!

Read also

How to Concatenate Video Files in Python

Learn how to join two or more video files using MoviePy library in Python

How to Combine a Static Image with Audio in Python

Learn how to add a static photo to an audio file to form a video file using MoviePy library in Python.

How to Extract PDF Metadata in Python

Learn how to use pikepdf library to extract useful information from PDF files in Python.

Comments

    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!




    Get our free Python guides

    Join 50,000+ Python programmers and enthusiasts like you!

    Topics
    Ethical Hacking Machine Learning General Python Tutorials Web Scraping Finance Computer Vision Python Standard Library Application Programming Interfaces Database Game Development Web Programming Digital Forensics Natural Language Processing Healthcare PDF File Handling Python for Multimedia GUI Programming Cryptography Packet Manipulation Using Scapy
    New tutorials View all
    • How AI Text Detection Works: The Technology Behind Modern AI Detectors
    • Beyond Guesswork: How Python Helps Identify AI-Generated Text
    • Discover the Possibilities of Python Programming
    • How to Track Your AI Visibility with Python
    • How to Automate Excel Reports in Python using Openpyxl
    Popular tutorials View all
    • How to Convert Speech to Text in Python
    • How to Read Emails in Python
    • How to Extract Tables from PDF in Python
    • How to Control your Mouse in Python
    • How to Make a Keylogger in Python
    Practical Python PDF Processing EBook - Topic - Bottom CodingFleet - Topic - Bottom
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.