Supports async / await pattern for FFmpeg operations.
Project description
Asynchronous FFmpeg
Supports async / await pattern for FFmpeg operations.
Advantage
- Support async / await pattern for FFmpeg operations
- Support Ctrl + C
1. Support async / await pattern for FFmpeg operations
This package supports FFmpeg asynchronously invoke with async / await pattern
wrapping ffmpeg.run_async()
of ffmpeg-python and returned subprocess.Popen
.
The async / await syntax makes asynchronous code as:
- Simple
- Readable
2. Support Ctrl + C
User can stop FFmpeg process gracefully by Ctrl + C.
This works as same as sending q
key to running FFmpeg.
This action is guaranteed by pytest.
Quickstart
1. Install
pip install asyncffmpeg
2. Implement
asyncffmpeg.FFmpegCoroutine
class has asynchronous method: execute()
.
To run concurrently, it requires not multi threading but multi processing
since FFmpeg process is CPU-bound operation.
The package asynccpu
is helpful to simple implement.
Ex:
import ffmpeg
from asynccpu import ProcessTaskPoolExecutor
from asyncffmpeg import FFmpegCoroutineFactory
async def create_stream_spec_copy():
stream = ffmpeg.input("input.mp4")
return ffmpeg.output(stream, "output.mp4", c="copy")
async def create_stream_spec_filter():
stream = ffmpeg.input("input.mp4")
stream = ffmpeg.filter(stream, "scale", 768, -1)
return ffmpeg.output(stream, "output.mp4")
ffmpeg_coroutine = FFmpegCoroutineFactory.create()
with ProcessTaskPoolExecutor(max_workers=3, cancel_tasks_when_shutdown=True) as executor:
awaitables = {
executor.create_process_task(ffmpeg_coroutine, create_stream_spec)
for create_stream_spec in [create_stream_spec_copy, create_stream_spec_filter]
}
await asyncio.gather(*awaitables)
Why not asyncio
but asynccpu
?
Unfortunately High-level APIs of asyncio
doesn't support CPU-bound operations
since it works based on not ProcessPoolExecutor
but ThreadPoolExecutor
.
When we want to run CPU-bound operations concurrently with asyncio
,
we need to use Low-level APIs which need finer control over the event loop behavior.
Note
The argument of Coroutine
requires not "raw Coroutine
object" but "Coroutine
function"
since raw Coroutine
object is not picklable.
This specification is depend on the one of Python multiprocessing
package:
multiprocessing — Process-based parallelism
Note When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe.
See: Answer: Python multiprocessing PicklingError: Can't pickle <type 'function'> - Stack Overflow
API
FFmpegCoroutineFactory
class FFmpegCoroutineFactory:
@staticmethod
def create(
*,
time_to_force_termination: int = 8
) -> FFmpegCoroutine:
time_to_force_termination: int = 8
The time limit (second) to wait stopping FFmpeg process gracefully
when send Ctrl + C.
At first, subprocess will try to send q
key to FFmpeg process.
In case when FFmpeg process doesn't stop gracefully by time limit,
subprocess will terminate process.
FFmpegCoroutine
class FFmpegCoroutine:
async def execute(
self,
create_stream_spec: Callable[[], Awaitable[StreamSpec]],
*,
after_start: Optional[Callable[[FFmpegProcess], Awaitable]] = None
) -> None:
create_stream_spec: Callable[[], Awaitable[StreamSpec]]
Coroutine
function to create stream spec for FFmpeg process.
Created stream spec will be set the first argument of ffmpeg.run_async()
of ffmpeg-python inside of FFmpegCoroutine
.
stream spec is a Stream, list of Streams, or label-to-Stream dictionary mapping
in ffmpeg-python.
after_start: Optional[Callable[[FFmpegProcess], Awaitable]] = None
Coroutine
function to execute after start FFmpeg process.
Credits
This package was created with Cookiecutter and the yukihiko-shinoda/cookiecutter-pypackage project template.
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
Built Distribution
File details
Details for the file asyncffmpeg-1.1.0.tar.gz
.
File metadata
- Download URL: asyncffmpeg-1.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bbcda5de526698be84c9e5ec1763596fc99df621e7fb850b8d05a27ea772dad |
|
MD5 | 480b7e5c83f61c64ce7859a7a6e8c6f9 |
|
BLAKE2b-256 | dff9c4609611246cd505058ecd97f6c72c248aa7c066896d7311f9bb7c62364a |
File details
Details for the file asyncffmpeg-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: asyncffmpeg-1.1.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec05ce72f5bf4096a51e7d5b613491e8afce8f2c94ec73934ecb6b6191ca547e |
|
MD5 | 0e70cfe90f9128c7e5240eff1a808141 |
|
BLAKE2b-256 | 63113f68d6d737345ecc8306babecc23697e9b6ed3e8f840f1873497277c3e51 |