Skip to content

Instantly share code, notes, and snippets.

@gwollman
Created October 27, 2021 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwollman/7a40ce5a96160ad5a21f2cca0b44f4e8 to your computer and use it in GitHub Desktop.
Save gwollman/7a40ce5a96160ad5a21f2cca0b44f4e8 to your computer and use it in GitHub Desktop.
A simple shell script to record the contents of a window to a video file using only ffmpeg
#!/bin/sh
outfile="${1-screencast.mp4}"
echo -n 'Click on the window you wish to record...'
tmpfile=$(mktemp /tmp/screencast.XXXXXX)
trap "rm -f $tmpfile" 0 1 3 15
xwininfo > $tmpfile
echo 'done.'
origin=$(awk '/Corners:/ { print $2 }' $tmpfile | sed 's/\(+[[:digit:]]*\)+/\1,/')
width=$(awk '/Width:/ { print $2 }' $tmpfile)
height=$(awk '/Height:/ { print $2 }' $tmpfile)
rm -f $tmpfile
trap - 0 1 3 15
echo "Recording to ${outfile}..."
ffmpeg -video_size "${width}x${height}" -framerate 24 -f x11grab -i ":0${origin}" -preset ultrafast -qp 0 -y "$outfile"
@gwollman
Copy link
Author

Apparently both Android and Firefox object to native 4:4:4 chroma format that the x11grab driver generates; you'll probably want to postprocess the video anyway for better compression, so add -pix_fmt yuv420p if compatibility matters to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment