Add ffprobe support

This commit is contained in:
Karl Kroening 2018-01-27 21:19:19 -08:00
parent 19f316e9c5
commit 25bda398c9
3 changed files with 37 additions and 2 deletions

View File

@ -1,9 +1,10 @@
from __future__ import unicode_literals
from . import _filters, _ffmpeg, _run
from . import _filters, _ffmpeg, _run, _probe
from ._filters import *
from ._ffmpeg import *
from ._run import *
from ._view import *
from ._probe import *
__all__ = _filters.__all__ + _ffmpeg.__all__ + _run.__all__ + _view.__all__
__all__ = _filters.__all__ + _ffmpeg.__all__ + _run.__all__ + _view.__all__ + _probe.__all__

28
ffmpeg/_probe.py Executable file
View File

@ -0,0 +1,28 @@
import json
import subprocess
class ProbeException(Exception):
def __init__(self, stderr_output):
super(ProbeException, self).__init__('ffprobe error')
self.stderr_output = stderr_output
def probe(filename):
"""Run ffprobe on the specified file and return a JSON representation of the output.
Raises:
ProbeException: if ffprobe returns a non-zero exit code, a ``ProbeException`` is returned with a generic error
message. The stderr output can be retrieved by accessing the ``stderr_output`` property of the exception.
"""
args = ['ffprobe', '-show_format', '-show_streams', '-of', 'json', filename]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
raise ExecException(err)
return json.loads(out)
__all__ = [
'probe',
]

View File

@ -353,3 +353,9 @@ def test_pipe():
out_data = p.stdout.read()
assert len(out_data) == frame_size * (frame_count - start_frame)
assert out_data == in_data[start_frame*frame_size:]
def test_ffprobe():
data = ffmpeg.probe(TEST_INPUT_FILE1)
assert set(data.keys()) == {'format', 'streams'}
assert data['format']['duration'] == '7.036000'