Access meta data of various audio and video files using Python

We can access the metadata of audio and video files using specialized Python libraries. Metadata provides information about media files like format, resolution, file size, duration, and bit rate. By accessing this metadata, we can manage media more efficiently and extract useful information for analysis.

Accessing Audio Metadata

Python offers several libraries for audio metadata extraction. Here are the two most popular ones ?

Using Mutagen Library

Mutagen is an open-source Python module that handles audio metadata for almost all audio formats including MP3, MP4, OGG, and FLAC. It can both read and manipulate audio metadata.

Installation ?

pip install mutagen

Key Methods

The main methods for accessing metadata are ?

  • audio["TIT2"].text[0] Gets the title of the audio file

  • audio.info.length Returns the duration in seconds

  • audio["TPE1"].text[0] Gets the artist name

  • audio["TALB"].text[0] Gets the album name

Example

from mutagen.mp3 import MP3

# Load audio file
audio = MP3("sample_audio.mp3")

# Access metadata
print("Duration:", audio.info.length, "seconds")
print("Title:", audio["TIT2"].text[0])
print("Artist:", audio["TPE1"].text[0])
print("Bitrate:", audio.info.bitrate, "bps")

Using eyeD3 Library

eyeD3 is another open-source library specifically designed for MP3 files. It provides easy access to ID3 tags and can read, write, and manipulate audio metadata.

Installation ?

pip install eyeD3

Example

import eyed3

# Load audio file
audio = eyed3.load("sample_audio.mp3")

# Access metadata
print("Duration:", audio.info.time_secs, "seconds")
print("Title:", audio.tag.title)
print("Artist:", audio.tag.artist)
print("Album:", audio.tag.album)
print("Bitrate:", audio.info.bit_rate[1], "kbps")

Accessing Video Metadata

For video files, Python provides several libraries to extract metadata information ?

Using MoviePy Library

MoviePy is an open-source library primarily used for video editing but also excellent for metadata extraction. It supports various formats like MP4, AVI, and MOV.

Installation ?

pip install moviepy

Example

from moviepy.editor import VideoFileClip

# Load video file
video = VideoFileClip("sample_video.mp4")

# Access metadata
print("Duration:", video.duration, "seconds")
print("Size (width x height):", video.size)
print("Frame rate:", video.fps, "fps")

# Close the video file
video.close()

Using OpenCV Library

OpenCV is a powerful computer vision library that can also extract video metadata. It provides detailed information about video properties.

Installation ?

pip install opencv-python-headless

Example

import cv2

# Load video file
video = cv2.VideoCapture("sample_video.mp4")

# Access metadata
fps = video.get(cv2.CAP_PROP_FPS)
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
duration = frame_count / fps

print("Frame rate:", fps, "fps")
print("Resolution:", int(width), "x", int(height))
print("Duration:", duration, "seconds")
print("Total frames:", int(frame_count))

# Release the video file
video.release()

Comparison of Libraries

Library Media Type Best For Key Features
Mutagen Audio Multiple audio formats Comprehensive metadata support
eyeD3 Audio (MP3) MP3 ID3 tags Easy-to-use API
MoviePy Video Video editing projects High-level interface
OpenCV Video Computer vision tasks Detailed video properties

Conclusion

Python provides excellent libraries for extracting metadata from audio and video files. Use Mutagen for comprehensive audio metadata support, eyeD3 for MP3-specific tasks, MoviePy for video editing workflows, and OpenCV for detailed video analysis.

Updated on: 2026-03-27T01:03:14+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements