Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

Getting video dimension / resolution / width x height from ffmpeg

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
...
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2010-01-24 00:55:16
  Duration: 00:00:35.08, start: 0.000000, bitrate: 354 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 640x360 [PAR 1:1 DAR 16:9], 597 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc
    Metadata:
      creation_time   : 2010-01-24 00:55:16
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 109 kb/s
    Metadata:
      creation_time   : 2010-01-24 00:55:17
At least one output file must be specified

How would I get height = 640, width= 360?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use ffprobe

Example 1: With keys / variable names

ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 input.mp4
width=1280
height=720

Example 2: Just width x height

ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=p=0:s=x input.m4v
1280x720

Example 3: JSON

ffprobe -v error -select_streams v -show_entries stream=width,height -of json input.mkv 
{
    "programs": [

    ],
    "streams": [
        {
            "width": 1280,
            "height": 720
        }
    ]
}

Example 4: JSON Compact

ffprobe -v error -select_streams v -show_entries stream=width,height -of json=compact=1 input.mkv 
{
    "programs": [

    ],
    "streams": [
        { "width": 1280, "height": 720 }
    ]
}

Example 5: XML

ffprobe -v error -select_streams v -show_entries stream=width,height -of xml input.mkv 
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
    <programs>
    </programs>

    <streams>
        <stream width="1280" height="720"/>
    </streams>
</ffprobe>

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. -select_streams v can be used to show info from all video streams and avoid empty audio stream info in JSON and XML output.

  • See the FFprobe Documentation and FFmpeg Wiki: FFprobe Tips for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...