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
7 changes: 5 additions & 2 deletions ffmpeg/_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ._utils import convert_kwargs_to_cmd_line_args


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

Raises:
Expand All @@ -18,7 +18,10 @@ def probe(filename, cmd='ffprobe', **kwargs):
args += [filename]

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
communicate_kwargs = {}
if timeout is not None:
communicate_kwargs['timeout'] = timeout
out, err = p.communicate(**communicate_kwargs)
if p.returncode != 0:
raise Error('ffprobe', out, err)
return json.loads(out.decode('utf-8'))
Expand Down
8 changes: 8 additions & 0 deletions ffmpeg/tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random
import re
import subprocess
import sys

try:
import mock # python 2
Expand Down Expand Up @@ -706,6 +707,13 @@ def test__probe():
assert data['format']['duration'] == '7.036000'


@pytest.mark.skipif(sys.version_info < (3, 3), reason="requires python3.3 or higher")
def test__probe_timeout():
with pytest.raises(subprocess.TimeoutExpired) as excinfo:
data = ffmpeg.probe(TEST_INPUT_FILE1, timeout=0)
assert 'timed out after 0 seconds' in str(excinfo.value)


def test__probe__exception():
with pytest.raises(ffmpeg.Error) as excinfo:
ffmpeg.probe(BOGUS_INPUT_FILE)
Expand Down