Skip to main content

A Python wrapper for the MediaInfo library.

Project description

pymediainfo

PyPI Supported Python versions Repology info GitHub Build status

pymediainfo is a wrapper for the MediaInfo library. It makes it easy to extract detailed information from multimedia files.

Compatibility

pymediainfo is compatible with the following:

  • Platforms: Linux, macOS and Windows.
  • Python Versions: Tested with Python 3.9 (the minimum required version) to 3.13, as well as PyPy3.

Installation

Please note that, without the MediaInfo library, pymediainfo cannot parse media files. This severely limits its functionality, allowing it to process only pre-generated XML output from MediaInfo.

Linux distribution Packages

Packages are available for most major Linux distributions. They often depend on the MediaInfo library and are the preferred way to install pymediainfo on Linux, as they allow for independent updates to pymediainfo and the MediaInfo library itself.

PyPI on Linux, macOS and Windows

If pymediainfo is not available for your Linux distribution, or if you're running macOS or Windows, you can install it from PyPI:

python -m pip install pymediainfo

Wheels containing a bundled version of the MediaInfo library are available for:

  • Linux x86-64 and ARM64.
  • macOS x86-64 and ARM64.
  • Windows x86-64 and x86.

If you do not want to use the wheels (for instance if you want to use the system-wide MediaInfo library instead of the bundled one):

python -m pip install pymediainfo --no-binary pymediainfo

Usage

Here are a few examples demonstrating how to use pymediainfo.

Getting information from an image

The MediaInfo class provides a parse() method which takes paths as input and returns MediaInfo objects.

Example snippet

from pymediainfo import MediaInfo

media_info = MediaInfo.parse("/home/user/image.jpg")
# Tracks can be accessed using the 'tracks' attribute or through shorthands
# such as 'image_tracks', 'audio_tracks', 'video_tracks', etc.
general_track = media_info.general_tracks[0]
image_track = media_info.image_tracks[0]
print(
  f"{image_track.format} of {image_track.width}×{image_track.height} pixels"
  f" and {general_track.file_size} bytes."
)

Example output

JPEG of 828×828 pixels and 19098 bytes.

Getting information from a video

In this example, we take advantage of the to_data() method, which returns a dict containing all attributes from a MediaInfo or Track object. This makes it easier to inspect tracks even when their attributes are unknown.

Example snippet

from pprint import pprint
from pymediainfo import MediaInfo

media_info = MediaInfo.parse("my_video_file.mp4")
for track in media_info.tracks:
    if track.track_type == "Video":
        print(f"Bit rate: {track.bit_rate}, Frame rate: {track.frame_rate}, Format: {track.format}")
        print("Duration (raw value):", track.duration)
        print("Duration (other values:")
        pprint(track.other_duration)
    elif track.track_type == "Audio":
        print("Track data:")
        pprint(track.to_data())

Example output

Bit rate: 3117597, Frame rate: 23.976, Format: AVC
Duration (raw value): 958
Duration (other values):
['958 ms',
'958 ms',
'958 ms',
'00:00:00.958',
'00:00:00;23',
'00:00:00.958 (00:00:00;23)']
Track data:
{'bit_rate': 236392,
'bit_rate_mode': 'VBR',
'channel_layout': 'L R',
'channel_positions': 'Front: L R',
'channel_s': 2,
'codec_id': 'mp4a-40-2',
'commercial_name': 'AAC',
'compression_mode': 'Lossy',
…
}

Accessing Track attributes

Since the attributes from a Track are dynamically created during parsing, there isn't a firm definition of what will be available at runtime.

In order to make consuming objects easier, the __getattribute__ method from Track objects has been overridden to return None when a non-existent attribute is accessed, instead of raising AttributeError.

Example snippet

from pymediainfo import MediaInfo

media_info = MediaInfo.parse("my_video_file.mp4")
for track in media_info.tracks:
    if track.bit_rate is None:
        print(f"{track.track_type} tracks do not have a bit rate associated with them")
    else:
        print(f"Track {track.track_id} of type {track.track_type} has a bit rate of {track.bit_rate} b/s")

Example output

General tracks do not have a bit rate associated with them
Track 1 of type Video has a bit rate of 4398075 b/s
Track 2 of type Audio has a bit rate of 131413 b/s
Menu tracks do not have a bit rate associated with them

Parsing pre-generated MediaInfo XML output

pymediainfo relies on MediaInfo's OLDXML output to create MediaInfo objects.

It is possible to create a MediaInfo object from an existing XML string. For instance if someone sent you the output of mediainfo --output=OLDXML, you can call the MediaInfo constructor directly.

Example snippet

from pymediainfo import MediaInfo

