Skip to content
Merged
Show file tree
Hide file tree
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
Ability to accept extra arguments for ffmpeg.probe command (Issue #187)
  • Loading branch information
akolpakov committed Apr 11, 2019
commit 3ddc5f04bfc9f0f5e7da05e48546b5d91cac62c9
8 changes: 6 additions & 2 deletions ffmpeg/_probe.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import subprocess
from ._run import Error
from ._utils import convert_kwargs_to_cmd_line_args


def probe(filename, cmd='ffprobe'):
def probe(filename, cmd='ffprobe', **kwargs):
"""Run ffprobe on the specified file and return a JSON representation of the output.

Raises:
Expand All @@ -12,7 +13,10 @@ def probe(filename, cmd='ffprobe'):
The stderr output can be retrieved by accessing the
``stderr`` property of the exception.
"""
args = [cmd, '-show_format', '-show_streams', '-of', 'json', filename]
args = [cmd, '-show_format', '-show_streams', '-of', 'json']
args += convert_kwargs_to_cmd_line_args(kwargs)
args += [filename]

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
Expand Down
16 changes: 3 additions & 13 deletions ffmpeg/_run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from .dag import get_outgoing_edges, topo_sort
from ._utils import basestring
from ._utils import basestring, convert_kwargs_to_cmd_line_args
from builtins import str
from functools import reduce
import collections
Expand Down Expand Up @@ -29,16 +29,6 @@ def __init__(self, cmd, stdout, stderr):
self.stderr = stderr


def _convert_kwargs_to_cmd_line_args(kwargs):
args = []
for k in sorted(kwargs.keys()):
v = kwargs[k]
args.append('-{}'.format(k))
if v is not None:
args.append('{}'.format(v))
return args


def _get_input_args(input_node):
if input_node.name == input.__name__:
kwargs = copy.copy(input_node.kwargs)
Expand All @@ -50,7 +40,7 @@ def _get_input_args(input_node):
args += ['-f', fmt]
if video_size:
args += ['-video_size', '{}x{}'.format(video_size[0], video_size[1])]
args += _convert_kwargs_to_cmd_line_args(kwargs)
args += convert_kwargs_to_cmd_line_args(kwargs)
args += ['-i', filename]
else:
raise ValueError('Unsupported input node: {}'.format(input_node))
Expand Down Expand Up @@ -136,7 +126,7 @@ def _get_output_args(node, stream_name_map):
if not isinstance(video_size, basestring) and isinstance(video_size, collections.Iterable):
video_size = '{}x{}'.format(video_size[0], video_size[1])
args += ['-video_size', video_size]
args += _convert_kwargs_to_cmd_line_args(kwargs)
args += convert_kwargs_to_cmd_line_args(kwargs)
args += [filename]
return args

Expand Down
11 changes: 11 additions & 0 deletions ffmpeg/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ def escape_chars(text, chars):
for ch in chars:
text = text.replace(ch, '\\' + ch)
return text


def convert_kwargs_to_cmd_line_args(kwargs):
"""Helper function to build command line arguments out of dict."""
args = []
for k in sorted(kwargs.keys()):
v = kwargs[k]
args.append('-{}'.format(k))
if v is not None:
args.append('{}'.format(v))
return args
5 changes: 5 additions & 0 deletions ffmpeg/tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,8 @@ def test__probe__exception():
ffmpeg.probe(BOGUS_INPUT_FILE)
assert str(excinfo.value) == 'ffprobe error (see stderr output for detail)'
assert 'No such file or directory'.encode() in excinfo.value.stderr


def test__probe__extra_args():
data = ffmpeg.probe(TEST_INPUT_FILE1, show_frames=None)
assert set(data.keys()) == {'format', 'streams', 'frames'}