Getting Started with ffmpeg-python

Okay, here’s a comprehensive article on getting started with ffmpeg-python, aiming for around 5000 words. I’ve structured it to be a detailed guide, covering installation, basic usage, common operations, advanced techniques, troubleshooting, and best practices.


Getting Started with ffmpeg-python: A Comprehensive Guide

ffmpeg-python is a powerful Python library that provides a clean and Pythonic way to interact with the ubiquitous FFmpeg command-line tool. FFmpeg is a multimedia framework capable of handling virtually any audio, video, or streaming task you can imagine. While FFmpeg’s command-line interface is incredibly powerful, it can also be complex and cumbersome, especially for intricate processing pipelines. ffmpeg-python bridges this gap, allowing you to leverage FFmpeg’s capabilities within the familiar and flexible environment of Python.

This guide will provide a comprehensive introduction to ffmpeg-python, covering everything from installation to advanced usage. Whether you’re a beginner or an experienced developer looking to integrate multimedia processing into your Python projects, this article will equip you with the knowledge and skills you need.

1. Introduction to FFmpeg and ffmpeg-python

1.1 What is FFmpeg?

FFmpeg is a free and open-source project consisting of a vast suite of libraries and programs for handling video, audio, and other multimedia files and streams. It’s the Swiss Army knife of multimedia processing, used behind the scenes by countless applications, websites, and services. Here’s a glimpse of its capabilities:

  • Decoding and Encoding: Convert between virtually any audio and video format (e.g., MP4, AVI, MKV, MOV, WebM, MP3, AAC, FLAC).
  • Transcoding: Change the bitrate, resolution, frame rate, or other parameters of a video or audio stream.
  • Muxing and Demuxing: Combine multiple audio and video streams into a single container file (muxing), or separate them (demuxing).
  • Filtering: Apply a wide range of filters to modify audio and video, such as scaling, cropping, rotating, adding text overlays, adjusting brightness and contrast, and much more.
  • Streaming: Stream media over various protocols (e.g., HTTP, RTMP, RTSP).
  • Recording: Capture video and audio from devices (e.g., webcams, microphones).

FFmpeg is typically used through its command-line tools:

  • ffmpeg: The primary tool for encoding, transcoding, muxing, demuxing, filtering, and streaming.
  • ffprobe: A tool for analyzing media files and streams, providing detailed information about their format, codecs, and metadata.
  • ffplay: A simple media player built on the FFmpeg libraries.

While powerful, the command-line interface can become unwieldy for complex tasks. Constructing long command strings with numerous options and filters can be error-prone and difficult to read and maintain.

1.2 What is ffmpeg-python?

ffmpeg-python is a Python library that provides a fluent, Pythonic interface to FFmpeg. It allows you to build complex FFmpeg command-line arguments programmatically, avoiding the need to manually construct and parse long, intricate strings. This makes your code more readable, maintainable, and less prone to errors.

Key features of ffmpeg-python:

  • Fluent API: Uses a chainable, object-oriented style to build FFmpeg commands.
  • Automatic Command Generation: Handles the complexities of constructing the correct FFmpeg command-line syntax.
  • Error Handling: Provides mechanisms for catching and handling FFmpeg errors.
  • Input and Output Management: Simplifies the management of input files, output files, and pipes.
  • Filter Graph Support: Allows you to create complex filter graphs using a clear and concise syntax.
  • Integration with other Python Libraries: Works seamlessly with other Python libraries for tasks like image processing (e.g., Pillow), numerical computation (e.g., NumPy), and data analysis (e.g., Pandas).

1.3 Why Use ffmpeg-python?

Here’s why you should consider using ffmpeg-python:

  • Readability and Maintainability: The fluent API makes your code much easier to read and understand compared to raw FFmpeg command strings. This is crucial for complex processing pipelines.
  • Reduced Errors: ffmpeg-python handles the complexities of FFmpeg syntax, reducing the risk of typos and incorrect command construction.
  • Pythonic Integration: Integrates seamlessly with other Python code and libraries, allowing you to leverage the full power of the Python ecosystem.
  • Simplified Complex Operations: Makes complex tasks like building filter graphs much more manageable.
  • Improved Development Workflow: Faster development and easier debugging compared to manually constructing FFmpeg commands.

2. Installation

2.1 Installing FFmpeg

Before you can use ffmpeg-python, you need to have FFmpeg installed on your system. ffmpeg-python is a wrapper around the FFmpeg command-line tools, so it relies on them being present.

