8

I'm not really well versed in the command line so hopefully this isn't too stupid of a question.

If I run:

ffmpeg -i videofile.avi

I get an output such as this:

ffmpeg version git-2013-11-21-6a7980e Copyright (c) 2000-2013 the FFmpeg develop    ers
  built on Nov 21 2013 12:06:32 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --prefix=/home/user/ffmpeg_build --extra-cflags=-I/home/user/ffmpeg_build/include --extra-ldflags=-L/home/user/ffmpeg_build/lib --b        indir=/home/user/bin --extra-libs=-ldl --enable-gpl --enable-libass --enable    -libfdk-    aac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-l    ibx264 --enable-    nonfree
  libavutil      52. 53.100 / 52. 53.100
  libavcodec     55. 44.100 / 55. 44.100
  libavformat    55. 21.100 / 55. 21.100
  libavdevice    55.  5.100 / 55.  5.100
  libavfilter     3. 91.100 /  3. 91.100
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
Input #0, avi, from 'videofile.avi':
  Metadata:
    encoder         : Lavf52.68.0
  Duration: 00:23:07.68, start: 0.000000, bitrate: 2390 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (XVID / 0x44495658), yuv420p, 640x480     [SAR 1:1 DAR 4:3], 23.98 fps, 23.98 tbr, 23.98 tbn, 1199 tbc
    Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 128 k    b/s

If I were only interested in the command outputting "640x480", how might I do that?

I imagine I have to pipe the output to, and perform, a regular expression? No idea how to do that. Thanks!

9

Get video resolution with ffmpeg:

ffmpeg -i filename 2>&1 | grep -oP 'Stream .*, \K[0-9]+x[0-9]+'

Output (e.g.):

640x480
  • muru's answer was certainly more than sufficient, but this one specifically answers my question as asked (ie. using ffmpeg). I don't know what the protocol here is with choosing an answer. In this case, I think Muru's answer is more helpful while Cyrus's is more on point. Sorry, not sure which way to go but right now I'm selecting Cyrus' as it is very specifically what I asked for (but I'm using Muru's lol) – bcsteeve Jan 24 '15 at 19:30
  • 1
    Be careful though. The example shown in the question would result in grep producing two matches (the first 0x44... and the second being the resolution). – muru Jan 24 '15 at 20:29
  • If the input contains multiple video streams, such as an input from a DVD, then this answer will output a width x height per video stream in multiple lines. This can be avoided if desired with ffprobe, or by adding head -n 1 to the command in the answer. – llogan May 16 '16 at 18:34
9

Using ffprobe from the ffmpeg package:

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

See the ffprobe documentation and FFmpeg Wiki: ffprobe tips for more info.

  • Thanks. Wasn't aware of ffprobe. So much help here! Thanks everyone. – bcsteeve Jan 25 '15 at 6:12
4
exiftool -b metavideo.mp4 -ImageWidth
exiftool -b metavideo.mp4 -ImageHeight

do the job without any greps

One-liner looks like:

exiftool -b metavideo.mp4 -ImageSize

That returns WxH string that you was looking for.

  • This is not technically a one-liner. – David Foerster May 16 '16 at 11:00
  • I haven't noticed that it's one-liner requested, anyway doing the same width -ImageSize parameter returns WxH string. – Alex Dolgov May 16 '16 at 13:09
  • Look at the title of the question again! ;-) Anyway +1 – David Foerster May 16 '16 at 13:27
3

I only see here samples of using exiftool and parsing it's output, which seems to be a weird choice. exiftool can be used in a direct manner to get the resolution (and almost any other metadata) :

exiftool -ImageSize -s3 filename

Will results in output in a form WIDTHxHEIGHT (eq: 1280×720)

NOTE: -s3 instructs exiftool to produce the shortest output format (without tag names, quotes, etc)

2

You can do this using regular expressions and ffmpeg, but I'd prefer to use exiftool, part of the libimage-exiftool-perl Install libimage-exiftool-perl package:

$ exiftool some/video.ogv | awk -F' *: *' '$1 == "Image Size"{print $2}'
1242x480

Why exiftool?

  • The ffmpeg/avprobe utilities print lines of the form Stream #0:0: Video: mpeg4 (Simple Profile) (XVID / 0x44495658), yuv420p, 640x480 where one or more values of the form AAAxBBB are possible (as you can see in your example). Determining which one is the resolution might be difficult.
  • exiftool, on the other hand, outputs data of the form Type : Value. So it's easier to get the exact field, namely Image Size. Plus, given the way the fields are specified (separated by colon and whitespace), you can easily extract the value.

What the awk command does is:

  • Set the field separator to ' *: *' - a colon : surrounded by any number of spaces.
  • Check if the first field ($1) is Image Size
  • And print the second field (print $2).

An alternate version would be:

awk '/^Image Size/{print $4}'
  • By default, awk uses whitespace to split fields, so Image, Size, : and 1242x480 are all different fields.
  • By matching the line's beginning (^) to Image Size, we get the correct line.
  • And we print the fourth field (since Image, Size and : are the first three fields).
  • Thanks! I might suggest you edit your example to change "Ubuntu_Free_Culture_Showcase/How\ fast.ogg" to something more generic. At first I was using it literally because I didn't recognize it as a file name. Of course, I saw my error 2 seconds later and your solution works great! – bcsteeve Jan 24 '15 at 2:09
  • @bcsteeve I used it because it's part of the example content installed in Ubuntu desktop (see /usr/share/example). :D – muru Jan 24 '15 at 3:51
1

A slightly esoteric use of mediainfo will easily extract width and height in the format that you are after using a simple one-liner. Observe the following example on my system:

andrew@illium~$ mediainfo --Inform="Video;%Width%x%Height%" test.mp4
1920x1080

The various further parameters that can be used in this way can be seen with mediainfo --Info-Parameters but perhaps this simple usage by itself will suffice for you...

0

I made a 1 liner for loop to extract resolution from directory of files.

for i in $(ls "/root/vidz/dollabillz/"*); do eval $(ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=width,height "$i"); size=${streams_stream_0_width}x${streams_stream_0_height}; echo $i $size; done;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.