Overskrev en fin patch ved et uhell, la inn igjen
[einar-bin] / screencast.sh
1 #!/bin/bash
2
3 # WARNING: This is a terribly dirty and messy shellscript, written over a
4 # couple of late nights. There was alcohol …
5 # You probably need to install all the gstreamer-plugins (at least -ugly
6 # and -ffmpeg) as well as gstreamer-tools to get this to work. I wanted to
7 # make mp4 files with x264, I didn't bother with free codecs.
8 #
9 # So this script allows you to create screencasts on Linux, with optional
10 # sound inputs (both Microphone and system sounds). It stores system sounds
11 # and microphone in separate audio streams.
12 #
13 # You also get to choose to record from the whole screen or just a specified
14 # window. It has been tested on Fedora 18, Ubuntu 13.04 and CrunchBang Waldorf
15 # (and thus should work on Debian Wheezy as well).
16 #
17 # It dumps the recording in your $HOME directory with a filename like
18 # screencast-YYYY-MM-DD-HH-MM-SS.mp4
19 #
20 # Written by Einar Jørgen Haraldseid (http://einar.slaskete.net)
21 # License: http://sam.zoy.org/wtfpl/COPYING
22
23 # Get device names and pretty names for enumerating playback sinks:
24 PLAYSINKLIST=$(pacmd list-sinks | \
25 grep -e "name: " -e "device.description = " | cut -d " " -f "2-" | \
26 sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
27
28 # Display playback monitor chooser
29 PLAYMON=$(echo "${PLAYSINKLIST}
30 none
31 Don't capture system sounds" | \
32 zenity --list --title "Choose Output Device" \
33 --text "Choose a sound device to capture system sound from:" \
34 --column "device" --column "Name" --print-column=1 \
35 --hide-column=1 2>/dev/null)
36
37 # Catch cancel
38 if [ -z ${PLAYMON} ]; then
39 echo "No choice made on output device, assuming cancel."
40 exit 1
41 fi
42
43 if [ ${PLAYMON} != "none" ]; then
44 # Unmute monitor of the playback sink (if set):
45 PLAYMON="${PLAYMON}.monitor"
46 pacmd set-source-mute ${PLAYMON} false >/dev/null
47 echo "Recording system sounds from ${PLAYMON}"
48 else
49 echo "Not recording system sounds."
50 fi
51
52 # Get device names and pretty names for microphones:
53 MICLIST=$(pacmd list-sources | \
54 grep -e "name: " -e "device.description = " | \
55 grep -v -i "monitor" | cut -d " " -f "2-" | \
56 sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
57
58 # Display device chooser
59 MIC=$(echo "${MICLIST}
60 none
61 Don't use a microphone" | \
62 zenity --list --title "Choose Microphone" \
63 --text "Choose a microphone to capture voice from:" \
64 --column "device" --column "Name" --print-column=1 \
65 --hide-column=1 2>/dev/null)
66
67 if [ -z ${MIC} ]; then
68 echo "No choice made on microphone, assuming cancel."
69 exit 1
70 fi
71
72 if [ ${MIC} != "none" ]; then
73 echo "Recording voice from ${MIC}"
74 else
75 echo "Not recording voice."
76 fi
77
78 # Get target window for recording:
79 TARGET=$(echo "root
80 Whole screen
81 window
82 Specific window" | \
83 zenity --list --title "Choose recording mode" \
84 --text "Do you want to record the whole screen,\
85 or record a specific window?" --column "target" \
86 --column "Mode" --print-column=1 --hide-column=1 2>/dev/null)
87
88 if [ -z ${TARGET} ]; then
89 echo "No choice for recording target, assuming cancel."
90 exit 1
91 fi
92
93 if [ ${TARGET} = "root" ]; then
94 echo "Root window chosen."
95 XWININFO=$(xwininfo -root)
96 else
97 echo "Custom window chosen."
98 XWININFO=$(xwininfo)
99 fi
100
101 # Get Window ID and dimensions, make sure X and Y dimensions are
102 # divisible by two, or else the encoder will fail
103 WID=$(echo "${XWININFO}" | grep "Window id:" | awk '{print $4}')
104 WIDTH=$(echo "${XWININFO}" | grep "Width: " | \
105 cut -d ":" -f 2 | awk '{print $1+$1%2}')
106 HEIGHT=$(echo "${XWININFO}" | grep "Height: " | \
107 cut -d ":" -f 2 | awk '{print $1+$1%2}')
108
109 # Calculate a suitable bitrate based on window dimensions
110 BITRATE=$(echo "${WIDTH} * ${HEIGHT} * 0.0075" | bc | cut -d "." -f 1 )
111
112 # Set file name.
113 FILENAME="screencast-$(date +%F-%H-%M-%S).mp4"
114
115 # Determine if we have ffenc_aac or avenc_aac available
116 AACENC="ffenc_aac"
117 if gst-inspect-1.0 --exists avenc_aac; then
118 AACENC="avenc_aac"
119 fi
120
121 # Enable inputs as suitable
122 if [ ${PLAYMON} != "none" ]; then
123 MONITORARG="pulsesrc device=${PLAYMON} slave-method=0 provide-clock=false \
124 ! audiorate ! audioconvert ! ${AACENC} bitrate=256000 ! queue2 ! mux."
125 fi
126 if [ ${MIC} != "none" ]; then
127 MICARG="pulsesrc device=${MIC} slave-method=0 provide-clock=false \
128 ! audiorate ! audioconvert ! ${AACENC} bitrate=256000 ! queue2 ! mux."
129 fi
130
131 # Launch gstreamer
132 gst-launch-1.0 -q -e ximagesrc xid="${WID}" do-timestamp=1 use-damage=0 \
133 ! video/x-raw,framerate=30/1 ! videoscale method=0 \
134 ! video/x-raw,width=${WIDTH},height=${HEIGHT} ! videoconvert \
135 ! x264enc speed-preset=veryfast bitrate=${BITRATE} ! queue2 \
136 ! mp4mux name="mux" \
137 ! filesink location="${HOME}/${FILENAME}" ${MONITORARG} ${MICARG}
138
139 echo "Recording done, file is ${FILENAME}"