Skip to main content

FFMPEG Python Helper

A Python wrapper for FFMPEG that provides a simple, intuitive API for common video processing tasks.

Features

  • 🔧 Easy FFMPEG Integration - Automatically detects FFMPEG installation
  • 🎥 Video Processing - Reformat videos between formats
  • 🎞️ GIF Creation - Convert videos to optimized GIFs with customizable settings
  • 🧠 In-Memory Processing - Process video data directly from bytes without temporary files
  • ✂️ Video Trimming - Trim videos with precise start time and duration control
  • 🐍 Pythonic API - Clean, object-oriented interface with proper error handling
  • 📁 File Validation - Automatic input file existence checking

Installation

Prerequisites

  • Python 3.14 or higher
  • FFMPEG installed and available in your system PATH

Install FFMPEG Python Helper

pip install ffmpeg-python-helper

Or install from source:

git clone https://github.com/yourusername/ffmpeg-python-helper.git
cd ffmpeg-python-helper
pip install -e .

Quick Start

from ffmpeg_python_helper import FFMPEG

# Initialize the FFMPEG wrapper
ffmpeg = FFMPEG()

# Check if FFMPEG is available
print(f"FFMPEG executable found at: {ffmpeg.executable}")

# Convert a video file
output = ffmpeg.reformat("input.mp4", "output.avi")
print(output.decode())

# Create a GIF from video
output = ffmpeg.gif("video.mp4", "animation.gif", fps=15, scale=480)
print(output.decode())

# Trim a video
output = ffmpeg.trim("video.mp4", "short_clip.mp4", start=10.5, duration=5.0)
print(output.decode())

# Create GIF from in-memory video data
with open("video.mp4", "rb") as f:
    video_data = f.read()
gif_data = ffmpeg.gifs(video_data, fps=15, scale=480)
with open("memory.gif", "wb") as f:
    f.write(gif_data)

API Reference

FFMPEG Class

The main class that wraps FFMPEG functionality.

Constructor

FFMPEG()

Creates a new FFMPEG instance. Automatically searches for FFMPEG in the system PATH.

  • Raises: FileNotFoundError if FFMPEG is not found in PATH

Properties

  • executable (str): The path to the FFMPEG executable found in the system

Class Methods

@classmethod
def api(cls) -> "FFMPEG"

Factory method that returns a new FFMPEG instance.

  • Returns: FFMPEG instance

Instance Methods

execute(*args: str, input_data: bytes | None = None) -> tuple[bytes, bytes]

Execute raw FFMPEG commands with the given arguments.

Parameters:

  • *args (str): FFMPEG command-line arguments
  • input_data (bytes | None, optional): Optional bytes to send to FFMPEG's stdin

Returns:

  • tuple[bytes, bytes]: A tuple containing (stdout, stderr) as bytes

Raises:

  • FileNotFoundError: If FFMPEG executable is not found
  • RuntimeError: If FFMPEG command returns a non-zero exit code

Example:

stdout, stderr = ffmpeg.execute("-version")
print(stdout.decode())

# Process data from memory
video_data = b"...video bytes..."
stdout, stderr = ffmpeg.execute("-i", "pipe:0", "-f", "null", "-", input_data=video_data)
reformat(input_file: str, output_file: str) -> bytes

Convert a video file from one format to another.

Parameters:

  • input_file (str): Path to the input video file
  • output_file (str): Path for the output video file

Returns:

  • bytes: FFMPEG output (stdout or stderr) as bytes

Raises:

  • FileNotFoundError: If input file doesn't exist

Example:

output = ffmpeg.reformat("input.mov", "output.mp4")
print(output.decode())
gif(input_file: str, output_file: str, fps: int = 10, scale: int = 320) -> bytes

Convert a video file to an optimized GIF.

Parameters:

  • input_file (str): Path to the input video file
  • output_file (str): Path for the output GIF file
  • fps (int, optional): Frames per second for the GIF (default: 10)
  • scale (int, optional): Width of the GIF in pixels, height is auto-scaled (default: 320)

Returns:

  • bytes: FFMPEG output (stdout or stderr) as bytes

Raises:

  • FileNotFoundError: If input file doesn't exist
  • RuntimeError: If GIF file was not created successfully

Example:

output = ffmpeg.gif("video.mp4", "output.gif", fps=15, scale=640)
print(output.decode())
gifs(input_byte: bytes, fps: int = 10, scale: int = 320) -> bytes

Convert video data from bytes to an optimized GIF (in-memory processing).

Parameters:

  • input_byte (bytes): Video data as bytes to convert to GIF
  • fps (int, optional): Frames per second for the GIF (default: 10)
  • scale (int, optional): Width of the GIF in pixels, height is auto-scaled (default: 320)

Returns:

  • bytes: The generated GIF data as bytes

Raises:

  • RuntimeError: If GIF conversion fails

Example:

# Read video data from a file
with open("video.mp4", "rb") as f:
    video_data = f.read()

# Convert to GIF in memory
gif_data = ffmpeg.gifs(video_data, fps=15, scale=480)

# Save the GIF
with open("output.gif", "wb") as f:
    f.write(gif_data)
trim(input_file: str, output_file: str, start: float = 0, duration: float | None = None) -> bytes

Trim a video file.

Parameters:

  • input_file (str): Path to the input video file
  • output_file (str): Path for the output trimmed video
  • start (float, optional): Start time in seconds (default: 0)
  • duration (float | None, optional): Duration in seconds, or None for remaining video (default: None)

Returns:

  • bytes: FFMPEG output (stdout or stderr) as bytes

Raises:

  • FileNotFoundError: If input file doesn't exist
  • ValueError: If start is negative or duration is non-positive
  • RuntimeError: If trimmed video was not created successfully

Example:

# Trim from 5 seconds to 10 seconds (5-second clip)
output = ffmpeg.trim("video.mp4", "clip.mp4", start=5, duration=5)
print(output.decode())

# Trim from 10 seconds to the end of video
output = ffmpeg.trim("video.mp4", "ending.mp4", start=10)
print(output.decode())

Advanced Usage

Custom FFMPEG Commands

For operations not covered by the built-in methods, use the execute method:

# Extract audio from video
ffmpeg.execute("-i", "video.mp4", "-q:a", "0", "-map", "a", "audio.mp3")

# Add watermark to video
ffmpeg.execute("-i", "video.mp4", "-i", "watermark.png", 
               "-filter_complex", "overlay=10:10", "output.mp4")

# Change video bitrate
ffmpeg.execute("-i", "input.mp4", "-b:v", "1M", "output.mp4")

Error Handling

from ffmpeg_python_helper import FFMPEG
import sys

try:
    ffmpeg = FFMPEG()
    ffmpeg.gif("video.mp4", "output.gif")
except FileNotFoundError as e:
    print(f"FFMPEG not found: {e}", file=sys.stderr)
    sys.exit(1)
except RuntimeError as e:
    print(f"Processing failed: {e}", file=sys.stderr)
    sys.exit(1)

Common Use Cases

Batch Processing

import os
from ffmpeg_python_helper import FFMPEG

ffmpeg = FFMPEG()
videos = ["video1.mp4", "video2.mp4", "video3.mp4"]

for video in videos:
    if os.path.exists(video):
        base_name = os.path.splitext(video)[0]
        ffmpeg.gif(video, f"{base_name}.gif", fps=12, scale=400)

Video Compilation

from ffmpeg_python_helper import FFMPEG

ffmpeg = FFMPEG()

# Trim interesting parts
ffmpeg.trim("concert.mp4", "intro.mp4", start=0, duration=30)
ffmpeg.trim("concert.mp4", "chorus.mp4", start=120, duration=45)
ffmpeg.trim("concert.mp4", "finale.mp4", start=300, duration=60)

# Later, use FFMPEG to concatenate trimmed parts
ffmpeg.execute("-f", "concat", "-safe", "0", "-i", "parts.txt", "highlight_reel.mp4")

Troubleshooting

FFMPEG Not Found

If you get FileNotFoundError when creating an FFMPEG instance:

  1. Install FFMPEG:

    • Windows: Download from ffmpeg.org
    • macOS: brew install ffmpeg
    • Linux: sudo apt install ffmpeg (Ubuntu/Debian) or sudo yum install ffmpeg (Fedora/RHEL)
  2. Add to PATH:

    • Ensure FFMPEG is in your system PATH
    • Test with ffmpeg -version in your terminal

File Not Found Errors

  • Ensure input file paths are correct and files exist
  • Use absolute paths if working with files in different directories
  • Check file permissions

GIF Creation Issues

  • Lower FPS or scale if GIF file is too large
  • Ensure input video has sufficient quality
  • Check available disk space

Development

Running Tests

python -m pytest tests/

Building Documentation

# Install documentation dependencies
pip install pdoc3

# Generate API documentation
pdoc --html ffmpeg_python_helper --output-dir docs

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details

Support

Acknowledgments

  • FFMPEG team for the amazing multimedia framework
  • Python community for excellent tooling and libraries

Metadata

Release files for ffmpeg-python-helper 2.0.0

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-python-helper 2.0.0
File Size Uploaded
ffmpeg_python_helper-2.0.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ffmpeg-python-helper 2.0.0
File Interpreter ABI Platform
ffmpeg_python_helper-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / ffmpeg_python_helper-2.0.0.tar.gz

Download URL ffmpeg_python_helper-2.0.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
af4af172954fa3c05428c51642bb78961d5c8f63de8cc617b9053b59d2358bf8
BLAKE2b-256 checksum
How to use checksums
6606dee97727083419210f88170f7ed899e549c4aef56eb27ed8128ebb515153
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ffmpeg_python_helper-2.0.0-py3-none-any.whl

Download URL ffmpeg_python_helper-2.0.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b342b1667d3a235cda85b3a935947bfc8e481e5dcde999a1e31ff4b38ddb51d7
BLAKE2b-256 checksum
How to use checksums
1fb6bac290803834bd15fc27564c7f0e5ff7f39d52bcc6c101eaeb1f67f761ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

4.1.0

2 release files

4.0.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

This release

2.0.0 This release

2 release files

0.1.0

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