Recently, I needed to use ffmpeg to stabilize some videos made with my iPhone, and the damned utility was giving me grief about missing VidStab support.

The process is a bit hairy, and ChatGPT initially sent me a wild goose chase, but eventually, I got things working on my WSL2 Ubuntu 18.04.6 LTS. Here’s how:

# Remove stock ffmpeg
apt-get remove ffmpeg

# Install some pre-requisites
add-apt-repository ppa:jonathonf/ffmpeg-4
apt-get update

apt-get install -y autoconf automake build-essential cmake git libaom-dev libass-dev libfdk-aac-dev libfreetype6-dev libmp3lame-dev libnuma-dev libopus-dev libsdl2-dev libtool libunistring-dev libva-dev libvdpau-dev libvorbis-dev libvpx-dev libx264-dev libx265-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev nasm pkg-config texinfo wget yasm zlib1g-dev libfontconfig1-dev

mkdir ~/ffmpeg_sources
cd ~/ffmpeg_sources
git clone https://github.com/georgmartius/vid.stab.git
cd vid.stab
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local
make
make install

cd ~/ffmpeg_sources
git clone https://github.com/FFmpeg/FFmpeg ffmpeg
cd ffmpeg
./configure --prefix=/usr/local --pkg-config-flags="--static" --extra-cflags="-I/usr/local/include" --extra-ldflags="-L/usr/local/lib" --extra-libs="-lpthread -lm" --bindir="/usr/local/bin" --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-libvidstab --enable-libfreetype --enable-libharfbuzz

Now I could generate my stabilized video, and the end result was quite decent:

/usr/local/bin/ffmpeg -i 2023-08-18_16-28-05_IMG_6679.MOV -vf vidstabdetect=shakiness=10:accuracy=15 -f null -

/usr/local/bin/ffmpeg -i 2023-08-18_16-28-05_IMG_6679.MOV -vf vidstabtransform=smoothing=30:input="transforms.trf" -c:v libx264 -preset veryfast -crf 12 -tune film 2023-08-18_16-28-05_IMG_6679_stabilized.MOV

# Generate side-by-side view
/usr/local/bin/ffmpeg -i 2023-08-18_16-28-05_IMG_6679.MOV -i 2023-08-18_16-28-05_IMG_6679_stabilized.MOV -vcodec libx264 -filter_complex "[0:v]setpts=PTS-STARTPTS, pad=iw*2:ih[bg]; [1:v]setpts=PTS-STARTPTS[fg]; [bg][fg]overlay=w" 2023-08-18_16-28-05_IMG_6679_sbs.MOV

If you have a lot of videos that you need to stabilize, I wrote a quick script that will take the input and output folders as command-line arguments and process all input video files using decent presets. You can get more info about various ffmpeg video stabilization, compression, and encoding options here.