Windows:

  1. Download: Go to the official FFmpeg website (ffmpeg.org) or a reputable build provider like gyan.dev or BtbN. Download a pre-built “release” build (not the source code). Look for a “static” build for simplicity. A “shared” build will also work, but requires ensuring the DLLs are in your system’s PATH.
  2. Extract: Extract the downloaded archive (usually a .zip or .7z file) to a convenient location (e.g., C:\ffmpeg).
  3. Add to PATH (Important): You need to add the bin directory inside the extracted FFmpeg folder to your system’s PATH environment variable. This allows your operating system to find the ffmpeg.exe, ffprobe.exe, and ffplay.exe executables from any command prompt or terminal.
    • Open the Start Menu and search for “environment variables”.
    • Click “Edit the system environment variables”.
    • Click the “Environment Variables…” button.
    • Under “System variables”, find the Path variable, select it, and click “Edit…”.
    • Click “New” and add the full path to the bin directory (e.g., C:\ffmpeg\bin).
    • Click “OK” on all the open windows.
    • Important: You may need to restart your command prompt or terminal (or even your computer) for the changes to take effect.
  4. Verify: Open a new command prompt or terminal and type ffmpeg -version. If FFmpeg is installed correctly, you should see version information printed to the console. If you get a “command not found” error, double-check the PATH configuration.

macOS:

  1. Homebrew (Recommended): The easiest way to install FFmpeg on macOS is using Homebrew, a package manager. If you don’t have Homebrew, install it first by following the instructions on the Homebrew website (brew.sh). Once Homebrew is installed, open a terminal and run:

    bash
    brew install ffmpeg

    2. MacPorts (Alternative): Another package manager option is MacPorts. If you have Macports:

    bash
    sudo port install ffmpeg

  2. Manual Installation (Not Recommended): You can also download pre-built binaries from the FFmpeg website or compile from source, but this is generally more complex.

  3. Verify: Open a new terminal and type ffmpeg -version to verify the installation.

Linux (Debian/Ubuntu):

  1. Using apt: Most Debian-based distributions (including Ubuntu) have FFmpeg in their default repositories. Use the apt package manager:

    bash
    sudo apt update
    sudo apt install ffmpeg

Linux (Fedora/CentOS/RHEL):

  1. Enable RPM Fusion (if needed): FFmpeg may not be in the default repositories. You might need to enable the RPM Fusion repository first. Refer to the RPM Fusion documentation for your specific distribution.
  2. Using dnf (Fedora) or yum (CentOS/RHEL):

    bash
    sudo dnf install ffmpeg # Fedora
    sudo yum install ffmpeg # CentOS/RHEL

Linux (Other Distributions):

Consult your distribution’s documentation for the recommended way to install FFmpeg. It’s often available through the distribution’s package manager (e.g., pacman on Arch Linux, zypper on openSUSE).

Verify Installation (All Platforms): After installing FFmpeg, open a new terminal or command prompt and type:

bash
ffmpeg -version

You should see output similar to this (the exact version numbers will vary):

ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
configuration: --prefix=/usr --extra-version=0ubuntu0.22.04.1 --toolchain=hardened ...
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100

If you see this, FFmpeg is installed correctly. If you get an error like “ffmpeg: command not found,” then the installation failed, or the ffmpeg executable is not in your system’s PATH.

2.2 Installing ffmpeg-python

Once FFmpeg is installed, installing the ffmpeg-python library is straightforward using pip, the Python package installer:

bash
pip install ffmpeg-python

This command will download and install ffmpeg-python and its dependencies. It’s highly recommended to use a virtual environment to manage your Python project’s dependencies. This helps avoid conflicts between different projects that might require different versions of libraries.

Creating a Virtual Environment (Recommended):

“`bash

Create a virtual environment (replace ‘venv’ with your desired name)

python3 -m venv venv

Activate the virtual environment:

On Windows:

venv\Scripts\activate

On macOS/Linux:

source venv/bin/activate

Now install ffmpeg-python within the activated environment:

pip install ffmpeg-python
“`

After activating the virtual environment, any packages you install with pip will be installed within that environment, isolated from your system-wide Python installation.

Verifying ffmpeg-python Installation:

After installation, you can verify that ffmpeg-python is installed correctly by running a simple Python script:

“`python
import ffmpeg

try:
(
ffmpeg
.input(‘dummy.mp4’) # Use a dummy filename
.output(‘dummy_out.mp4’)
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print(f”FFmpeg error: {e.stderr.decode()}”)
# This will likely print an error because dummy.mp4 doesn’t exist.
# But the fact that it tried to run FFmpeg means ffmpeg-python is working.
else:
print(“ffmpeg-python seems to be working, but the input file was not found.”)
“`

This script attempts to run a very basic FFmpeg command (which will fail because dummy.mp4 doesn’t exist). The important part is that it demonstrates that ffmpeg-python can successfully import and attempt to execute FFmpeg. If you see an error related to FFmpeg itself (like “No such file or directory”), that’s expected. If you see an error like ModuleNotFoundError: No module named 'ffmpeg', then ffmpeg-python is not installed correctly.

3. Basic Usage

Now that you have FFmpeg and ffmpeg-python installed, let’s explore some basic usage examples.

3.1. Simple Video Conversion

Let’s start with a fundamental task: converting a video from one format to another. Suppose you have a video file named input.avi and you want to convert it to an MP4 file named output.mp4.

“`python
import ffmpeg

(
ffmpeg
.input(‘input.avi’)
.output(‘output.mp4’)
.run()
)
“`

This concise code does the following:

  1. ffmpeg.input('input.avi'): Specifies the input file (input.avi).
  2. .output('output.mp4'): Specifies the output file (output.mp4).
  3. .run(): Executes the FFmpeg command.

This is equivalent to the following FFmpeg command-line:

bash
ffmpeg -i input.avi output.mp4

ffmpeg-python automatically constructs the correct command-line arguments based on the chained method calls.

3.2. Specifying Codecs and Options

You can specify various options, such as codecs, bitrates, and resolutions, using keyword arguments in the output() method.

“`python
import ffmpeg

(
ffmpeg
.input(‘input.avi’)
.output(‘output.mp4′, vcodec=’libx264′, acodec=’aac’, video_bitrate=’1M’, audio_bitrate=’128k’)
.run()
)
“`

This example:

  • vcodec='libx264': Uses the H.264 video codec (libx264 is a popular H.264 encoder).
  • acodec='aac': Uses the AAC audio codec.
  • video_bitrate='1M': Sets the video bitrate to 1 megabit per second.
  • audio_bitrate='128k': Sets the audio bitrate to 128 kilobits per second.

This corresponds to the following FFmpeg command-line:

bash
ffmpeg -i input.avi -vcodec libx264 -acodec aac -b:v 1M -b:a 128k output.mp4

Notice how ffmpeg-python translates the keyword arguments into the appropriate FFmpeg command-line options. It automatically handles the -b:v (video bitrate) and -b:a (audio bitrate) prefixes.

3.3. Overwriting Output Files

By default, FFmpeg will not overwrite an existing output file. If output.mp4 already exists in the previous examples, FFmpeg will prompt you to confirm whether you want to overwrite it. To automatically overwrite output files, use the overwrite_output() method:

“`python
import ffmpeg

(
ffmpeg
.input(‘input.avi’)
.output(‘output.mp4′, vcodec=’libx264’)
.overwrite_output()
.run()
)
“`

This is equivalent to adding the -y option to the FFmpeg command-line:

bash
ffmpeg -y -i input.avi -vcodec libx264 output.mp4

3.4. Getting Media Information with ffprobe

ffmpeg-python also provides a wrapper for the ffprobe command, which allows you to retrieve detailed information about media files.

“`python
import ffmpeg

try:
probe = ffmpeg.probe(‘input.avi’)
video_stream = next((stream for stream in probe[‘streams’] if stream[‘codec_type’] == ‘video’), None)
audio_stream = next((stream for stream in probe[‘streams’] if stream[‘codec_type’] == ‘audio’), None)

if video_stream:
    print(f"Video Codec: {video_stream['codec_name']}")
    print(f"Width: {video_stream['width']}")
    print(f"Height: {video_stream['height']}")
    print(f"Duration: {video_stream['duration']} seconds")

if audio_stream:
  print(f"Audio Codec: {audio_stream['codec_name']}")

except ffmpeg.Error as e:
print(e.stderr.decode())

“`

This code:

  1. ffmpeg.probe('input.avi'): Runs ffprobe on the input file and returns a dictionary containing detailed information about the file.
  2. probe['streams']: Accesses the list of streams in the file (audio, video, subtitles, etc.).
  3. Iterates through each stream in probe['streams'], extracting information on video and audio streams if available.
  4. Prints relevant information such as codec_name, width, height, duration

The probe variable will contain a nested dictionary structure with information about the file’s format, streams, and metadata. You can explore the contents of this dictionary to access specific information you need. The next function along with a generator expression is an efficient way to find the first video and audio stream, or return None if not found.

3.5. Error Handling

It’s crucial to handle potential errors that might occur during FFmpeg processing. ffmpeg-python provides the ffmpeg.Error exception, which you can catch to handle errors gracefully.

“`python
import ffmpeg

try:
(
ffmpeg
.input(‘input.avi’)
.output(‘output.mp4’)
.run()
)
except ffmpeg.Error as e:
print(f”FFmpeg error: {e.stderr.decode()}”)
# You can also access the standard output: e.stdout.decode()
“`

This code uses a try...except block to catch the ffmpeg.Error exception. If FFmpeg encounters an error, the except block will be executed, and you can access the error message from e.stderr (standard error). e.stdout (standard output) might also contain useful information, even in case of an error. Decoding with .decode() is essential to convert the byte output from FFmpeg into a readable string.

3.6. Capturing Standard Output and Standard Error

Sometimes, you might want to capture the standard output (stdout) and standard error (stderr) of the FFmpeg command, even if it doesn’t result in an error. You can do this using the capture_stdout and capture_stderr arguments to the run() method:

“`python
import ffmpeg

try:
out, err = (
ffmpeg
.input(‘input.avi’)
.output(‘output.mp4’)
.run(capture_stdout=True, capture_stderr=True)
)
print(“Standard Output:”)
print(out.decode())
print(“\nStandard Error:”)
print(err.decode())

except ffmpeg.Error as e:
print(f”FFmpeg error: {e.stderr.decode()}”)
``
This example runs the command, storing the standard output to
out, standard error toerr, and prints the decoded versions to the console.
By setting
capture_stdout=Trueandcapture_stderr=True, therun()method returns a tuple containing the standard output and standard error as byte strings. You can then decode them using.decode()` to get human-readable strings.

4. Common Operations

Let’s explore some more common video and audio processing tasks using ffmpeg-python.

4.1. Extracting Audio from Video

To extract the audio from a video file and save it as a separate audio file (e.g., MP3):

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4’)
.output(‘output.mp3′, acodec=’libmp3lame’, ab=’192k’) # Specify audio codec and bitrate
.run()
)
“`

This extracts the audio and encodes it as an MP3 file using the libmp3lame encoder with a bitrate of 192kbps.

4.2. Cropping a Video

To crop a video to a specific region:

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4’)
.filter(‘crop’, 800, 600, 100, 50) # crop=width:height:x:y
.output(‘output_cropped.mp4’)
.run()
)
“`

This uses the crop filter:

  • 800: Width of the cropped region.
  • 600: Height of the cropped region.
  • 100: X-coordinate of the top-left corner of the cropped region.
  • 50: Y-coordinate of the top-left corner of the cropped region.

4.3. Resizing (Scaling) a Video

To resize a video to a different resolution:

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4’)
.filter(‘scale’, 1280, 720) # scale=width:height
.output(‘output_resized.mp4’)
.run()
)
“`

This uses the scale filter to resize the video to 1280×720 pixels. You can also specify only one dimension (width or height) and use -1 for the other to automatically calculate the corresponding dimension while maintaining the aspect ratio:

“`python

Resize to a width of 640, keeping the aspect ratio

(
ffmpeg
.input(‘input.mp4’)
.filter(‘scale’, 640, -1)
.output(‘output_resized.mp4’)
.run()
)
“`

4.4. Concatenating Videos

To concatenate (join) multiple video files together:

There are two main methods for concatenating videos with FFmpeg:

  • Concat demuxer (for videos with the same codecs): This is the faster and preferred method when all input videos have the exact same codecs, resolution, frame rate, and other parameters.
  • Complex filtergraph (for videos with different codecs): This method is more flexible and can handle videos with different codecs, but it re-encodes the entire output, which is slower.

Concat Demuxer (Same Codecs):

“`python
import ffmpeg
import os

List of input files (must have the same codecs)

input_files = [‘input1.mp4’, ‘input2.mp4’, ‘input3.mp4’]

1. Create a text file listing the input files

with open(‘mylist.txt’, ‘w’) as f:
for file in input_files:
f.write(f”file ‘{os.path.abspath(file)}’\n”) # Use absolute paths

2. Use the concat demuxer

(
ffmpeg
.input(‘mylist.txt’, format=’concat’, safe=0)
.output(‘output_concat.mp4′, c=’copy’) # copy stream, no re-encoding
.run()
)
“`

Explanation:

  1. Create mylist.txt: This text file tells FFmpeg which files to concatenate, and in what order. Each line should be in the format file 'path/to/file.mp4'. Using os.path.abspath is crucially important on Windows. The concat demuxer is very sensitive to the file paths, and relative paths often cause problems. Absolute paths are much more reliable. The safe=0 is required when using absolute paths.
  2. ffmpeg.input('mylist.txt', format='concat', safe=0): This tells ffmpeg-python to use the concat demuxer. The format='concat' argument is essential.
  3. c='copy': This is the most important part for performance. It tells FFmpeg to copy the video and audio streams without re-encoding them. This is much faster than re-encoding, and it preserves the original quality. This only works if all input files have the same codecs.

Complex Filtergraph (Different Codecs):

“`python
import ffmpeg

List of input files (can have different codecs)

input_files = [‘input1.mp4’, ‘input2.avi’, ‘input3.webm’]

Create input streams

inputs = []
for file in input_files:
inputs.append(ffmpeg.input(file))

Concatenate the streams

(
ffmpeg
.concat(*inputs, v=1, a=1) # v=1: one video stream, a=1: one audio stream
.output(‘output_concat.mp4′, vcodec=’libx264′, acodec=’aac’) # Re-encode
.run()
)

“`

Explanation:

  1. Create input streams: We create a list of ffmpeg.input() objects, one for each input file.
  2. ffmpeg.concat(*inputs, v=1, a=1): This uses the concat filter (not the demuxer). The *inputs unpacks the list of input streams into separate arguments. v=1 and a=1 specify that we want to combine all video streams into one output video stream, and all audio streams into one output audio stream.
  3. vcodec='libx264', acodec='aac': Since the input files might have different codecs, we must re-encode the output. We specify the desired output codecs (H.264 for video, AAC for audio). This method is significantly slower than the concat demuxer method because of the re-encoding.

Choosing the Right Method:

  • Always try the concat demuxer first. If it works (no errors and the output is correct), it’s the best option.
  • If the concat demuxer fails or produces a corrupted output, use the complex filtergraph method. This is the fallback for when the input files have incompatible codecs.

4.5. Adding a Watermark (Overlay Image)

To add a watermark (an image overlay) to a video:

“`python
import ffmpeg

main = ffmpeg.input(‘input.mp4’)
overlay = ffmpeg.input(‘watermark.png’)

(
ffmpeg
.overlay(main, overlay, x=10, y=10) # Overlay at position (10, 10)
.output(‘output_watermarked.mp4’)
.run()
)
“`

This example overlays the watermark.png image onto the input.mp4 video at position (10, 10) (top-left corner coordinates).

You can also control the overlay’s position relative to the video’s edges:

“`python

Overlay in the bottom-right corner, with a 10-pixel margin

(
ffmpeg
.overlay(main, overlay, x=’main_w-overlay_w-10′, y=’main_h-overlay_h-10′)
.output(‘output_watermarked.mp4’)
.run()
)
“`

main_w, main_h, overlay_w, and overlay_h are special variables representing the width and height of the main video and the overlay image, respectively.

4.6. Adding Text Overlay

To add text to a video:

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4′)
.drawtext(text=’My Video’, x=50, y=50,
fontfile=’/path/to/font.ttf’, fontsize=24, fontcolor=’white’)
.output(‘output_text.mp4’)
.run()
)
“`

This uses the drawtext filter. Key parameters:

  • text: The text to display.
  • x, y: The position of the text (top-left corner).
  • fontfile: The path to a TrueType or OpenType font file (e.g., .ttf, .otf). This is often required; FFmpeg might not find system fonts automatically.
  • fontsize: The font size.
  • fontcolor: The font color (e.g., white, black, red, #FF0000).

You can also add a box around the text:

python
.drawtext(..., box=1, boxcolor='[email protected]', boxborderw=5)

  • box=1: Enables the box.
  • boxcolor='[email protected]': Sets the box color to black with 50% opacity.
  • boxborderw=5: Sets the box border width to 5 pixels.

4.7. Creating a GIF from a Video

To convert a video to an animated GIF:

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4’)
.output(‘output.gif’, vf=’fps=10,scale=320:-1:flags=lanczos’, **{‘loop’: 0})
#vf allows multiple filters
.run()
)

“`

Explanation of arguments:

  • vf='fps=10,scale=320:-1:flags=lanczos': This is a filtergraph specified as a single string. It combines two filters:
    • fps=10: Sets the frame rate of the GIF to 10 frames per second.
    • scale=320:-1:flags=lanczos: Resizes the video to a width of 320 pixels, automatically calculating the height to maintain the aspect ratio. lanczos is a high-quality scaling algorithm.
  • **{'loop': 0}: This sets the loop to zero, which plays the GIF indefinitely. Setting it to -1 would cause the GIF to not play. Setting the value to 1 would play the animation twice.

This is a basic GIF conversion. For better quality and smaller file sizes, you can use the palettegen and paletteuse filters, which create a custom color palette for the GIF:

“`python
import ffmpeg

Create a palette

palette = (
ffmpeg
.input(‘input.mp4’)
.filter(‘fps’, 10)
.filter(‘scale’, 320, -1)
.filter(‘palettegen’)
.output(‘palette.png’)
)

Use the palette to create the GIF

(
ffmpeg
.input(‘input.mp4’)
.input(‘palette.png’)
.filter(‘fps’, 10)
.filter(‘scale’, 320, -1)
.filter(‘paletteuse’)
.output(‘output_palette.gif’, **{‘loop’: 0})
.run()
)
“`

This two-pass approach generally produces much better results for GIFs.

4.8. Extracting a Thumbnail Image

To extract a single frame from a video and save it as an image (e.g., a thumbnail):

“`python
import ffmpeg

(
ffmpeg
.input(‘input.mp4’, ss=2) # Seek to 2 seconds
.output(‘thumbnail.jpg’, vframes=1) # Capture one frame
.run()
)
“`

  • ss=2: Seeks to the 2-second mark in the video. This is much faster than decoding the entire video up to that point. You can use time in seconds (e.g., 2.5) or in HH:MM:SS.ms format (e.g., 00:00:02.500).
  • vframes=1: Specifies that only one video frame should be captured.

4.9. Speeding Up or Slowing Down Video

To change the playback speed of a video:

“`python
import ffmpeg

Speed up the video by 2x

(
ffmpeg
.input(‘input.mp4’)
.filter(‘setpts’, ‘0.5*PTS’) # PTS = Presentation Time Stamp
.output(‘output_fast.mp4’)
.run()
)

Slow down the video by 0.5x (half speed)

(
ffmpeg
.input(‘input.mp4’)
.filter(‘setpts’, ‘2*PTS’)
.output(‘output_slow.mp4’)
.run()
)
“`

The setpts filter modifies the presentation timestamps (PTS) of the video frames. 0.5*PTS makes the video play twice as fast, and 2*PTS makes it play at half speed. You can use any factor you want.

To also adjust the audio pitch accordingly (otherwise the audio will sound distorted), you can use the atempo filter for audio:

“`python

Speed up video and audio by 2x

(
ffmpeg
.input(‘input.mp4’)
.filter(‘setpts’, ‘0.5*PTS’)
.filter(‘atempo’, 2.0)
.output(‘output_fast.mp4’)
.run()
)

Slow down video by 0.5x and audio by 0.5

(
ffmpeg
.input(‘input.mp4’)
.filter(‘setpts’, ‘2*PTS’)
.filter(‘atempo’, 0.5)
.output(‘output_slow.mp4’)
.run()
)
“`

4.10. Rotating a Video

To rotate a video:

“`python
import ffmpeg

Rotate 90 degrees clockwise

(
ffmpeg
.input(‘input.mp4’)
.filter(‘transpose’, 1) # 1 = 90 degrees clockwise
.output(‘output_rotated.mp4’)
.run()
)
“`

The transpose filter takes the following values:

  • 0: Rotate 90 degrees counterclockwise and vertically flip.
  • 1: Rotate 90 degrees clockwise.
  • 2: Rotate 90 degrees counterclockwise.
  • 3: Rotate 90 degrees clockwise and vertically flip.

5. Advanced Techniques

5.1. Working with Filter Graphs

ffmpeg-python excels at creating complex filter graphs. A filter graph is a chain of filters applied to one or more input streams, producing one or more output streams.

Here’s a more complex example that combines several filters:

“`python
import ffmpeg

in_file = ffmpeg.input(‘input.mp4’)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top