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.

MIT License

Downloads
2.2K
Stars
2

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ffmpeg-media-type\n", "\n", "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.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "### Table of Contents\n", "\n", "- Documentation\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Installation\n", "\n", "You can install ffmpeg-media-type via pip:\n", "\n", "bash\n", "pip install ffmpeg-media-type\n", "\n", "\n", "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Usage\n", "\n", "To use ffmpeg-media-type, first import the library:\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import ffmpeg_media_type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Detecting Media File Information\n", "\n", "To detect media file information, use the detect function, providing the path to the media file as a parameter:\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MediaInfo(type='image', width=163, height=117, duration=None, format='png_pipe', size=2212, suggest_ext='png')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "media_info = ffmpeg_media_type.detect('https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/overlay.png')\n", "media_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The detect function returns a model containing the following information:\n", "\n", "- type: The type of media file (e.g. video, audio, image, etc.).\n", "- duration: The duration of the media file in seconds.\n", "- width: The width of the media file in pixels.\n", "- height: The height of the media file in pixels.\n", "- format: The format of the media file (e.g. mp4, mp3, png, etc.).\n", "- size: The size of the media file in bytes.\n", "- suggest_ext: The suggested file extension for the media file (e.g. mp4, mp3, png, etc.).\n", "\n", "Here's an example of how to access these details:\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "duration = media_info.duration\n", "width = media_info.width\n", "height = media_info.height" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Duration: 5.312 seconds\n", "Width: 1280 pixels\n", "Height: 720 pixels\n" ] } ], "source": [ "import ffmpeg_media_type\n", "\n", "# Specify the path to the media file\n", "file_path = 'https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/SampleVideo_1280x720_1mb.mp4'\n", "\n", "# Detect media file information\n", "media_info = ffmpeg_media_type.detect(file_path)\n", "\n", "# Extract information from the media_info dictionary\n", "duration = media_info.duration\n", "width = media_info.width\n", "height = media_info.height\n", "\n", "# Print the extracted information\n", "print(f"Duration: {duration} seconds")\n", "print(f"Width: {width} pixels")\n", "print(f"Height: {height} pixels")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Enhancing Accuracy in Guessing Media File Extensions with FFmpeg\n", "\n", "- 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.\n", "- 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.\n", "\n", "check cache for details.\n", "\n", "### Access ffprobe output\n", "\n", "If you need to access the raw ffprobe output, you can use the ffprobe function:\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "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))))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ffprobe_output = ffmpeg_media_type.ffprobe('https://raw.githubusercontent.com/livingbio/ffmpeg-media-type/main/docs/media/SampleVideo_1280x720_1mb.mp4')\n", "\n", "ffprobe_output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contributing\n", "\n", "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.\n", "\n", "Before submitting a pull request, make sure to run the tests using the following command:\n", "\n", "bash\n", "poetry install --with dev\n", "py.test src\n", "\n", "\n", "Please ensure that your code follows the established coding style and passes all tests.\n", "\n", "## License\n", "\n", "This project is licensed under the MIT License. See the LICENSE file for more information." ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }