8000 Merge pull request #67 from kkroening/probe · monir-dev/ffmpeg-python@7077eaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 7077eaa

Browse files
authored
Merge pull request kkroening#67 from kkroening/probe
Add ffprobe support
2 parents 8420f3b + 4927bbe commit 7077eaa

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

ffmpeg/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import unicode_literals
22

3-
from . import _filters, _ffmpeg, _run
3+
from . import _filters, _ffmpeg, _run, _probe
44
from ._filters import *
55
from ._ffmpeg import *
66
from ._run import *
77
from ._view import *
8+
from ._probe import *
89

9-
__all__ = _filters.__all__ + _ffmpeg.__all__ + _run.__all__ + _view.__all__
10+
__all__ = _filters.__all__ + _ffmpeg.__all__ + _run.__all__ + _view.__all__ + _probe.__all__

ffmpeg/_probe.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import subprocess
3+
4+
5+
class ProbeException(Exception):
6+
def __init__(self, stderr_output):
7+
super(ProbeException, self).__init__('ffprobe error')
8+
self.stderr_output = stderr_output
9+
10+
11+
def probe(filename):
12+
"""Run ffprobe on the specified file and return a JSON representation of the output.
13+
14+
Raises:
15+
ProbeException: if ffprobe returns a non-zero exit code, a ``ProbeException`` is returned with a generic error
16+
message. The stderr output can be retrieved by accessing the ``stderr_output`` property of the exception.
17+
"""
18+
args = ['ffprobe', '-show_format', '-show_streams', '-of', 'json', filename]
19+
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
20+
out, err = p.communicate()
21+
if p.returncode != 0:
22+
raise ProbeException(err)
23+
return json.loads(out.decode('utf-8'))
24+
25+
26+
__all__ = [
27+
'probe',
28+
'ProbeException',
29+
]

ffmpeg/tests/test_ffmpeg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
TEST_OVERLAY_FILE = os.path.join(SAMPLE_DATA_DIR, 'overlay.png')
1717
TEST_OUTPUT_FILE1 = os.path.join(SAMPLE_DATA_DIR, 'out1.mp4')
1818
TEST_OUTPUT_FILE2 = os.path.join(SAMPLE_DATA_DIR, 'out2.mp4')
19+
BOGUS_INPUT_FILE = os.path.join(SAMPLE_DATA_DIR, 'bogus')
1920

2021

2122
subprocess.check_call(['ffmpeg', '-version'])
@@ -432,3 +433,16 @@ def test_pipe():
432433
out_data = p.stdout.read()
433434
assert len(out_data) == frame_size * (frame_count - start_frame)
434435
assert out_data == in_data[start_frame*frame_size:]
436+
437+
438+
def test_ffprobe():
439+
data = ffmpeg.probe(TEST_INPUT_FILE1)
440+
assert set(data.keys()) == {'format', 'streams'}
441+
assert data['format']['duration'] == '7.036000'
442+
443+
444+
def test_ffprobe_exception():
445+
with pytest.raises(ffmpeg.ProbeException) as excinfo:
446+
ffmpeg.probe(BOGUS_INPUT_FILE)
447+
assert str(excinfo.value) == 'ffprobe error'
448+
assert b'No such file or directory' in excinfo.value.stderr_output

0 commit comments

Comments
 (0)
0