8000 Merge pull request #188 from akolpakov/ffprobe_extra_args · Powercoder64/ffmpeg-python@1f3ce1e · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 1f3ce1e

Browse files
authored
Merge pull request kkroening#188 from akolpakov/ffprobe_extra_args
Ability to accept extra arguments for ffmpeg.probe command (Issue kkroening#187)
2 parents e616531 + 3ddc5f0 commit 1f3ce1e

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

ffmpeg/_probe.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
22
import subprocess
33
from ._run import Error
4+
from ._utils import convert_kwargs_to_cmd_line_args
45

56

6-
def probe(filename, cmd='ffprobe'):
7+
def probe(filename, cmd='ffprobe', **kwargs):
78
"""Run ffprobe on the specified file and return a JSON representation of the output.
89
910
Raises:
@@ -12,7 +13,10 @@ def probe(filename, cmd='ffprobe'):
1213
The stderr output can be retrieved by accessing the
1314
``stderr`` property of the exception.
1415
"""
15-
args = [cmd, '-show_format', '-show_streams', '-of', 'json', filename]
16+
args = [cmd, '-show_format', '-show_streams', '-of', 'json']
17+
args += convert_kwargs_to_cmd_line_args(kwargs)
18+
args += [filename]
19+
1620
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1721
out, err = p.communicate()
1822
if p.returncode != 0:

ffmpeg/_run.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import unicode_literals
22
from .dag import get_outgoing_edges, topo_sort
3-
from ._utils import basestring
3+
from ._utils import basestring, convert_kwargs_to_cmd_line_args
44
from builtins import str
55
from functools import reduce
66
import collections
@@ -29,16 +29,6 @@ def __init__(self, cmd, stdout, stderr):
2929
self.stderr = stderr
3030

3131

32-
def _convert_kwargs_to_cmd_line_args(kwargs):
33-
args = []
34-
for k in sorted(kwargs.keys()):
35-
v = kwargs[k]
36-
args.append('-{}'.format(k))
37-
if v is not None:
38-
args.append('{}'.format(v))
39-
return args
40-
41-
4232
def _get_input_args(input_node):
4333
if input_node.name == input.__name__:
4434
kwargs = copy.copy(input_node.kwargs)
@@ -50,7 +40,7 @@ def _get_input_args(input_node):
5040
args += ['-f', fmt]
5141
if video_size:
5242
args += ['-video_size', '{}x{}'.format(video_size[0], video_size[1])]
53-
args += _convert_kwargs_to_cmd_line_args(kwargs)
43+
args += convert_kwargs_to_cmd_line_args(kwargs)
5444
args += ['-i', filename]
5545
else:
5646
raise ValueError('Unsupported input node: {}'.format(input_node))
@@ -136,7 +126,7 @@ def _get_output_args(node, stream_name_map):
136126
if not isinstance(video_size, basestring) and isinstance(video_size, collections.Iterable):
137127
video_size = '{}x{}'.format(video_size[0], video_size[1])
138128
args += ['-video_size', video_size]
139-
args += _convert_kwargs_to_cmd_line_args(kwargs)
129+
args += convert_kwargs_to_cmd_line_args(kwargs)
140130
args += [filename]
141131
return args
142132

ffmpeg/_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@ def escape_chars(text, chars):
7878
for ch in chars:
7979
text = text.replace(ch, '\\' + ch)
8080
return text
81+
82+
83+
def convert_kwargs_to_cmd_line_args(kwargs):
84+
"""Helper function to build command line arguments out of dict."""
85+
args = []
86+
for k in sorted(kwargs.keys()):
87+
v = kwargs[k]
88+
args.append('-{}'.format(k))
89+
if v is not None:
90+
args.append('{}'.format(v))
91+
return args

ffmpeg/tests/test_ffmpeg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,8 @@ def test__probe__exception():
650650
ffmpeg.probe(BOGUS_INPUT_FILE)
651651
assert str(excinfo.value) == 'ffprobe error (see stderr output for detail)'
652652
assert 'No such file or directory'.encode() in excinfo.value.stderr
653+
654+
655+
def test__probe__extra_args():
656+
data = ffmpeg.probe(TEST_INPUT_FILE1, show_frames=None)
657+
assert set(data.keys()) == {'format', 'streams', 'frames'}

0 commit comments

Comments
 (0)
0