How would I get the height and width of a video from ffmpeg
\'s information output. For example, with the following output:
$ ffmpeg -i video.mp4
The best way for to answer this question would be for an ffmpeg developer to explain exactly what the format of the ffmpeg output is expected to be and whether we can consistently assume the size to be located in a specified context within it. Until then we can only guess from example what the format usually is.
Here's my attempt. It's verbose compared to these "one-liners", but that's because I'd like to know why it fails when it eventually does.
import subprocess
def get_video_size(video_filename):
"""Returns width, height of video using ffprobe"""
# Video duration and hence start time
proc = subprocess.Popen(['ffprobe', video_filename],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = proc.communicate()[0]
# Check if ffprobe failed, probably on a bad file
if 'Invalid data found when processing input' in res:
raise ValueError("Invalid data found by ffprobe in %s" % video_filename)
# Find the video stream
width_height_l = []
for line in res.split("\n"):
# Skip lines that aren't stream info
if not line.strip().startswith("Stream #"):
continue
# Check that this is a video stream
comma_split = line.split(',')
if " Video: " not in comma_split[0]:
continue
# The third group should contain the size and aspect ratio
if len(comma_split) < 3:
raise ValueError("malform video stream string:", line)
# The third group should contain the size and aspect, separated
# by spaces
size_and_aspect = comma_split[2].split()
if len(size_and_aspect) == 0:
raise ValueError("malformed size/aspect:", comma_split[2])
size_string = size_and_aspect[0]
# The size should be two numbers separated by x
width_height = size_string.split('x')
if len(width_height) != 2:
raise ValueError("malformed size string:", size_string)
# Cast to int
width_height_l.append(map(int, width_height))
if len(width_height_l) > 1:
print "warning: multiple video streams found, returning first"
return width_height_l[0]
ffprobe
ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 input.mp4
width=1280
height=720
ffprobe -v error -show_entries stream=width,height -of csv=p=0:s=x input.m4v
1280x720
ffprobe -v error -show_entries stream=width,height -of json input.mkv
{
"programs": [
],
"streams": [
{
"width": 1280,
"height": 720
},
{
}
]
}
What the options do:
-v error
Make a quiet output, but allow errors to be displayed. Excludes the usual generic FFmpeg output info including version, config, and input details.
-show_entries stream=width,height
Just show the width
and height
stream information.
-of
option chooses the output format (default, compact, csv, flat, ini, json, xml). See FFprobe Documentation: Writers for a description of each format and to view additional formatting options.
-select_streams v:0
This can be added in case your input contains multiple video streams. v:0
will select only the first video stream. Otherwise you'll get as many width
and height
outputs as there are video streams.
See the FFprobe Documentation and FFmpeg Wiki: FFprobe Tips for more info.