One liner ffmpeg (or other) to get only resolution?
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!
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 onlywidth
andheight
from the big list of parameters thatffprobe
can provide. -
-of csv=s=x:p=0
formats the text output. Thecsv
formatting type is used because it makes a simple output.s=x
makes it use anx
to separate thewidth
andheight
values.p=0
makes it omit thestream
prefix in the output.
See the ffprobe documentation and FFmpeg Wiki: ffprobe tips for more info.
Get video resolution with ffmpeg:
ffmpeg -i filename 2>&1 | grep -oP 'Stream .*, \K[0-9]+x[0-9]+'
Output (e.g.):
640x480
exiftool -b metavideo.mp4 -ImageWidth
exiftool -b metavideo.mp4 -ImageHeight
do the job without any grep
s
One-liner looks like:
exiftool -b metavideo.mp4 -ImageSize
That returns WxH string that you was looking for.
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)
You can do this using regular expressions and ffmpeg
, but I'd prefer to use exiftool
, part of the 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 formStream #0:0: Video: mpeg4 (Simple Profile) (XVID / 0x44495658), yuv420p, 640x480
where one or more values of the formAAAxBBB
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 formType : Value
. So it's easier to get the exact field, namelyImage 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
) isImage 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
,:
and1242x480
are all different fields. - By matching the line's beginning (
^
) toImage Size
, we get the correct line. - And we print the fourth field (since
Image
,Size
and:
are the first three fields).