t004-screencasts.txt - adamsgaard.dk - my academic webpage
 (HTM) git clone git://src.adamsgaard.dk/adamsgaard.dk
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       t004-screencasts.txt (4139B)
       ---
            1 On Monday 2020-03-16 the buildings of the danish public sector were
            2 closed as an emergency response to COVID-19. This includes Aarhus
            3 University where I teach two undergraduate courses. The university
            4 asks lecturers to move their teaching to digital platforms. As many
            5 times before, this requires creative thinking for those of us who
            6 do not use Microsoft and Apple products.
            7 
            8 I needed a way to record my pdf slideshows while talking over the
            9 presentation.  Ideally, I also wanted the ability to show the video
           10 of my webcam as an overlay in an attempt to make the presentation
           11 a bit more engaging when explaining complex parts.
           12 
           13 Fortunately, [1]ffmpeg(1) makes it easy to record the screen and
           14 laptop audio.  I want to keep the fan noise low during recording
           15 by applying minimal compression and encoding. The following shell
           16 script serves the purpose of starting and stopping recording:
           17 
           18     #!/bin/sh
           19     lockfile=/tmp/screenrecord.pid
           20 
           21     startrecording() {
           22         out="$HOME/screenrecord-$(date '+%Y-%m-%d_%H:%M:%S').mkv"
           23         ffmpeg -y \
           24             -f x11grab \
           25             -framerate 60 \
           26             -s "$(xdpyinfo | grep dimensions | awk '{print $2}')" \
           27             -i $DISPLAY \
           28             -f sndio -i default \
           29             -r 30 \
           30             -c:v libx264rgb -crf 0 -preset ultrafast -c:a flac \
           31             "$out" >/dev/null 2>&1 &
           32         printf '%s' "$!" > "$lockfile"
           33 
           34         sleep 1
           35         if [ ! -f "$out" ]; then
           36             echo 'error: ffmpeg recording did not start' >&2
           37             notify-send -u CRITICAL "${0##*/}" 'ffmpeg recording did not start'
           38             rm -f "$lockfile"
           39             exit 1
           40         fi
           41     }
           42 
           43     stoprecording() {
           44         kill "$(cat "$lockfile")"
           45         rm -f "$lockfile"
           46         notify-send "${0##*/}" 'recording ended'
           47     }
           48 
           49     if [ -f "$lockfile" ]; then
           50         stoprecording
           51     else
           52         startrecording
           53     fi
           54 
           55 I have bound the above script to the key binding Alt+r which makes
           56 it easy to start and stop recording in my X session.
           57 
           58 On Linux systems, the sound driver sndio should be replaced by alsa
           59 in the above ffmpeg(1) command.  Audio recording is disabled by
           60 default on OpenBSD, but can be permanently enabled with the following
           61 commands:
           62 
           63     # sysctl kern.audio.record=1
           64     # echo kern.audio.record=1 >> /etc/sysctl.conf
           65 
           66 On OpenBSD I can show the webcam video feed with the [2]video(1)
           67 command. The following script toggles the video feed:
           68 
           69     #!/bin/sh
           70     # remember to `chown $USER /dev/video0`
           71     if pgrep video >/dev/null 2>&1; then
           72         pkill video
           73     else
           74         nohup video -s 320 >/dev/null 2>&1 &
           75     fi
           76 
           77 On Linux, the command mpv /dev/video0 can take place of the video(1)
           78 command above. I have the above script bound to the keybinding Alt+v
           79 so I can quickly show and hide my face while recording.
           80 
           81 I set [3]dwm(1), my window manager, to open the video feed as a
           82 floating window on the bottom right of the screen.  The full dwm
           83 configuration can be found [4]here.
           84 
           85 When I am done recording a lecture, I encode and compress the video
           86 file to save bandwidth during upload. The following script encodes
           87 all input files and reduces file size to roughly 15% without
           88 concievable loss in quality:
           89 
           90     #!/bin/sh
           91 
           92     encode() {
           93         ffmpeg -y -i "$1" \
           94             -c:v libx264 -threads 0 -preset faster -pix_fmt yuv420p \
           95             -c:a aac -crf 10 \
           96             "${1%.*}_out.mp4"
           97     }
           98 
           99     for f in "$@"; do
          100         encode "$f"
          101     done
          102 
          103 If there is a delay between video and audio, this can also be
          104 adjusted using ffmpeg(1).  I correct for a 0.3 s delay that I
          105 encounter when recording on my laptop:
          106 
          107     #!/bin/sh
          108 
          109     synchronize() {
          110         ffmpeg -y -i "$1" \
          111             -itsoffset 0.300 \
          112             -i "$1" \
          113             -map 0:v -map 1:a \
          114             -c copy \
          115             "${1%.*}_out.${1##*.}"
          116     }
          117 
          118     for f in "$@"; do
          119         synchronize "$f"
          120     done
          121 
          122 [5]Example screen recording using ffmpeg(1) and video(1) with the
          123 above scripts.
          124 
          125 References:
          126 
          127 [1] https://ffmpeg.org/
          128 [2] https://man.openbsd.org/man1/video.1
          129 [3] https://dwm.suckless.org/
          130 [4] https://src.adamsgaard.dk/dwm/
          131 [5] https://adamsgaard.dk/video/screencast.mp4