Skip to content

Instantly share code, notes, and snippets.

@AntumDeluge
Last active May 21, 2021 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntumDeluge/75f63661c75cdc77c3497a3515b1b2fe to your computer and use it in GitHub Desktop.
Save AntumDeluge/75f63661c75cdc77c3497a3515b1b2fe to your computer and use it in GitHub Desktop.
Shell script for recording my desktop with FFmpeg (Windows 10 w/ MSYS2/BASH shell).
#!/bin/bash
# This script can be re-distributed under the CC0 license (public domain).
#
# It is created specifically for my system (Windows 10 with MSYS2/BASH shell).
# If you want to use it on you may need to change certain values such as the
# audio device name.
# defines whether or not mouse is drawn
MOUSE=1
# defines whether audio is recorded from microphone
MIC=0
# video capture framerate
FPS=60
# command line arguments
IDX="0"
while [ "${IDX}" -lt $# ]; do
IDX=$[$IDX+1]
ARG=${!IDX}
if [ "${ARG}" == "-title" ]; then
if [ $# -gt "${IDX}" ]; then
IDX=$[$IDX+1]
TITLE=${!IDX}
else
echo -e "ERROR: \"title\" argument requires a value"
exit 1
fi
elif [ "${ARG}" == "-fps" ]; then
if [ $# -gt "${IDX}" ]; then
IDX=$[$IDX+1]
FPS=${!IDX}
else
echo -e "ERROR: \"fps\" argument requires a value"
exit 1
fi
elif [ "${ARG}" == "-nomouse" ]; then
MOUSE=0
elif [ "${ARG}" == "-mic" ]; then
MIC=1
fi
done
if [ ! -z "${TITLE}" ]; then
# window title set
echo "Capturing window \"${TITLE}\""
VINPUT="title=${TITLE}"
else
echo "Capturing entire screen"
VINPUT="desktop"
fi
OUTNAME=$(echo "/c/users/antum/Videos/Captures/output-$(date +%Y-%m-%d_%T.%N)" | sed -e 's|:|.|g' )
OUTVID="${OUTNAME}-video.mkv"
OUTAUD="${OUTNAME}-audio.wav"
LOGAUD="${OUTNAME}-audio.log"
OUTFINAL="${OUTNAME}.mkv"
SYSAUDIO="Stereo Mix (Realtek High Definition Audio)"
MICAUDIO="Microphone (Realtek High Definition Audio)"
# capture audio in separate process to prevent sync issues
if [ "${MIC}" -gt "0" ]; then
# FIXME: stereo mix & mic don't sound good together or only mic is being recorded
#ffmpeg -hide_banner -rtbufsize 500M -f dshow -i audio="${SYSAUDIO}" -f dshow -i audio="${MICAUDIO}" -filter_complex amerge=inputs=2 -c:a pcm_s16le -ac 1 "${OUTAUD}" > "${LOGAUD}" 2>&1 & APID=$!
ffmpeg -hide_banner -rtbufsize 500M -thread_queue_size 512 -f dshow -ac 1 -i audio="${MICAUDIO}" -c:a pcm_s16le "${OUTAUD}" > "${LOGAUD}" 2>&1 & APID=$!
else
ffmpeg -hide_banner -rtbufsize 500M -thread_queue_size 512 -f dshow -ac 1 -i audio="${SYSAUDIO}" -c:a pcm_s16le "${OUTAUD}" > "${LOGAUD}" 2>&1 & APID=$!
fi
# video
ffmpeg -hide_banner -rtbufsize 1500M -thread_queue_size 512 -f gdigrab -framerate ${FPS} -draw_mouse ${MOUSE} -i "${VINPUT}" -c:v libx264 -preset ultrafast -qp 0 -x264opts keyint=1 "${OUTVID}"
VIDRET=$?
kill -s SIGINT ${APID}
if [ "${VIDRET}" -eq "0" ]; then
echo -e "\nMuxing audio and video ..."
# Join audio & video
ffmpeg -hide_banner -i "${OUTVID}" -i "${OUTAUD}" -map 0:0 -map 1:0 -c copy "${OUTFINAL}"
if [ "$?" -eq "0" ]; then
rm "${OUTVID}" "${OUTAUD}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment