FFmpeg Python Bindings

0.2.0 · active · verified Mon Apr 06

ffmpeg-python is a Python wrapper for the powerful FFmpeg multimedia framework, enabling developers to programmatically build and execute FFmpeg command-line arguments. It simplifies complex media manipulation tasks such as format conversion, video editing, and audio extraction within Python applications. The library's current PyPI version is 0.2.0, and while its own releases are infrequent, it remains actively used and functional due to its reliance on the externally maintained FFmpeg binary. Users should ensure their underlying FFmpeg installation is up-to-date.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert a video file from one format (e.g., MP4) to another (e.g., AVI) using `ffmpeg-python`. It creates a stream from an input file, specifies an output file, and then executes the FFmpeg command. For this example to function correctly, an `input.mp4` file must exist, and the FFmpeg binary must be installed and accessible on your system.

import ffmpeg
import os

# Create dummy input file for demonstration
# In a real scenario, 'input.mp4' would be an existing video file.
# For a runnable example without actual FFmpeg, this part would be mocked or use a very small, pre-existing file.
# For this example, we assume 'input.mp4' exists.
# To make it runnable for demonstration, let's create a placeholder.
# WARNING: This part is for demonstration. FFmpeg needs a valid input file.
# In a production environment, ensure 'input.mp4' is a real video file.

input_filename = 'input.mp4'
output_filename = 'output.avi'

# This block is a placeholder to ensure the file exists for a 'runnable' check, 
# but FFmpeg still requires a proper media file.
if not os.path.exists(input_filename):
    print(f"Please create a dummy video file named '{input_filename}' for this example to fully run with FFmpeg.")
    print("Example will proceed by trying to convert, but might fail if FFmpeg can't find/process the file.")
    # Example of how you might generate a minimal dummy file if FFmpeg was installed and functional:
    # import subprocess
    # try:
    #     subprocess.run(['ffmpeg', '-f', 'lavfi', '-i', 'testsrc=duration=1:size=128x72:rate=10', input_filename], check=True)
    # except FileNotFoundError:
    #     print("FFmpeg command-line tool not found. Please install FFmpeg.")
    # except subprocess.CalledProcessError as e:
    #     print(f"Error creating dummy input file: {e}")



try:
    stream = ffmpeg.input(input_filename)
    stream = ffmpeg.output(stream, output_filename)
    ffmpeg.run(stream, overwrite_output=True)
    print(f"Successfully converted {input_filename} to {output_filename}")
except ffmpeg.Error as e:
    print(f"FFmpeg error: {e.stderr.decode('utf8')}")
except FileNotFoundError:
    print("FFmpeg command not found. Please ensure FFmpeg is installed and in your system's PATH.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →