Resizing a Video with ffmpeg

A customer had a video for a website. But it was in high definition and with a large pixel footprint.

The video needed to be made smaller, of a size appropriate to the web page, making its bandwidth usage optimal.

I chose to use ffmpeg to make my conversions. I could also use it to get the video’s info: width, height etc

I had previously installed ffmpeg using

apt-get install ffmpeg

File Info

During the conversion I would need to know the existing width and height.

Using ffmpeg I can get this info. Either by using ffprobe or ffmpeg, as given below:

ffprobe inputVideo.mov

or

ffmpeg -i inputVideo.mov

Which gives the following output:

ffprobe version 3.3.2 Copyright (c) 2007-2017 the FFmpeg developers
built with gcc 6.3.0 (Debian 6.3.0-21) 20170628
configuration: --disable-decoder=amrnb --disable-decoder=libopenjpeg --disable-mips32r2 --disable-mips32r6 --disable-mips64r6 --disable-mipsdsp --disable-mipsdspr2 --disable-mipsfpu --disable-msa --disable-libopencv --disable-podpages --disable-stripping --enable-avfilter --enable-avresample --enable-gcrypt --enable-gnutls --enable-gpl --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libilbc --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx265 --enable-libxvid --enable-libzvbi --enable-nonfree --enable-opengl --enable-openssl --enable-postproc --enable-pthreads --enable-shared --enable-version3 --incdir=/usr/include/x86_64-linux-gnu --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --toolchain=hardened --enable-frei0r --enable-chromaprint --enable-libx264 --enable-libiec61883 --enable-libdc1394 --enable-vaapi --disable-opencl --enable-libmfx --disable-altivec --shlibdir=/usr/lib/x86_64-linux-gnu
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'theVideo.mov':
Metadata:
major_brand : qt
minor_version : 537199360
compatible_brands: qt
creation_time : 2017-07-10T19:59:39.000000Z
Duration: 00:03:59.11, start: 0.000000, bitrate: 7402 kb/s
Stream #0:0(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s (default)
Metadata:
creation_time : 2017-07-10T19:59:39.000000Z
handler_name : Apple Alias Data Handler
Stream #0:1(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 5863 kb/s, 23.98 fps, 23.98 tbr, 2997 tbn, 5994 tbc (default)
Metadata:
creation_time : 2017-07-10T19:59:39.000000Z
handler_name : Apple Alias Data Handler
encoder : H.264


The relevant details can be copied

And then to resize..

Scaling the Image Size

Using the scale parameter the size of the image can be changed.

ffmpeg -i inputVideo.avi -vf scale=320:240 outputVideo.avi

I began with this, basing my sizes on calculating the width and height of the original information.

ffmpeg -i inputVideo.avi -vf scale=320:240 outputVideo.avi

This will keep the aspect ratio, setting the horizontal size.

ffmpeg -i inputVideo.avi -vf scale=320:-1 outputVideo_320.avi

This makes it easier, only the one calculation and the other looks after itself.

For a simple multiplier scaling

ffmpeg -i input.avi -vf scale=iw/2:ih outputVideo_half_width.avi

Changing the Video Encoding

And best of all as you can see from the above I’ve simply set the output file name and ffmpeg has worked out the required encoding.

If you wish to also create other variations on the original video, for example mp4, mkv and ogv then substitute the extension on the outputVideo,

Flags and Faststart

Using the combination of what is movflags amd faststart the metadata fro the video can be added to the start.

ffmpeg -i inputVideo.avi -movflags faststart -vf scale=320:240 outputVideo.avi

This will make the processing of the video. when viewed. With potentially less chance of buffering issues.

However, it will take longer in processing and creating the scaled resultant video.

Further Thoughts

I used ffmpeg on Linux. It’s also available on Windows and macOS.

References

Ffmpeg Project