Giter Site home page Giter Site logo

asyncio-subprocess-ffmpeg's Introduction

Python asyncio FFmpeg

Actions Status

Examples of Python asyncio.subprocess with FFmpeg and also traditional synchronous processes. We use src/ Python layout to demonstrate general good packaging practice, so to use the scripts:

pip install -e .

All examples are under the examples/ directory.

Coroutine vs Threads vs processes

For computationally bound programs, multiple processes is often a good choice. Try:

python examples/benchmark.py -h

FFprobe

Both synchronous (traditional for loop) and asynchronous pipeline are demonstrated. They call FFprobe executable to return JSON formatted metadata.

  • probe_sync.py: retrieve file metadata synchronously.
  • probe_coroutine.py: retrieve file metadata in asynchronous pipeline or list using Python asyncio coroutines.

FFplay

Testing asynchronous techniques with video playback makes some effects obvious. The FFplay asyncio example is more advanced than the FFprobe example. In the FFprobe example, the lazy asyncio generator produces metadata concurrently as fast as it's requested. There is no resource throttling in the FFprobe example, so the CPU could become overwhelmed with context switching.

The FFplay example in contrast is an example of a task using resource throttling via asyncio.Queue. The queuing could also be implemented for FFprobe style task if desired. However, the rationale employed is that the FFprobe task is overall lightweight, and thus other parts of the pipeline inherently limit resource utilization. If the FFprobe task was in an asyncio.gather() algorithm, resource utilization could get too high. Thus we have a "win-win" by using asyncio generator for FFprobe--the throttling comes implicitly from other parts of the pipeline.

Threading

Even though coroutines are more efficient in many applications, the syntax of concurrent.futures.ThreadPoolExecutor is perhaps the simplest possible way to spawn independent processes in a controlled fashion.

  • play_thread.py: use ThreadPoolExecutor without queue
  • play_thread_queue.py: use ThreadPoolExecutor with queue

Coroutine

  • play_coroutine.py: asyncio coroutine event loop to spawn processes.

asyncio-subprocess-ffmpeg's People

Contributors

scivision avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.