Record window with letterbox/pillarbox on linux

By Henrik Nordvik on Tue 01 May 2012 in Linux.

It is possible to get good video recording on linux using ffmpeg and the right options. I used some time to set up mine, and made a script to make it easier to use. This script makes a letterbox or pillarbox (black stripes around the edge if the aspect ratios don’t match) as needed. Save this file to, run chmod +x, then run ./ output.flv. Then you just need to click on the window you want to record.

#!/bin/bash -x

INFO=$(xwininfo -frame)

WIN_GEO=$(echo $INFO | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
WIN_XY=$(echo $INFO | grep -oEe 'Corners:\s+\+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed 's/+/</')


OUTPUT_WIDTH=$(echo $OUTRES | cut -d'x' -f 1)
OUTPUT_HEIGHT=$(echo $OUTRES | cut -d'x' -f 2)

ffmpeg -f x11grab -s "$INRES" -r $FPS -i :0.0+$WIN_XY -f alsa \
    -ac 2 -i default -vcodec libx264 -acodec libmp3lame -ab 128k \
    -ar 44100 -threads 0 \
    -vf "scale=min($OUTPUT_WIDTH/iw\,$OUTPUT_HEIGHT/ih)*iw:-1 , pad=$OUTPUT_WIDTH:$OUTPUT_HEIGHT:max((ow-iw)/2\,0):max((oh-ih)/2\,0)" \
    -f flv $1

It is based on Tyler’s script, which does not add letterbox/pillarbox, but it streams to

Recent posts

View all posts