Access meta data of various audio and video files using Python


We can access the metadata of audio files using Mutagen and the eyeD3 module in Python. For video metadata, we can use movies and the OpenCV library in Python. Metadata is the data that provides information about other data like audio and video data. Metadata of audio and video files include the format of the file, resolution of the file, file size, duration, bit rate, etc. By accessing these metadata we can manage the media more efficiently as well as analyze the metadata to get some useful information. In this article, we will look at some of the libraries or modules which Python provides for accessing metadata of audio and video files.

Accessing Audio Metadata

Some of the libraries which are used to access the metadata of audio files are −

Using Mutagen Library

Mutagen is an open-source python module that is used to handle audio metadata. It supports almost all kinds of audio files like mp3,mp4, OGG, FLAC, etc. Mutagen is used to access the metadata of audio files and also used to manipulate audio data.

Before using mutagen we can install mutagen using the pip command in Python.

pip install mutagen

Pip is a Python package manager. Pip install mutagen installs the mutagen library in your local file.

Syntax

audio["TIT2"].text[0]

The audio[“TIT2”] attribute returns an object with various information about the audio file in the form of key-value pairs. The title of the audio file is accessed using the text key from the object.

audio.info.length

The audio.info returns an object which contains all the information about the audio file. The length of the audio file is accessed using audio.info.length which returns the length in seconds.

Example

Here is an example of how the metadata of an mp3 file can be accessed using the mutagen.mp3 module from mutagen.

from mutagen.mp3 import MP3

audio = MP3("audio.mp3") # put your audio file in the place of audio.mp3
print(audio.info.length)  # Print the length of the audio file
print(audio["TIT2"].text[0])  # Print the title of the audio file

Output

222.17142857142858
Suhana Safar Par Prem Nagar Hai

Using eyeD3 Library

The eyeD3 is also an open-source library in Python that is used to work with audio files, specifically mp3 audio files. With eyeD3 we can read and write metadata of audio files and also manipulate or update audio files.

Before using eyeD3 we can install it using pip command in python as −

pip install eyeD3

Pip is a python package manager. Pip install eyeD3 installs the eyeD3 library in your local file.

Syntax

eyed3.load(your_audio_file)

The eyed3.load() function loads the audio file and can be stored in a variable. The parameter your_audio_file is the path of the audio file which you need to load.

Algorithm

  • Load any audio file using eyed3.load function

  • Access the audio file length using audio.info.time_secs attribute.

  • Access the audio file title using audio.tag.title attribute.

Example

Here is an example of how we can access metadata of an audio file using eyeD3.

import eyed3

audio = eyed3.load("audio.mp3") # put your audio file in the place of audio.mp3
print(audio.info.time_secs)  # Print the length of the audio file
print(audio.tag.title)  # Print the title of the audio file

Output

223.33
Suhana Safar Par Prem Nagar Hai

Accessing Video Metadata

Python also has some open source libraries for accessing metadata of video files such as −

Method 1: Using Moviepy Library

moviepy is an open-source Python library that is used for video editing. It can also be used to access the metadata of video files. Moviepy supports various video file formats such as mp4, AVI, MOV, etc. Moviepy can help us in reading and writing metadata of video files and also manipulate the video files.

Before using moviepy you have to install moviepy library using pip command in python as −

pip install moviepy

Algorithm

To use moviepy to access metadata of video file we have to −

  • Import VideoFileClip module from moviepy.editor

  • Load the video file using VideoClipFile

  • Access the metadata of loaded video files using attributes of movipy like duration,size etc.

Example

We will import the VideoFileClip module from moviept.editor package and then load our video file using the VideoFileClip module. Video.duration and video.size returns the duration and display size of the video file respectively.

from moviepy.editor import VideoFileClip

video = VideoFileClip("video.mp4")
print(video.duration)  # Print the duration of the video
print(video.size)  # Print the size of the video

Output

50.74
[1920, 1080]

Using OpenCV Library

OpenCV is an open-source computer vision library in Python that is used to work with video data. It can also be used to access the metadata of various video file formats like MP4.AVi.MOV etc. Metadata of video files can be read and written using OpenCV and video manipulation can also be done.

Before using the cv2 module we have to install opencv-python-headless using the pip command in Python −

pip install opencv-python-headless

Pip is a Python package manager. Pip install opencv-python-headless installs the openCv library in your local file.

Syntax

video.get(cv2.CAP_PROP_FPS)

Cv2 .CAP_PROP_FPS returns the frame rate of the video file. The frame rate of a specific video is returned using video.get() function.

video.get(cv2.CAP_PROP_FRAME_WIDTH)

cv2.CAP_PROP_FRAME_WIDTH returns the frame width of the video file. The frame width of a specific video is returned using video.get() function.

video.get(cv2.CAP_PROP_FRAME_HEIGHT)

cv2.CAP_PROP_FRAME_HEIGHT returns the frame height of the video file. The frame height of a specific video is returned using video.get() function.

Algorithm

To use opencv for accessing metadata of video files we have to −

  • Import opencv

  • Load the video file using cv2.VideoCapture attribute

  • Access various metadata of the file using attributes like CAP_PROP_FRAME_WIDTH, FRAME_HEIGHT etc.

Example

Using cv2.VideoCapture module imports the video file in a variable. Now this stored video file can be used to get the metadata using CAP_PROP_FPS, CAP_PROP_FRAME_WIDTH, etc attributes from the cv2 module.

import cv2

video = cv2.VideoCapture("video.mp4")
fps = video.get(cv2.CAP_PROP_FPS)  # Get the frame rate of the video
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)  # Get the width of the video
height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)  # Get the height of the video

print(fps, width, height)  # Print the frame rate, width, and height of the video

Output

60.0 1920.0 1080.0

Conclusion

In this article, we discussed some of the libraries which are provided by Python for accessing metadata of audio and video files. For audio files, we explored the mutagen and eyeD3 libraries and for video files, we explored the moviepy and openCV libraries. There are many other libraries in Python that can be used to access the metadata of audio and video files. It's better to go through the docs of these libraries for a better understanding of the functionality these libraries provide.

Updated on: 17-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements