Pymediainfo

7.0.1 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `pymediainfo` to parse a media file. It shows how to import the `MediaInfo` class and use its `parse` method to extract track-level metadata. The example creates a dummy file and attempts to parse it. Note that a real media file would yield more detailed information.

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)

view raw JSON →