How to Get the Resolution (Width and Height) for a Video File from a Linux Command Line

How can I get the resolution (width and height) for a video file from a linux command line?

MediaInfo has a command line version and provides the dimensions together with tons of other information.

How can I get the resolution (width and height) for a video file in Nautilus/Caja linux file manager?

MediaInfo http://mediainfo.sf.net has a command line version and provides the dimensions together with tons of other information.

Getting video dimension / resolution / width x height from ffmpeg

Have a look at mediainfo Handles most of the formats out there.

If you looking for a way to parse the output from ffmpeg, use the regexp \d+x\d+

Example using perl:

$ ./ffmpeg -i test020.3gp 2>&1 | perl -lane 'print $1 if /(\d+x\d+)/'
176x120

Example using python (not perfect):

$ ./ffmpeg -i /nfshome/enilfre/pub/test020.3gp 2>&1 | python -c "import sys,re;[sys.stdout.write(str(re.findall(r'(\d+x\d+)', line))) for line in sys.stdin]"

[][][][][][][][][][][][][][][][][][][]['176x120'][][][]

Python one-liners aren't as catchy as perl ones :-)

Get dimensions of a video file

In my last company we had similar problem and I couldn't find any python library to do this. So I ended up using mediainfo from python, media info also has a command line option and it is very easy to parse the output, so practically your python module which uses media-info will be sufficient. It has further advantage because eventually you will find all media-info type software doesn't support all codecs/format so you can use multiple software/libs under the hood with single python wrapper.

Get video resolution in nodejs

To be honest I think the best method I found was to use fluent-ffmpeg with ffprobe as you are able to set the the path to the executable. The only problem is that ffmpeg has to be shipped with the app. So different executables have to be shipped, one for each distribution/os/derivation. If anyone has anything better I am open to answers.

Getting the width, height and aspect ratio using fluent-ffmpeg is done like so:

var ffmpeg = require('fluent-ffmpeg');

ffmpeg.setFfprobePath(pathToFfprobeExecutable);

ffmpeg.ffprobe(pathToYourVideo, function(err, metadata) {
if (err) {
console.error(err);
} else {
// metadata should contain 'width', 'height' and 'display_aspect_ratio'
console.log(metadata);
}
});

display only lines from output that contains a specified word

@paulsm4 was spot on ... the output goes to STDERR.

streams=$(ffprobe -i  "movie.mp4"  |& grep "Stream")

Note the &

Change video resolution ffmpeg

I guess that really there are two questions here...

  1. How do I batch convert files?
  2. How do I auto scale a video?

How do I batch convert files?

These scripts ought to do the trick...

Windows

for %%i in (*.mp4) do (
ffmpeg -y -i "%%i" << TODO >> "%%~ni_shrink.mp4"
)

Linux (UNTESTED!)

for i in *.mp4; 
do
ffmpeg -y -i "$i" << TODO >> "${i%.mp4}_shrink.mp4";
done

(I'm not too sure about the output file expansion in the Linux script, worth validating that.)


How do I auto scale a video?

This is a little trickier. As you have your command, the aspect ratio is potentially going to get messed up. Options here...

  1. Scale the video, ignore aspect ratio. Result = distorted video
  2. Scale the video, keep aspect ratio so that the scaled height (or width) is adjusted to fit. Result = optimal
  3. Scale the video, keep aspect ratio and pad with black bars so that the video size is exactly 480x320. Result = wasted/increased file size
  4. Crop the input before scaling so that it "fills" the 480x320 resolution. Result = incomplete video

Option 2 would be the preferred solution, otherwise you are (probably unnecessarily) increasing the output file size. Option 3, I'll give a partially tested solution. Option 4 I'm not even going to touch.

Option 2: Scale the video, keep aspect ratio so that height is adjusted to fit

ffmpeg -y -i "%%i" -vf scale=480:-2,setsar=1:1 -c:v libx264 -c:a copy "%%~ni_shrink.mp4"

Option 3: Scale the video, keep aspect ratio and pad with black bars so that the video size is exactly 480x320

ffmpeg -y -i "%%i" -vf "[in]scale=iw*min(480/iw\,320/ih):ih*min(480/iw\,320/ih)[scaled]; [scaled]pad=480:320:(480-iw*min(480/iw\,320/ih))/2:(320-ih*min(480/iw\,320/ih))/2[padded]; [padded]setsar=1:1[out]" -c:v libx264 -c:a copy "%%~ni_shrink.mp4"


Related Topics



Leave a reply



Submit