Skip to main content

A python interface for FFmpeg using asyncio

Project description

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

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)

asyncio.run(ffmpeg.execute())

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-ffmpeg-1.0.15.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

python_ffmpeg-1.0.15-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file python-ffmpeg-1.0.15.tar.gz.

File metadata

  • Download URL: python-ffmpeg-1.0.15.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for python-ffmpeg-1.0.15.tar.gz
Algorithm Hash digest
SHA256 72afea2459c47b83167f5e7ca83f333285ca141a0e984d06f0adc11c9aee33db
MD5 6aebbd7e7988df96cff3d4a1614ecd5f
BLAKE2b-256 8342facbbbd5713e45c80e24f7398c39c23ab14a527d0a9534f0aea65836a6e6

See more details on using hashes here.

File details

Details for the file python_ffmpeg-1.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for python_ffmpeg-1.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 eed9891c5de7300c4b1e49e4d7cd0405577da3136d685b5033e5bb3f1b9c9573
MD5 d178bda7b2b01bcc4f6a7202819a617b
BLAKE2b-256 1c475c9aafbb1d5a746e4097af340b626f3751a9c442c6c4a5edfa46349d4563

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page