Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initializer and initargs to the execute function #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ffmpeg/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import os
import signal
import subprocess
from typing import IO, Optional, Union
from multiprocessing.sharedctypes import Synchronized
from typing import IO, Callable, Iterable, Optional, Union

from pyee import EventEmitter
from typing_extensions import Self
Expand Down Expand Up @@ -143,12 +144,20 @@ def output(
self._options.output(url, options, **kwargs)
return self

def execute(self, stream: Optional[Union[bytes, IO[bytes]]] = None, timeout: Optional[float] = None) -> bytes:
def execute(
self,
stream: Optional[Union[bytes, IO[bytes]]] = None,
timeout: Optional[float] = None,
initializer: Optional[Callable[[Synchronized], None]] = None,
initargs: Iterable = (),
) -> bytes:
"""Execute FFmpeg using specified global options and files.

Args:
stream: A stream to input to the standard input. Defaults to None.
timeout: The maximum number of seconds to wait before returning. Defaults to None.
initializer: A callable used to initialize worker threads. Defaults to None.
initargs: A tuple of arguments to pass to the initializer.

Raises:
FFmpegAlreadyExecuted: If FFmpeg is already executed.
Expand Down Expand Up @@ -177,7 +186,9 @@ def execute(self, stream: Optional[Union[bytes, IO[bytes]]] = None, timeout: Opt
stderr=subprocess.PIPE,
)

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=4, initializer=initializer, initargs=initargs
) as executor:
self._executed = True
futures = [
executor.submit(self._write_stdin, stream),
Expand Down