raw_xml_string = """<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo version="24.11">
<File>
<track type="General">
<Complete_name>binary_file</Complete_name>
<File_size>1.00 Byte</File_size>
</track>
</File>
</Mediainfo>"""
media_info = MediaInfo(raw_xml_string)
print(f"File name is: {media_info.general_tracks[0].complete_name}")

Example output

File name is: binary_file

Text output (à la mediainfo)

If you want a text report, similar to what mediainfo my_video_file.mp4 outputs, use the output="text" argument with the parse() method. In this case, it will return a string, not a MediaInfo object.

Example snippet

from pymediainfo import MediaInfo

# To mirror a simple call to "mediainfo" without the "--Full" or "-f" option, we
# set "full=False". Leaving it at the default of "full=True" would result in
# more verbose output.
print(MediaInfo.parse("my_video_file.mp4", output="text", full=False))

Example output

General
Complete name                            : my_video_file.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
[…]

Documentation

For more detailed information, please refer to the reference documentation available at https://pymediainfo.readthedocs.io/.

Issues and Questions

For feature requests and bug reports, please use the GitHub issue tracker at https://github.com/sbraz/pymediainfo/issues.

If you have any questions, feel free to ask in the discussions at https://github.com/sbraz/pymediainfo/discussions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymediainfo-7.0.1.tar.gz (441.6 kB view details)

Uploaded Source

Built Distributions

pymediainfo-7.0.1-py3-none-win_amd64.whl (3.3 MB view details)

Uploaded Python 3Windows x86-64

pymediainfo-7.0.1-py3-none-win32.whl (3.1 MB view details)

Uploaded Python 3Windows x86

pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl (6.0 MB view details)

Uploaded Python 3manylinux: glibc 2.27+ x86-64

pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl (5.8 MB view details)

Uploaded Python 3manylinux: glibc 2.27+ ARM64

pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl (7.0 MB view details)

Uploaded Python 3macOS 10.10+ universal2 (ARM64, x86-64)

File details

Details for the file pymediainfo-7.0.1.tar.gz.

File metadata

  • Download URL: pymediainfo-7.0.1.tar.gz
  • Upload date:
  • Size: 441.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pymediainfo-7.0.1.tar.gz
Algorithm Hash digest
SHA256 0d5df59ecc615e24c56f303b8f651579c6accab7265715e5d429186d7ba21514
MD5 9c17d48af6221a7cd305c6731f42c567
BLAKE2b-256 4d8080a6fb21005b81e30f6193d45cba13857df09f5d483e0551fa6fbb3aaeed

See more details on using hashes here.

File details

Details for the file pymediainfo-7.0.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: pymediainfo-7.0.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 3.3 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pymediainfo-7.0.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 13224fa7590e198763b8baf072e704ea81d334e71aa32a469091460e243893c7
MD5 e41c0678eacde21aa82a1692fbf55894
BLAKE2b-256 e7269d50c2a330541bc36c0ea7ce29eeff5b0c35c2624139660df8bcfa9ae3ce

See more details on using hashes here.

File details

Details for the file pymediainfo-7.0.1-py3-none-win32.whl.

File metadata

  • Download URL: pymediainfo-7.0.1-py3-none-win32.whl
  • Upload date:
  • Size: 3.1 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pymediainfo-7.0.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 01bcaf82b72cefbf4b96f13b2547e1b2e0e734bab7173d7c33f7f01acc07c98b
MD5 2c62d1f68831c34073715dcdd6fc1ff1
BLAKE2b-256 ed7fc48f8514cb60c9ff9be81b6f383e73e66c7461ef854a1b62628e3c823f13

See more details on using hashes here.

File details

Details for the file pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 cde98112f1ce486589b17a12e5da42085faea996224f7c67fa45b8c1dca719c6
MD5 229aa73253ace15fdb4765fadd9e1a52
BLAKE2b-256 0210a9bc1446a48d3a15940eb1af79a71978f368f27e2cc86f9ec3ec2d206a20

See more details on using hashes here.

File details

Details for the file pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl.

File metadata

File hashes

Hashes for pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl
Algorithm Hash digest
SHA256 3648e2379fa67bd02433d1e28c707df3a53834dd480680615a9fefd2266f1182
MD5 0001a0bd1e82afc686174cb19c5c6443
BLAKE2b-256 77dfbc6b5a08e908c64a81f6ff169716d408ce7380ceff44e1eceb095f49e0dc

See more details on using hashes here.

File details

Details for the file pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl.

File metadata

File hashes

Hashes for pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl
Algorithm Hash digest
SHA256 286f3bf6299be0997093254e0f371855bc5cf2aaf8641d19455a011e3ee3a84d
MD5 a439e74d8eb95141727fd22501f74dea
BLAKE2b-256 a64ad895646df3d3ff617b54d7f06a02ed9d6f5b86673030a543927310e0f7ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page