shrinking videos to share on the web

[linkstandalone]

To reduce bandwidth or space on your HDD/SSD, it is useful to reduce the size of video. For instance some internet forums or image boards might limit video submissions to be very short (< 10MB). You can actually reduce the size of substantially long videos (several minutes) below this limit by reducing the resolution and bitrate. Obviously for high fidelity video, you want high video and audio bitrates, but in many cases, you can get away with as low as 32k and 64k bitrate for audio and video respectively. Usually, though, you probably want an audio bitrate of 64-128k at least, and a video bitrate as high above 64k as you can get. Lower bitrates will just compress the video more, and make it more pixelated-looking. In general, the file size = bitrate * video length in seconds. So if you had a 60s video at a 64k bitrate for both audio and video, you could expect a file size on the order of 60*(64k + 64k)/8 = 960 kB. Then again, resolution and framerate would both have to be taken into account. This script is pretty simple ffmpeg command, and doesn't do anything to reduce framerate. I should note that this script could be easily recreated or tweaked by studying the ffmpeg documentation. Additionally, it hasn't been prepared to take filenames that include a space in the title.


#!/bin/sh
#$1=input file
#$2=output file
#$3=audio bitrate
#$4=video bitrate
#$5=scale

ffmpeg -i $1 -c:v libvpx -c:a libvorbis -b:a $3 -b:v $4 -s $5 $2
echo "$(du -sh $1) --> $(du -sh $2)"

As an example, you might call this script with

~/path/to/script.sh ~/path/to/input.mp4 ~/path/to/output.webm 64k 128k 480x720
to reduce a 1080p mp4 video to a much smaller webm format. The libvpx and libvorbis parts are optional, here I am just declaring which codec to use. These two are a video encoder for the webm royalty-free media file format intended to be used for sharing videos on the internet. You could choose a different codec if you are using these videos for some other purpose. The last line of the script will just print the input and output file sizes. You may have to empirically try a few different bitrates or resolutions to get a resulting file that is sufficiently small while still maintaining the quality you expect.