从ffmpeg -i获取视频维度

Dav*_*542 11 python video ffmpeg ffprobe

如何从ffmpeg信息输出中获取视频的高度和宽度.例如,使用以下输出 -

$ ffmpeg -i 1video.mp4
...

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/david/Desktop/1video.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
Run Code Online (Sandbox Code Playgroud)

我怎么会得到height = 640, width= 360?谢谢.

llo*_*gan 41

使用 ffprobe

示例1:使用键/变量名称

ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 input.mp4
width=1280
height=720
Run Code Online (Sandbox Code Playgroud)

示例2:正好宽x高

ffprobe -v error -show_entries stream=width,height -of csv=p=0:s=x input.m4v
1280x720
Run Code Online (Sandbox Code Playgroud)

示例3:JSON

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

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

        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

选项的作用如下:

  • -v error进行安静输出,但允许显示错误.排除通常的通用FFmpeg输出信息,包括版本,配置和输入详细信息.

  • -show_entries stream=width,height只显示widthheight流信息.

  • -of选项选择输出格式(默认,紧凑,csv,平面,ini,json,xml).有关每种格式的说明,请参阅FFprobe文档:Writer,以及查看其他格式选项.

  • -select_streams v:0如果您的输入包含多个视频流,则可以添加此选项.v:0将仅选择第一个视频流.否则,您将获得widthheight视频流一样多的数量和输出.

  • 有关详细信息,请参阅FFprobe文档FFmpeg Wiki:FFprobe技巧.

  • `-of json`以JSON格式返回数据,这在Python中更容易访问(避免使用正则表达式). (3认同)

Fre*_*ihl 11

看一下mediainfo处理大多数格式.

如果您正在寻找解析ffmpeg输出的方法,请使用regexp \d+x\d+

使用perl的示例:

$ ./ffmpeg -i test020.3gp 2>&1 | perl -lane 'print $1 if /(\d+x\d+)/'
176x120
Run Code Online (Sandbox Code Playgroud)

使用python的例子(不完美):

$ ./ffmpeg -i /nfshome/enilfre/pub/test020.3gp 2>&1 | python -c "import sys,re;[sys.stdout.write(str(re.findall(r'(\d+x\d+)', line))) for line in sys.stdin]"
Run Code Online (Sandbox Code Playgroud)

[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [ '176x120'] [] [] []

Python one-liners并不像perl那样引人注目:-)


old*_*cho 5

正如此处提到的,ffprobe提供了一种检索视频文件数据的方法。我发现以下命令ffprobe -v quiet -print_format json -show_streams input-video.xxx对于查看您可以签出哪种数据很有用。

然后我编写了一个函数来运行上述命令并返回视频文件的高度和宽度:

import subprocess
import shlex
import json

# function to find the resolution of the input video file
def findVideoResolution(pathToInputVideo):
    cmd = "ffprobe -v quiet -print_format json -show_streams"
    args = shlex.split(cmd)
    args.append(pathToInputVideo)
    # run the ffprobe process, decode stdout into utf-8 & convert to JSON
    ffprobeOutput = subprocess.check_output(args).decode('utf-8')
    ffprobeOutput = json.loads(ffprobeOutput)

    # find height and width
    height = ffprobeOutput['streams'][0]['height']
    width = ffprobeOutput['streams'][0]['width']

    return height, width
Run Code Online (Sandbox Code Playgroud)