5737a4c67cefbc8e3ca10cc02bd9ca10d99e61b5
[einar-bin] / ffmpeg-grab-window-with-sound.bash
1 #!/bin/bash
2 # A simple script to grab a screencast of a window using ffmpeg
3 # Once started, select the window you want to record, and start
4 # talking/using the indicated program
5
6 # Select window to grab info from
7 INFO=$(xwininfo -frame)
8
9 # Get the window size
10 WIN_SIZE=$(echo $INFO | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
11 # Get the window position
12 WIN_XY=$(echo $INFO | grep -oEe 'Corners: \+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed -e 's/\+/,/')
13 # Determine window width and window height, and adjust to be divisible by two
14 WIN_WIDTH=$(echo $WIN_SIZE | cut -d "x" -f 1 | awk '{print $1+$1%2}')
15 WIN_HEIGHT=$(echo $WIN_SIZE | cut -d "x" -f 2 | awk '{print $1+$1%2}')
16
17 # Run ffmpeg
18 ffmpeg \
19 -f alsa -ac 1 -i pulse \ # Audio from alsa, using one channel, using the default pulse input device
20 -f x11grab -r 30 -s ${WIN_WIDTH}x${WIN_HEIGHT} -i :0.0+${WIN_XY} \ # Video from X11, framerate 30
21 -vcodec libx264 -vpre lossless_ultrafast \ # Use the libx264 video encoder with the lossless_ultrafast preset
22 -acodec libmp3lame \ # Use the libmp3lame audio encoder
23 ${HOME}/screencast-$(date +%F-%H-%M-%S).mkv # Output to a file in the user's homedir, using current date and time