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 data that provides information about other data, such as audio and video data. Metadata for audio and video files includes file format, file resolution, file size, duration, bitrate, etc. By accessing this metadata, we can manage media more efficiently and analyze the metadata to obtain some useful information. In this article, we will take a look at some of the libraries or modules provided by Python for accessing metadata of audio and video files.
Some libraries for accessing audio file metadata are -
Mutagen is an open source Python module for processing audio metadata. It supports almost all types of audio files such as mp3, mp4, OGG, FLAC, etc. Mutagen is used to access the metadata of audio files and also to manipulate audio data.
Before using mutagen, we can use the pip command in Python to install mutagen.
pip install mutagen
Pip is a Python package manager.Pip install mutagenwill install the mutagen library in your local files.
audio["TIT2"].text[0]
audio["TIT2"]The property returns an object in the form of key-value pairs that contains various information about the audio file. Access the title of the audio file using the text key in the object.
audio.info.length
audio.info returns an object containing all information about the audio file. The length of an audio file can be accessed using audio.info.length, which returns the length in seconds.
The following is an example of how to access the metadata of an mp3 file using the mutagen.mp3 module in 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
222.17142857142858 Suhana Safar Par Prem Nagar Hai
eyeD3 is also a Python open source library for processing audio files, especially mp3 audio files. Using eyeD3, we can read and write metadata of audio files, and also operate or update audio files.
Before using eyeD3, we can install it using the pip command in python -
pip install eyeD3
Pip is a Python package manager.Pip install eyeD3Install the eyeD3 library in a local file.
eyed3.load(your_audio_file)
eyed3.load()The function loads audio files and can be stored in variables. Parameteryour_audio_fileis the path to the audio file you need to load.
Use eyed3.load function to load any audio file
Use the audio.info.time_secs property to access the audio file length.
Use the audio.tag.title property to access the title of the audio file.
Here is an example of using eyeD3 to access audio file metadata.
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
223.33 Suhana Safar Par Prem Nagar Hai
Python also has some open source libraries for accessing video file metadata, such as -
moviepy is an open source Python library for video editing. It can also be used to access the metadata of video files. Moviepy supports a variety of video file formats, such as mp4, AVI, MOV, etc. Moviepy can help us read and write metadata of video files and operate on video files.
Before using moviepy, you must install the moviepy library using the pip command in python: -
pip install moviepy
To usemoviepyto access the metadata of a video file we must -
Import VideoFileClip module from moviepy.editor
Use VideoClipFile to load video files
Use movipy's attributes (such as duration, size, etc.) to access the metadata of the loaded video file.
We will import the VideoFileClip module from the moviepy.editor package and then use the VideoFileClip module to load our video files.Video.durationandvideo.sizereturn 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
50.74 [1920, 1080]
OpenCV is an open source computer vision library used in Python for processing video data. It can also be used to access metadata of various video file formats such as MP4, AVI, MOV, etc. You can use OpenCV to read and write metadata of video files and perform video processing.
Before using the cv2 module, we must 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 installed the openCv library in your local files.
video.get(cv2.CAP_PROP_FPS)
Cv2 .CAP_PROP_FPS Returns the frame rate of the video file. Use the video.get() function to return the frame rate of a specific video.
video.get(cv2.CAP_PROP_FRAME_WIDTH)
cv2.CAP_PROP_FRAME_WIDTH returns the frame width of the video file. Use the video.get() function to return the frame width of a specific video.
video.get(cv2.CAP_PROP_FRAME_HEIGHT)
cv2.CAP_PROP_FRAME_HEIGHT Returns the frame height of the video file. Use the video.get() function to return the frame height of a specific video.
要使用OpenCV访问视频文件的元数据,我们必须执行以下操作−
导入opencv
使用 cv2.VideoCapture 属性加载视频文件
使用 CAP_PROP_FRAME_WIDTH、FRAME_HEIGHT 等属性访问文件的各种元数据。
使用 cv2.VideoCapture 模块将视频文件导入变量中。现在,这个存储的视频文件可用于使用 cv2 模块中的 CAP_PROP_FPS、CAP_PROP_FRAME_WIDTH 等属性获取元数据。
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
60.0 1920.0 1080.0
在本文中,我们讨论了 Python 提供的一些用于访问音频和视频文件元数据的库。对于音频文件,我们探索了 mutagen 和 eyeD3 库;对于视频文件,我们探索了 moviepy 和 openCV 库。 Python 中还有许多其他库可用于访问音频和视频文件的元数据。最好浏览这些库的文档,以便更好地了解这些库提供的功能。
The above is the detailed content of Access metadata of various audio and video files using Python. For more information, please follow other related articles on the PHP Chinese website!