Ffmpeg-python: Obtain stream information

Created on 11 Jan 2018  路  7Comments  路  Source: kkroening/ffmpeg-python

Hello,
Is there a way to get the size (width and height) of an input video stream?
Thanks for the good work!

question

Most helpful comment

Actually I decided to add an ffmpeg.probe function in #67.

Example to print duration of a media file:

import ffmpeg
print(ffmpeg.probe('in.mp4')['format']['duration']))

This will go out in the next release.

All 7 comments

ffmpeg-python does not do anything with the input files you specify. In fact it doesn't even check if they exist or are valid (try ffmpeg.input("file_that_does_not_exist.doc")).

If you want to retrieve information about some video you can run ffprobe and parse its output, i.e.:

$ ffprobe -hide_banner some_file.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'some_file.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.71.100
  Duration: 00:03:00.07, start: 0.000000, bitrate: 373 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 237 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

Then you can use re to parse the output, for example:

import re, subprocess

o = subprocess.check_output(["ffprobe", "-hide_banner", "some_file.mp4"])
match = re.search(r"Video:.+?(\d+x\d+) \[SAR.+", o)
v_size = match.group(1)

```python

print(v_size)
640x360

Please note that the regex may need to be adapted if the video has multiple streams: the current one will only match the resolution of the first video.

These are some regular expressions that I use in one of my projects to extract some information, which can then be accessed with `match.groupdict()` (they still match the first stream only):

```python
mediaprobe_re = re.compile(
    r"Duration:\s+(?P<dur>(?:(?:\d:?)+[.]?\d*)|N/A)(?:.+start:\s+(?P<start>\d+[.]\d+))?.+bitrate:\s+(?P<bitrate>(?:\d+\s*..[/]s)|N/A)")
streamprobe_re = re.compile(
    r"\s*Stream.+:\s+Video:.+\s+(?P<res>\d+x\d+)(?:.*,\s*(?P<fps>\d+[.]?\d*)\sfps)?(?:.+\(default\))?")
audioprobe_re = re.compile(
    r"\s*Stream.+:\s+Audio:.*")
fftime_re = re.compile(
    r"(?P<h>\d+):(?P<m>\d+):(?P<s>\d+)\.(?P<fract>\d+)")

Note that you must do the parsing on your own: ffmpeg-python does not support this.
However, this may be an idea for a new feature; @kkroening what do you think?

Ok, thanks a lot for the detailed answer. I'll look into that.

It'd be nice to be able to call ffprobe from python. No specific plans to implement it, but wouldn't be opposed to it. I can see lots of cases where it'd be useful though for sure.

Here's an example of calling ffprobe on the extra-examples2 branch: jffprobe.py.

It's a command-line tool that works like this:

python jffprobe.py *.mp3

It produces a JSON representation on stdout of the ffprobe data for all the filenames listed and any error/warning messages on stderr.

But you could probably take the run_ffprobe function and put it in another script. I might add this into the core of ffmpeg-python at some point, but again no specific plans as of this moment.

Actually I decided to add an ffmpeg.probe function in #67.

Example to print duration of a media file:

import ffmpeg
print(ffmpeg.probe('in.mp4')['format']['duration']))

This will go out in the next release.

That's awesome! I didn't know ffprobe could output json, that's quite handy.

For completeness, here's how to use the ffmpeg.probe function to get width and height of a video:

probe = ffmpeg.probe(args.file)
video = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
width = int(video['width'])
height = int(video['height'])
Was this page helpful?
0 / 5 - 0 ratings