Skip to main content

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.

Release files for pymediainfo 7.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymediainfo 7.0.1
File Size Uploaded
pymediainfo-7.0.1.tar.gz 441.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pymediainfo 7.0.1
File
pymediainfo-7.0.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
pymediainfo-7.0.1-py3-none-win32.whl Python 3 none Windows x86-32 Details
pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl Python 3 none Linux glibc 2.27+ x86-64 Details
pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl Python 3 none Linux glibc 2.27+ ARM64 Details
pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl Python 3 none macOS 10.10+ universal2 (ARM64, x86-64) Details

Total release size: 25.6 MB

Release files / pymediainfo-7.0.1.tar.gz

Download URL pymediainfo-7.0.1.tar.gz
Size 441.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0d5df59ecc615e24c56f303b8f651579c6accab7265715e5d429186d7ba21514
BLAKE2b-256 checksum
How to use checksums
4d8080a6fb21005b81e30f6193d45cba13857df09f5d483e0551fa6fbb3aaeed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / pymediainfo-7.0.1-py3-none-win_amd64.whl

Download URL pymediainfo-7.0.1-py3-none-win_amd64.whl
Size 3.3 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
13224fa7590e198763b8baf072e704ea81d334e71aa32a469091460e243893c7
BLAKE2b-256 checksum
How to use checksums
e7269d50c2a330541bc36c0ea7ce29eeff5b0c35c2624139660df8bcfa9ae3ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / pymediainfo-7.0.1-py3-none-win32.whl

Download URL pymediainfo-7.0.1-py3-none-win32.whl
Size 3.1 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
01bcaf82b72cefbf4b96f13b2547e1b2e0e734bab7173d7c33f7f01acc07c98b
BLAKE2b-256 checksum
How to use checksums
ed7fc48f8514cb60c9ff9be81b6f383e73e66c7461ef854a1b62628e3c823f13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl

Download URL pymediainfo-7.0.1-py3-none-manylinux_2_27_x86_64.whl
Size 6.0 MB
Tags Linux glibc 2.27+ x86-64 Python 3
SHA-256 checksum
How to use checksums
cde98112f1ce486589b17a12e5da42085faea996224f7c67fa45b8c1dca719c6
BLAKE2b-256 checksum
How to use checksums
0210a9bc1446a48d3a15940eb1af79a71978f368f27e2cc86f9ec3ec2d206a20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl

Download URL pymediainfo-7.0.1-py3-none-manylinux_2_27_aarch64.whl
Size 5.8 MB
Tags Linux glibc 2.27+ ARM64 Python 3
SHA-256 checksum
How to use checksums
3648e2379fa67bd02433d1e28c707df3a53834dd480680615a9fefd2266f1182
BLAKE2b-256 checksum
How to use checksums
77dfbc6b5a08e908c64a81f6ff169716d408ce7380ceff44e1eceb095f49e0dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl

Download URL pymediainfo-7.0.1-py3-none-macosx_10_10_universal2.whl
Size 7.0 MB
Tags Python 3 macOS 10.10+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
286f3bf6299be0997093254e0f371855bc5cf2aaf8641d19455a011e3ee3a84d
BLAKE2b-256 checksum
How to use checksums
a64ad895646df3d3ff617b54d7f06a02ed9d6f5b86673030a543927310e0f7ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

7.0.1 This release

6 release files

7.0.0

6 release files

6.1.0

4 release files

6.0.1

4 release files

6.0.0

4 release files

5.1.0

4 release files

5.0.4

4 release files

5.0.3

4 release files

5.0.2

4 release files

5.0.1

1 release file

5.0

2 release files

4.3

4 release files

4.2.1

4 release files

4.2

4 release files

4.1

4 release files

4.0

4 release files

3.2.1

4 release files

3.2

4 release files

3.1

1 release file

3.0

1 release file

2.3.0

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.9

1 release file

2.1.8

1 release file

2.1.7

1 release file

2.1.6

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

2.1

1 release file

2.0

1 release file

1.4.0

1 release file

1.3.5

1 release file

1.3.4

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page