Cooler incrementation of the FAIL counter
[einar-bin] / screencast-ts.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
5 # to work. This version encodes h264 video and aac sound(s) to an MPEG4 Transport Stream file.
6 #
7 # So this script allows you to create screencasts on Linux, with optional sound inputs (both Mic
8 # and system sounds). It stores system sounds and mic in separate audio streams.
9 # You also get to choose to record from the whole screen or just a specified window. It has only
10 # been tested on Fedora 18.
11 #
12 # It dumps the recording in your $HOME directory with filename screencast-YYYY-MM-DD-HH-MM-SS.ts
13 #
14 # Written by Einar Jørgen Haraldseid (http://einar.slaskete.net)
15 # License: http://sam.zoy.org/wtfpl/COPYING
16
17 # Get device names and pretty names for playback sinks:
18 PLAYSINKLIST=$(pacmd list-sinks | grep -e "name: " -e "device.description = " | cut -d " " -f "2-" | sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
19
20 # Display playback monitor chooser
21 PLAYMON=$(echo "${PLAYSINKLIST}
22 none
23 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)
24
25 if [ -z ${PLAYMON} ]; then
26 echo "No choice made on output device, assuming cancel."
27 exit 1
28 fi
29
30 if [ ${PLAYMON} != "none" ]; then
31 # Unmute monitor of the playback sink (if set):
32 PLAYMON="${PLAYMON}.monitor"
33
34 pacmd set-source-mute ${PLAYMON} false >/dev/null
35 fi
36
37 # Get device names and pretty names for microphones:
38 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")
39
40 # Display device chooser
41 MIC=$(echo "${MICLIST}
42 none
43 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)
44
45 if [ -z ${MIC} ]; then
46 echo "No choice made on microphone, assuming cancel."
47 exit 1
48 fi
49
50 # Get target window for recording:
51 TARGET=$(echo "root
52 Whole screen
53 window
54 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)
55
56 if [ -z ${TARGET} ]; then
57 echo "No choice for recording target, assuming cancel."
58 exit 1
59 fi
60
61 if [ ${TARGET} = "root" ]; then
62 echo "Root chosen"
63 XWININFO=$(xwininfo -root)
64 else
65 echo "custom chosen"
66 XWININFO=$(xwininfo)
67 fi
68
69 # Get Window ID and dimensions, make sure dimensions are divisible by two, or else the encoder will fail
70 WID=$(echo "${XWININFO}" | grep "Window id:" | awk '{print $4}')
71 WIDTH=$(echo "${XWININFO}" | grep "Width: " | cut -d ":" -f 2 | awk '{print $1+$1%2}')
72 HEIGHT=$(echo "${XWININFO}" | grep "Height: " | cut -d ":" -f 2 | awk '{print $1+$1%2}')
73
74 # Calculate a suitable bitrate based on window dimensions
75 BITRATE=$(echo "${WIDTH} * ${HEIGHT} * 0.006" | bc | cut -d "." -f 1 )
76
77 # Set file name.
78 FILENAME="screencast-$(date +%F-%H-%M-%S).ts"
79
80 # Enable inputs as suitable
81 if [ ${PLAYMON} != "none" ]; then
82 MONITORARG="pulsesrc device=${PLAYMON} slave-method=0 provide-clock=false ! audiorate ! audioconvert ! ffenc_aac bitrate=256000 ! queue2 ! mux."
83 fi
84 if [ ${MIC} != "none" ]; then
85 MICARG="pulsesrc device=${MIC} slave-method=0 provide-clock=false ! audiorate ! audioconvert ! ffenc_aac bitrate=256000 ! queue2 ! mux."
86 fi
87
88 # Launch gstreamer
89 gst-launch -e ximagesrc xid="${WID}" do-timestamp=1 use-damage=0 ! video/x-raw-rgb,framerate=30/1 \
90 ! ffmpegcolorspace ! videoscale method=0 ! video/x-raw-yuv,width=${WIDTH},height=${HEIGHT} \
91 ! x264enc speed-preset=ultrafast bitrate=${BITRATE} ! queue2 \
92 ! h264parse ! mpegtsmux name="mux" ! filesink location="${HOME}/${FILENAME}" $MONITORARG $MICARG
93