python-ffmpeg icon indicating copy to clipboard operation
python-ffmpeg copied to clipboard

Add initializer and initargs to the execute function

Open Era-Dorta opened this issue 1 year ago • 0 comments
trafficstars

This PR exposes the initializer and initargs arguments of the ThreadPoolExecutor method. One useful example of this is integrating with tqdm to get a progress bar, as was requested in this issue #13.

Here is a minimal example on how to do that, which I'm happy to turn into a PR if this gets merged.

from multiprocessing import Value
from multiprocessing.sharedctypes import Synchronized

...
# ffmpeg command to turn images into a video
...

with tqdm(total=frame_idx) as pbar:
    prev_frame_idx_shared = Value("i", 0)

    def thread_initializer(args: Synchronized) -> None:
        global prev_frame_idx_shared
        prev_frame_idx_shared = args

    @ffmpeg.on("progress")
    def on_progress(progress: Progress) -> None:
        pbar.update(progress.frame - prev_frame_idx_shared.value)
        prev_frame_idx_shared.value = progress.frame

    ffmpeg.execute(initializer=thread_initializer, initargs=(prev_frame_idx_shared,))

Era-Dorta avatar Jul 10 '24 10:07 Era-Dorta