A python interface for FFmpeg using asyncio
- Python 3.5+
- pyee
pip install python-ffmpeg
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)
loop = asyncio.get_event_loop()
loop.run_until_complete(ffmpeg.execute())
loop.close()
executable
: the path to the ffmpeg executable
Initializes the FFmpeg
instance.
key
value
Specifies a global option -key
or -key value
url
options
kwargs
Specifies an input file. An arbitrary number of input files can be specified by calling this method multiple times.
url
options
kwargs
Specifies an output file. An arbitrary number of output files can be specified by calling this method multiple times.
Executes FFmpeg using specified options and files.
Gracefully terminates the running FFmpeg process.
event
: the name of the eventlistener
: the callback function
Registers the listener
to the event
. This method can be used as a decorator.
arguments
: a sequence of arguments to execute FFmpeg
The 'start'
event is emitted just before FFmpeg is executed.
line
The 'stderr'
event is emitted when FFmpeg writes a line to stderr
.
progress
: a namedtuple withframe
,fps
,size
,time
,bitrate
,speed
fields
The 'progress'
event is emitted when FFmpeg reports progress.
The 'completed'
event is emitted when FFmpeg is successfully exited.
The 'terminated'
event is emitted when FFmpeg is terminated by calling FFmpeg.terminate()
.
code
: a return code of the FFmpeg process
The 'error'
event is emitted when FFmpeg is exited with a non-zero return code