Script for å beregne soloppgang og solnedgang, til bruk med raspistill
[einar-bin] / screencast-vp8.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 #
6 # Further tweaks and porting to free codecs done one early Saturday morning
7 # in May. There was no coffee … and very little experience with vp8
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 # It dumps the recording in your $HOME directory with a filename like
14 # screencast-YYYY-MM-DD-HH-MM-SS.mp4
15 #
16 # Written by Einar Jørgen Haraldseid (http://einar.slaskete.net)
17 # License: http://sam.zoy.org/wtfpl/COPYING
18
19 # Get device names and pretty names for enumerating playback sinks:
20 PLAYSINKLIST=$(pacmd list-sinks | \
21 grep -e "name: " -e "device.description = " | cut -d " " -f "2-" | \
22 sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
23
24 # Display playback monitor chooser
25 PLAYMON=$(echo "${PLAYSINKLIST}
26 none
27 Don't capture system sounds" | \
28 zenity --list --title "Choose Output Device" \
29 --text "Choose a sound device to capture system sound from:" \
30 --column "device" --column "Name" --print-column=1 \
31 --hide-column=1 2>/dev/null)
32
33 # Catch cancel
34 if [ -z ${PLAYMON} ]; then
35 echo "No choice made on output device, assuming cancel."
36 exit 1
37 fi
38
39 if [ ${PLAYMON} != "none" ]; then
40 # Unmute monitor of the playback sink (if set):
41 PLAYMON="${PLAYMON}.monitor"
42 pacmd set-source-mute ${PLAYMON} false >/dev/null
43 echo "Recording system sounds from ${PLAYMON}"
44 else
45 echo "Not recording system sounds."
46 fi
47
48 # Get device names and pretty names for microphones:
49 MICLIST=$(pacmd list-sources | \
50 grep -e "name: " -e "device.description = " | \
51 grep -v -i "monitor" | cut -d " " -f "2-" | \
52 sed -e "s/name: \|= //g" -e "s/<\|>\|\x22//g")
53
54 # Display device chooser
55 MIC=$(echo "${MICLIST}
56 none
57 Don't use a microphone" | \
58 zenity --list --title "Choose Microphone" \
59 --text "Choose a microphone to capture voice from:" \
60 --column "device" --column "Name" --print-column=1 \
61 --hide-column=1 2>/dev/null)
62
63 if [ -z ${MIC} ]; then
64 echo "No choice made on microphone, assuming cancel."
65 exit 1
66 fi
67
68 if [ ${MIC} != "none" ]; then
69 echo "Recording voice from ${MIC}"
70 else
71 echo "Not recording voice."
72 fi
73
74 # Get target window for recording:
75 TARGET=$(echo "root
76 Whole screen
77 window
78 Specific window" | \
79 zenity --list --title "Choose recording mode" \
80 --text "Do you want to record the whole screen,\
81 or record a specific window?" --column "target" \
82 --column "Mode" --print-column=1 --hide-column=1 2>/dev/null)
83
84 if [ -z ${TARGET} ]; then
85 echo "No choice for recording target, assuming cancel."
86 exit 1
87 fi
88
89 if [ ${TARGET} = "root" ]; then
90 echo "Root window chosen."
91 XWININFO=$(xwininfo -root)
92 else
93 echo "Custom window chosen."
94 XWININFO=$(xwininfo)
95 fi
96
97 # Get Window ID and dimensions, make sure X and Y dimensions are
98 # divisible by two, or else the encoder might fail
99 WID=$(echo "${XWININFO}" | grep "Window id:" | awk '{print $4}')
100 WIDTH=$(echo "${XWININFO}" | grep "Width: " | \
101 cut -d ":" -f 2 | awk '{print $1+$1%2}')
102 HEIGHT=$(echo "${XWININFO}" | grep "Height: " | \
103 cut -d ":" -f 2 | awk '{print $1+$1%2}')
104
105 # Calculate a suitable bitrate based on window dimensions
106 BITRATE=$(echo "${WIDTH} * ${HEIGHT} * 0.85" | bc | cut -d "." -f 1 )
107
108 # Set file name.
109 FILENAME="screencast-$(date +%F-%H-%M-%S).webm"
110
111 # Enable inputs as suitable
112 if [ ${PLAYMON} != "none" ]; then
113 MONITORARG="pulsesrc device=${PLAYMON} slave-method=0 provide-clock=false \
114 ! audiorate ! audioconvert ! vorbisenc ! queue2 ! mux."
115 fi
116 if [ ${MIC} != "none" ]; then
117 MICARG="pulsesrc device=${MIC} slave-method=0 provide-clock=false \
118 ! audiorate ! audioconvert ! vorbisenc ! queue2 ! mux."
119 fi
120
121 # Launch gstreamer
122 gst-launch-1.0 -q -e ximagesrc xid="${WID}" do-timestamp=1 use-damage=0 \
123 ! video/x-raw,framerate=20/1 ! videoscale method=0 \
124 ! video/x-raw,width=${WIDTH},height=${HEIGHT} ! videoconvert \
125 ! vp8enc end-usage=vbr cpu-used=16 target-bitrate=${BITRATE} \
126 deadline=100 static-threshold=1000 min-quantizer=0 max-quantizer=63 ! queue2 \
127 ! webmmux name="mux" \
128 ! filesink location="${HOME}/${FILENAME}" ${MONITORARG} ${MICARG}
129
130 echo "Recording done, file is ${FILENAME}"