1

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.

George Chalhoub
  • 14,968
  • 3
  • 38
  • 61
  • 1
    Have you tried a regex looking for NUMBER x NUMBER. If not, why not use the JSON output instead? – mario Mar 04 '15 at 10:28
  • I don't know how to regex a NUMBER x NUMBER – George Chalhoub Mar 04 '15 at 10:38
  • 1
    related: http://stackoverflow.com/questions/7708373/get-ffmpeg-information-in-friendly-way – georg Mar 04 '15 at 10:40
  • Requires very little research, see http://php.net/manual/en/reference.pcre.pattern.syntax.php or [regexp.info](http://regexp.info), or even just googling for "regex width x height". – mario Mar 04 '15 at 10:40
  • 1
    I'd recommend using ffprobe, which should be installed alongside ffmpeg. `ffprobe -i input.mov -show_streams -select_streams v:0` will give you information about the video in easy-to-parse key=value pairs, one per line. Width and height are on separate lines, so you can just search for `^width=` and `^height=`. – evilsoup Mar 04 '15 at 10:48
  • -bash: ffprobe: command not found – George Chalhoub Mar 04 '15 at 10:55

3 Answers3

8

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

llogan
  • 121,796
  • 28
  • 232
  • 243
  • I use same first command but I got two result `640x360 640x360` – Salem May 30 '18 at 21:48
  • @SalemF The `program_streams` section is interfering. Workaround for files (likely mpg files for broadcast) with this section is to change `-show_entries stream=width,height` to `-show_entries program_stream=width,height`. Otherwise, just eliminate the double entry with another tool. – llogan May 30 '18 at 23:23
  • I output result as JSON so I can process it lather `ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of json input.mp4` Or simply ignore the second line of output `stream` – Salem May 31 '18 at 15:12
  • 1
    @SalemF Yes, just ignore the second line of the output, or use `ffprobe -v error -select_streams v:0 -show_entries program_stream=height,width -of json input`, but this won't output anything for inputs that do not have a `program_stream` section. – llogan May 31 '18 at 17:26
3

Try this regex:

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

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

Don't parse everything with regex, bro.

Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
1

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

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

streetturtle
  • 5,472
  • 2
  • 25
  • 43