Skip to main content

python-ffmpeg

A python interface for FFmpeg using asyncio

Requirements

  • Python 3.7+
  • pyee

Installation

pip install python-ffmpeg

Usage

import asyncio

from ffmpeg import FFmpeg


async def main():
    ffmpeg = (
        FFmpeg()
        .option("y")
        .input(
            "rtsp://example.com/cam",
            # Specify file options using kwargs
            rtsp_transport="tcp",
            rtsp_flags="prefer_tcp",
        )
        .output(
            "output.ts",
            # Use a dictionary when an option name contains special characters
            {"codec:v": "copy"},
            f="mpegts",
        )
    )

    @ffmpeg.on("start")
    def on_start(arguments):
        print("Arguments:", arguments)

    @ffmpeg.on("stderr")
    def on_stderr(line):
        print("stderr:", line)

    @ffmpeg.on("progress")
    def on_progress(progress):
        print(progress)

    @ffmpeg.on("progress")
    def time_to_terminate(progress):
        # Gracefully terminate when more than 200 frames are processed
        if progress.frame > 200:
            ffmpeg.terminate()

    @ffmpeg.on("completed")
    def on_completed():
        print("Completed")

    @ffmpeg.on("terminated")
    def on_terminated():
        print("Terminated")

    @ffmpeg.on("error")
    def on_error(code):
        print("Error:", code)

    await ffmpeg.execute()


if __name__ == "__main__":
    asyncio.run(main())

API

FFmpeg

init(executable='ffmpeg')

  • executable: the path to the ffmpeg executable

Initializes the FFmpeg instance.

option(key, value=None)

  • key
  • value

Specifies a global option -key or -key value

input(url, options=None, **kwargs)

  • url
  • options
  • kwargs

Specifies an input file. An arbitrary number of input files can be specified by calling this method multiple times.

output(url, options=None, **kwargs)

  • url
  • options
  • kwargs

Specifies an output file. An arbitrary number of output files can be specified by calling this method multiple times.

execute()

Executes FFmpeg using specified options and files.

terminate()

Gracefully terminates the running FFmpeg process.

on(event, listener=None)

  • event: the name of the event
  • listener: the callback function

Registers the listener to the event. This method can be used as a decorator.

Event: 'start'

  • arguments: a sequence of arguments to execute FFmpeg

The 'start' event is emitted just before FFmpeg is executed.

Event: 'stderr'

  • line

The 'stderr' event is emitted when FFmpeg writes a line to stderr.

Event: 'progress'

  • progress: a namedtuple with frame, fps, size, time, bitrate, speed fields

The 'progress' event is emitted when FFmpeg reports progress.

Event: 'completed'

The 'completed' event is emitted when FFmpeg is successfully exited.

Event: 'terminated'

The 'terminated' event is emitted when FFmpeg is terminated by calling FFmpeg.terminate().

Event: 'error'

  • code: a return code of the FFmpeg process

The 'error' event is emitted when FFmpeg is exited with a non-zero return code

Release files for python-ffmpeg 1.0.16

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

Source distribution (sdist)

Source distribution for python-ffmpeg 1.0.16
File Size Uploaded
python-ffmpeg-1.0.16.tar.gz 5.5 kB Details

Built distribution (wheel)

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

Total release size: 11.8 kB

Release files / python-ffmpeg-1.0.16.tar.gz

Download URL python-ffmpeg-1.0.16.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c6ca40a0b8e807b4a65384cf2d4e920b25a93f19c8f2f3d127e6d3449e236631
BLAKE2b-256 checksum
How to use checksums
bb647643c72064919125d1474ac96fa0a49746e944ca9832a193caade283f029
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / python_ffmpeg-1.0.16-py3-none-any.whl

Download URL python_ffmpeg-1.0.16-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
466165ed39ecf461d8d7c201fc46e6ab1c44c7a9bb35466740c6d834f694f8e4
BLAKE2b-256 checksum
How to use checksums
e569ec902a2d4ba07c4245b0eeddda52dcd1859799a7c6d6b6dc861733ccc025
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

2.0.12

2 release files

2.0.11

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

This release

1.0.16 This release

2 release files

1.0.15

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

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

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