Skip to content

Instantly share code, notes, and snippets.

@sieste
Created March 21, 2020 00:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sieste/64d9df7e3e86ef183a3bf1a5f929a613 to your computer and use it in GitHub Desktop.
Save sieste/64d9df7e3e86ef183a3bf1a5f929a613 to your computer and use it in GitHub Desktop.
screencast: A bash script to simultaneously record audio from mic and video from screen
#!/bin/bash
#
# Record screen and microphone audio
# Stefan Siegert 2020, adapted from code by Steven Gordon
#
# Example usage:
# screencast intro-to-datacomms
# The audio and screen will be recorded. Press 'z' to stop.
# Two files will be created: intro-to-datacomms-audio.flac, intro-to-datacomms-screen.mp4
# Add a 5 second delay before starting.
echo -n "Starting in "
for i in 5 4 3 2 1
do
echo -n " $i"
sleep 1
done
echo "Press z to finish..."
# Basename of file. Separate extensions for audio and screen are added
# E.g. internet-lecture
outfile=$1
# Name and default options for audio output
audioFile=${outfile}-audio.flac
audioRate=44100
audioBits=16
audioChannels=1
# options for screen output
# H.265 video codec and options for fast recording
screenFile=${outfile}-screen.mp4
screenSize=2560x1440
screenOffset=' '
screenRate=10;
screenCodec=libx265
screenOptions=" -preset ultrafast -crf 25" # higher crf worse quality
# Record the microphone audio using SoX, putting process in background,
# and get the process ID (so we can kill it later)
rec -q -r ${audioRate} -b ${audioBits} -c ${audioChannels} ${audioFile} &
audioPID=$!
# Record the screen using ffmpeg, putting the process in background,
# get the process ID (so we can kill it later)
ffmpeg -v 0 -f x11grab -r ${screenRate} -s ${screenSize} -i :0.0+0,0 -vcodec ${screenCodec} ${screenOptions} ${screenFile} &
screenPID=$!
# Enter loop reading input from terminal, stop when 'z' key pressed
op="s"
while [ ${op} != "z" ]
do
read -n1 op
done
# Kill the SoX and ffmpeg processes, which causes them to stop recording
kill ${audioPID} ${screenPID}
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment