Get dimension of a video using regex and ffmpeg

2019-05-30 10:47发布

问题:

If I have this string given by a ffmpeg command when you try to get information about a video:

Copyright (c) 2000-2015 the FFmpeg developers built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-11) configuration: --prefix=/usr/local/cpffmpeg --enable-shared --enable-nonfree --enable-gpl --enable-pthreads --enable-libopencore-amrnb --enable-decoder=liba52 --enable-libopencore-amrwb --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --extra-cflags=-I/usr/local/cpffmpeg/include/ --extra-ldflags=-L/usr/local/cpffmpeg/lib --enable-version3 --extra-version=syslint libavutil 54. 19.100 / 54. 19.100 libavcodec 56. 26.100 / 56. 26.100 libavformat 56. 23.106 / 56. 23.106 libavdevice 56. 4.100 / 56. 4.100 libavfilter 5. 11.102 / 5. 11.102 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 1.100 / 1. 1.100 libpostproc 53. 3.100 / 53. 3.100Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/zpanel/hostdata/zadmin/public_html/chandelier.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 URL : Follow Me On > www.hamhame1.in compilation : 0 title : Follow Me On > www.hamhame1.in artist : Follow Me On > www.hamhame1.in album : Follow Me On > www.hamhame1.in date : Follow Me On > www.hamhame1.in genre : Follow Me On > www.hamhame1.in comment : Follow Me On > www.hamhame1.in composer : Follow Me On > www.hamhame1.in original_artist : Follow Me On > www.hamhame1.in copyright : Follow Me On > www.hamhame1.in encoder : Follow Me On > www.hamhame1.in album_artist : Follow Me On > www.hamhame1.in season_number : 0 episode_sort : 0 track : 0 disc : 0 media_type : 0 Duration: 00:03:51.35, start: 0.000000, bitrate: 2778 kb/s Stream 0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 > DAR 16:9], 2646 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default) Metadata: handler_name : SoundHandlerAt least one output file must be specified

In this case video dimension is: 1920x1080

How can I export video dimension knowing that yuv420p and [SAR 1:1 > DAR 16:9] might be different (and also that. 1920x1080 could be 402x250 or 24x59). I'm not really interested in using third-party classes.

回答1:

Try this regex:

(\b[^0]\d+x[^0]\d+\b)

Demo https://regex101.com/r/bM6cN0/1

Don't parse everything with regex, bro.



回答2:

Use ffprobe. It is from the FFmpeg project. No need for regex (if your input has two video streams then that is two sets of info to process). You can limit the parsing to specific streams and modify outputs to display whatever parameter(s) you want.

Example 1: widthxheight

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

Result:

1280x720

Example 2: With keys

ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1 input.mp4

Result:

width=1280
height=720

Also see

  • FFprobe Documentation
  • FFmpeg Wiki: FFprobe


回答3:

Something like \d{2,5}x\d{2,5} should do the work.

Test: https://regex101.com/r/cV8mE0/1