screencast.sh is a quick and dirty video screen recording script
[einar-bin] / screencast.sh
1 #!/bin/bash
2 # WARNING: This is a terribly dirty and messy shellscript, written over a couple of late nights
3 # There was alcohol
4 # You probably need to install all the gstreamer-plugins (at least -ugly and -ffmpeg) to get this to work
5 # I wanted to make mp4 with x264, I didn't bother with free codecs
6
7 # This script allows you to create screencasts on Linux, with optional sound inputs (both Mic and system sounds)
8 # You also get to choose to record from the whole screen or just a specified window. It has only been tested
9 # on Fedora 18.
10
11 # Written by Einar Jørgen Haraldseid (http://einar.slaskete.net)
12 # License: http://sam.zoy.org/wtfpl/COPYING
13
14 # Get device names and pretty names for playback sinks:
15 PLAYSINKLIST=$(pacmd list-sinks | grep -e "name: " -e "device.description = " | cut -d " " -f "2-" | sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
16
17 # Display playback monitor chooser
18 PLAYMON=$(echo "${PLAYSINKLIST}
19 none
20 Don't capture system sounds" | zenity --list --title "Choose Output Device" --text "Choose a sound device to capture system sound from:" --column "device" --column "Name" --print-column=1 --hide-column=1 2>/dev/null)
21
22 if [ -z ${PLAYMON} ]; then
23 echo "No choice made on output device, assuming cancel."
24 exit 1
25 fi
26
27 if [ ${PLAYMON} != "none" ]; then
28 # Unmute monitor of the playback sink (if set):
29 PLAYMON="${PLAYMON}.monitor"
30
31 pacmd set-source-mute ${PLAYMON} false >/dev/null
32 fi
33
34 # Get device names and pretty names for microphones:
35 MICLIST=$(pacmd list-sources | grep -e "name: " -e "device.description = " | grep -v -i "monitor" | cut -d " " -f "2-" | sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
36
37 # Display device chooser
38 MIC=$(echo "${MICLIST}
39 none
40 Don't use a microphone" | zenity --list --title "Choose Microphone" --text "Choose a microphone to capture voice from:" --column "device" --column "Name" --print-column=1 --hide-column=1 2>/dev/null)
41
42 if [ -z ${MIC} ]; then
43 echo "No choice made on microphone, assuming cancel."
44 exit 1
45 fi
46
47 # Get target window for recording:
48 TARGET=$(echo "root
49 Whole screen
50 window
51 Specific window" | zenity --list --title "Choose recording mode" --text "Do you want to record the whole screen, or record a specific window?" --column "target" --column "Mode" --print-column=1 --hide-column=1 2>/dev/null)
52
53 if [ -z ${TARGET} ]; then
54 echo "No choice for recording target, assuming cancel."
55 exit 1
56 fi
57
58 if [ ${TARGET} = "root" ]; then
59 echo "Root chosen"
60 XWININFO=$(xwininfo -root)
61 else
62 echo "custom chosen"
63 XWININFO=$(xwininfo)
64 fi
65
66 # Get Window ID and dimensions, make sure dimensions are divisible by two, or else the encoder will fail
67 WID=$(echo "${XWININFO}" | grep "Window id:" | awk '{print $4}')
68 WIDTH=$(echo "${XWININFO}" | grep "Width: " | cut -d ":" -f 2 | awk '{print $1+$1%2}')
69 HEIGHT=$(echo "${XWININFO}" | grep "Height: " | cut -d ":" -f 2 | awk '{print $1+$1%2}')
70
71 # Calculate a suitable bitrate based on window dimensions
72 BITRATE=$(echo "${WIDTH} * ${HEIGHT} * 0.006" | bc | cut -d "." -f 1 )
73
74 # Enable inputs as suitable
75 if [ ${PLAYMON} != "none" ]; then
76 MONITORARG="pulsesrc device=${PLAYMON} slave-method=0 provide-clock=false ! audiorate ! audioconvert ! ffenc_aac bitrate=256000 ! queue2 ! mux."
77 fi
78 if [ ${MIC} != "none" ]; then
79 MICARG="pulsesrc device=${MIC} slave-method=0 provide-clock=false ! audiorate ! audioconvert ! ffenc_aac bitrate=256000 ! queue2 ! mux."
80 fi
81
82 # Launch gstreamer
83 gst-launch -e ximagesrc xid="${WID}" do-timestamp=1 use-damage=0 ! video/x-raw-rgb,framerate=30/1 \
84 ! ffmpegcolorspace ! videoscale method=0 ! video/x-raw-yuv,width=${WIDTH},height=${HEIGHT} \
85 ! x264enc speed-preset=ultrafast bitrate=${BITRATE} ! queue2 \
86 ! mp4mux name="mux" ! filesink location="output.mp4" $MONITORARG $MICARG
87