Skip to content

Instantly share code, notes, and snippets.

@computercam
Last active February 28, 2024 17:30
Show Gist options
  • Save computercam/904e33e1ddc62da4dcca7bf5c6688006 to your computer and use it in GitHub Desktop.
Save computercam/904e33e1ddc62da4dcca7bf5c6688006 to your computer and use it in GitHub Desktop.
Record Screen. Script and cronjob to automate recording osx screen

Requires ffpmeg

brew install ffmpeg

Make sure the script is executable

chmod u+x /Volumes/External/screenshots/record_screen

For regularly scheduled recording use crontab

# sudo crontab -e -u root
*/1 * * * * /Volumes/External/screenshots/record_screen

Giving permission to cron to record

You will also want to give screen recording permissions to cron and screencapture so they can run in the background.

To make the binaries easier to find do the following

cd ~
ln -s `which cron` .
ln -s `which screencapture` .

Now go to: System Preferences > Security & Privacy > Screen Recording Now use the + button and add the binaries you just created links for.

Encrypt and compress

If you want to encrypt and compress the recordings, use the v2 of the script

To decrypt

gpg -d [filename].tar.gz.gpg | tar xzvf - --strip-components=1
#!/bin/sh
OUTPATH=/Volumes/External/screenshots/
SECONDS=59
CODEC=h264
FORMAT=mp4
FPS=6
TIMESTAMP=`/bin/date +%m_%d_%Y_%H:%M:%S`
SC="/usr/sbin/screencapture"
FFMPEG="/usr/local/bin/ffmpeg"
function record_screen() {
DISPLAY=$1
FILENAME=${TIMESTAMP}__$DISPLAY
DEST="${OUTPATH}${FILENAME}"
($SC -D $DISPLAY -x -V $SECONDS $DEST.mov && $FFMPEG -y -i $DEST.mov -filter:v fps=$FPS -vcodec $CODEC $DEST.$FORMAT && rm $DEST.mov) &
}
record_screen 1
record_screen 2
#!/bin/sh
OUTPATH=/Volumes/External/screenshots/
SECONDS=5
CODEC=h264
FORMAT=mp4
ENCRYPT_FORMAT=".tar.gz.gpg"
FPS=6
TIMESTAMP=`/bin/date +%m_%d_%Y_%H:%M:%S`
SC="/usr/sbin/screencapture"
FFMPEG="/usr/local/bin/ffmpeg"
CHMOD="/bin/chmod"
CHOWN="/usr/sbin/chown"
TAR="/usr/bin/tar"
GPG="/usr/local/bin/gpg"
function fix_perms() {
$CHOWN -R root:wheel $OUTPATH
$CHMOD -R o-rwx $OUTPATH
}
function convert_format() {
local SRC=$1.mov
local DEST=$1.$FORMAT
$FFMPEG -y -i $SRC -filter:v fps=$FPS -vcodec $CODEC $DEST
}
function record_screen() {
local DEST=$1.mov
local DISPLAY=$2
$SC -D $DISPLAY -x -V $SECONDS $DEST
}
function encrypt_file() {
local SRC=$1.$FORMAT
local DEST="${1}${ENCRYPT_FORMAT}"
$TAR czvpf - $SRC | $GPG --passphrase $TIMESTAMP --batch --symmetric --cipher-algo aes256 -o $DEST
}
function remove_unencrypted() {
local MP4="${1}.mp4"
local MOV="${1}.mov"
rm $MP4
rm $MOV
}
function run() {
local DISPLAY=$1
local FILENAME=${TIMESTAMP}__$DISPLAY
local DEST="${OUTPATH}${FILENAME}"
(record_screen $DEST $DISPLAY && convert_format $DEST && encrypt_file $DEST && remove_unencrypted $DEST && fix_perms) &
}
run 1
run 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment