Code Monkey home page Code Monkey logo

python-ffmpeg's Introduction

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

python-ffmpeg's People

Contributors

jonghwanhyeon avatar codeindulgence avatar ayoy avatar jmarcet avatar golyalpha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.