One liner ffmpeg (or other) to get only resolution?

13,837

Solution 1

Get video resolution with ffmpeg:

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

Output (e.g.):

640x480

Solution 2

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

Example result:

1280x720

What these options do:

  • -v error makes the output less verbose.
  • -select_streams v:0 selects only the first video stream.
  • -show_entries stream=width,height chooses only width and height from the big list of parameters that ffprobe can provide.
  • -of csv=s=x:p=0 formats the text output. The csv formatting type is used because it makes a simple output. s=x makes it use an x to separate the width and height values. p=0 makes it omit the stream prefix in the output.

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

Solution 3

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.

Solution 4

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)

Solution 5

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...

Share:
13,837

Related videos on Youtube

bcsteeve
Author by

bcsteeve

Updated on September 18, 2022

Comments

  • bcsteeve
    bcsteeve over 1 year

    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!

  • bcsteeve
    bcsteeve over 9 years
    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!
  • muru
    muru over 9 years
    @bcsteeve I used it because it's part of the example content installed in Ubuntu desktop (see /usr/share/example). :D
  • bcsteeve
    bcsteeve over 9 years
    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)
  • muru
    muru over 9 years
    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).
  • bcsteeve
    bcsteeve over 9 years
    Thanks. Wasn't aware of ffprobe. So much help here! Thanks everyone.
  • David Foerster
    David Foerster about 8 years
    This is not technically a one-liner.
  • Alex Dolgov
    Alex Dolgov about 8 years
    I haven't noticed that it's one-liner requested, anyway doing the same width -ImageSize parameter returns WxH string.
  • David Foerster
    David Foerster about 8 years
    Look at the title of the question again! ;-) Anyway +1
  • llogan
    llogan about 8 years
    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.
  • Zanna
    Zanna about 6 years
    Parsing ls will not end well... quoting won't fix the mess
  • Kevin Dollabillz
    Kevin Dollabillz about 6 years
    hasn't failed me yet after 100,000 encodes