Skip to main content

ffmpeg-media-type

ffmpeg-media-type is a Python library that utilizes FFmpeg to detect various media file information, such as duration, width, and height. This library provides an easy-to-use interface for extracting essential details from media files by leveraging the powerful capabilities of FFmpeg.

CI codecov pypi downloads versions license Checked with mypy Code style: black Imports: isort

Table of Contents

Installation

You can install ffmpeg-media-type via pip:

pip install ffmpeg-media-type

Note: FFmpeg must be installed on your system for this library to function correctly. Make sure you have FFmpeg installed and added to your system's PATH.

Usage

To use ffmpeg-media-type, first import the library:

import ffmpeg_media_type

Detecting Media File Information

To detect media file information, use the detect function, providing the path to the media file as a parameter:

media_info = ffmpeg_media_type.detect('https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/overlay.png')
media_info
MediaInfo(type='image', width=163, height=117, duration=None, format='png_pipe', size=2212, suggest_ext='png')

The detect function returns a model containing the following information:

  • type: The type of media file (e.g. video, audio, image, etc.).
  • duration: The duration of the media file in seconds.
  • width: The width of the media file in pixels.
  • height: The height of the media file in pixels.
  • format: The format of the media file (e.g. mp4, mp3, png, etc.).
  • size: The size of the media file in bytes.
  • suggest_ext: The suggested file extension for the media file (e.g. mp4, mp3, png, etc.).

Here's an example of how to access these details:

duration = media_info.duration
width = media_info.width
height = media_info.height

Example

import ffmpeg_media_type

# Specify the path to the media file
file_path = 'https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/SampleVideo_1280x720_1mb.mp4'

# Detect media file information
media_info = ffmpeg_media_type.detect(file_path)

# Extract information from the media_info dictionary
duration = media_info.duration
width = media_info.width
height = media_info.height

# Print the extracted information
print(f"Duration: {duration} seconds")
print(f"Width: {width} pixels")
print(f"Height: {height} pixels")
Duration: 5.312 seconds
Width: 1280 pixels
Height: 720 pixels

Enhancing Accuracy in Guessing Media File Extensions with FFmpeg

  • Typically, the media file's extension is utilized to determine its file type. Nevertheless, this approach may not always yield accurate results. For instance, a file bearing the .mp4 extension could, in reality, be an audio file.
  • The ffmpeg-media-type tool enhances the precision of media file extension guessing by leveraging the built-in format functionality of FFmpeg through the command ffmpeg -formats.

check cache for details.

Access ffprobe output

If you need to access the raw ffprobe output, you can use the ffprobe function:

ffprobe_output = ffmpeg_media_type.ffprobe('https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/SampleVideo_1280x720_1mb.mp4')

ffprobe_output
FFProbeInfo(format=FFProbeFormat(filename='https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/SampleVideo_1280x720_1mb.mp4', duration='5.312000', format_name='mov,mp4,m4a,3gp,3g2,mj2', format_long_name='QuickTime / MOV', start_time='0.000000', size='1055736', probe_score=100), streams=(FFProbeStream(index=0, width=1280, height=720, codec_type='video', codec_name='h264', codec_long_name='H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10', profile='Main', pix_fmt='yuv420p', r_frame_rate='25/1', tags=FFProbeStreamTags(rotate=0)), FFProbeStream(index=1, width=None, height=None, codec_type='audio', codec_name='aac', codec_long_name='AAC (Advanced Audio Coding)', profile='LC', pix_fmt=None, r_frame_rate='0/0', tags=FFProbeStreamTags(rotate=0))))

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. If you would like to contribute code, please fork the repository and submit a pull request.

Before submitting a pull request, make sure to run the tests using the following command:

poetry install --with dev
py.test src

Please ensure that your code follows the established coding style and passes all tests.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Metadata

Release files for ffmpeg-media-type 1.0.5

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

Source distribution (sdist)

Source distribution for ffmpeg-media-type 1.0.5
File Size Uploaded
ffmpeg_media_type-1.0.5.tar.gz 31.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ffmpeg-media-type 1.0.5
File Interpreter ABI Platform
ffmpeg_media_type-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 174.5 kB

Release files / ffmpeg_media_type-1.0.5.tar.gz

Download URL ffmpeg_media_type-1.0.5.tar.gz
Size 31.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6796d4105d375766382e0dc0aedeaba66713c97c60fb7cb5b8394baac3a82435
BLAKE2b-256 checksum
How to use checksums
29dab30e691ec67b6b2414da8c736295882a9f1c9cd85b2f617a7c3597adbde5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / ffmpeg_media_type-1.0.5-py3-none-any.whl

Download URL ffmpeg_media_type-1.0.5-py3-none-any.whl
Size 142.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cdd23ae205b0e7d82466d7db66819709a83cdeed1a244606f5b7f58e1c5314e
BLAKE2b-256 checksum
How to use checksums
0cb103dfb7831483d3ea00f55bf05aa894a05d5321085321c0f9387aa5838d21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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