Skip to content

Commit

Permalink
Support arbitrary keyword arguments for subprocess.Popen (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch00k authored Jul 17, 2023
1 parent d44a9ee commit 11b2f03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions ffmpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(
def __repr__(self):
return "<{0!r} {1!r}>".format(self.__class__.__name__, self.cmd)

def run(self, input_data=None, stdout=None, stderr=None, env=None):
def run(self, input_data=None, stdout=None, stderr=None, env=None, **kwargs):
"""Execute FFmpeg command line.
``input_data`` can contain input for FFmpeg in case ``pipe`` protocol is used for input.
Expand All @@ -84,14 +84,21 @@ def run(self, input_data=None, stdout=None, stderr=None, env=None):
:param stderr: redirect FFmpeg ``stderr`` there (default is `None` which means no
redirection)
:param env: custom environment for ffmpeg process
:param kwargs: any other keyword arguments to be forwarded to `subprocess.Popen
<https://docs.python.org/3/library/subprocess.html#subprocess.Popen>`_
:return: a 2-tuple containing ``stdout`` and ``stderr`` of the process
:rtype: tuple
:raise: `FFRuntimeError` in case FFmpeg command exits with a non-zero code;
`FFExecutableNotFoundError` in case the executable path passed was not valid
"""
try:
self.process = subprocess.Popen(
self._cmd, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, env=env
self._cmd,
stdin=subprocess.PIPE,
stdout=stdout,
stderr=stderr,
env=env,
**kwargs
)
except OSError as e:
if e.errno == errno.ENOENT:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cmd_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,21 @@ def test_custom_env(popen_mock):
popen_mock.assert_called_with(
mock.ANY, stdin=mock.ANY, stdout=mock.ANY, stderr=mock.ANY, env="customenv"
)


@mock.patch("ffmpy.subprocess.Popen")
def test_arbitraty_popen_kwargs(popen_mock):
ff = FFmpeg()
popen_mock.return_value.communicate.return_value = ("output", "error")
popen_mock.return_value.returncode = 0
ff.run(creationflags=42, encoding="foo", text="bar")
popen_mock.assert_called_with(
mock.ANY,
stdin=mock.ANY,
stdout=mock.ANY,
stderr=mock.ANY,
env=None,
creationflags=42,
encoding="foo",
text="bar",
)

0 comments on commit 11b2f03

Please sign in to comment.