Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.