Pymediainfo
Pymediainfo is a Python wrapper for the MediaInfo library, providing an easy way to extract detailed metadata from multimedia files. It is actively maintained with frequent updates, including support for new Python versions and bundled MediaInfo library updates.
Warnings
- breaking Pymediainfo v7.0.0 dropped support for Python 3.7 and 3.8. The minimum required Python version is now 3.9.
- breaking In v7.0.0, optional arguments to `MediaInfo.parse()` became positional-only. This means you must pass them as keyword arguments (`param=value`) rather than positional arguments (`value`).
- breaking Pymediainfo v6.0.0 dropped support for Python 3.6.
- gotcha The `pymediainfo` library is a wrapper and requires the underlying `MediaInfo` C++ library to be installed on the system to parse actual media files. Without it, only pre-generated XML can be processed. While wheels for some platforms bundle MediaInfo, Linux users often need to install `libmediainfo` separately.
- gotcha Due to how the underlying `MediaInfo` C++ library works, `MediaInfo.parse()` should not be called simultaneously from multiple threads with *different* arguments. Doing so can cause inconsistencies or failures because library options are shared across threads.
Install
-
pip install pymediainfo -
pip install pymediainfo --no-binary pymediainfo
Imports
- MediaInfo
from pymediainfo import MediaInfo
Quickstart
from pymediainfo import MediaInfo
import os
# Create a dummy media file for demonstration
dummy_file_path = "./dummy_media.txt"
with open(dummy_file_path, "w") as f:
f.write("This is a dummy file to simulate media content.\n")
f.write("It won't have real media info, but demonstrates parsing.")
try:
media_info = MediaInfo.parse(dummy_file_path)
# Iterate over all tracks and print basic information
if media_info.tracks:
print(f"Analyzing: {os.path.basename(dummy_file_path)}")
for track in media_info.tracks:
print(f" Track Type: {track.track_type}")
if hasattr(track, 'format'):
print(f" Format: {track.format}")
if hasattr(track, 'file_size'):
print(f" File Size: {track.file_size} bytes")
# You can access many other attributes dynamically
# For example, for video: track.width, track.height
# For audio: track.channel_s, track.sampling_rate
else:
print(f"No tracks found for {os.path.basename(dummy_file_path)}. This might happen for non-media files or if MediaInfo library isn't fully functional.")
finally:
# Clean up the dummy file
if os.path.exists(dummy_file_path):
os.remove(dummy_file_path)