diff --git a/ffmpeg/_probe.py b/ffmpeg/_probe.py index 41e8168..090d7ab 100644 --- a/ffmpeg/_probe.py +++ b/ffmpeg/_probe.py @@ -4,7 +4,7 @@ from ._run import Error 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: @@ -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')) diff --git a/ffmpeg/tests/test_ffmpeg.py b/ffmpeg/tests/test_ffmpeg.py index 0d600f2..51ee258 100644 --- a/ffmpeg/tests/test_ffmpeg.py +++ b/ffmpeg/tests/test_ffmpeg.py @@ -8,6 +8,7 @@ import pytest import random import re import subprocess +import sys try: @@ -711,6 +712,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)