Replace screen recording implementation mainly with ffmpeg

Finally figured out how to record with ffmpeg without lag or maybe that could've been a case of bad luck because of my hardware.

In any case, I also learned about the  and the here-string. Worth it for me.
This commit is contained in:
foo-dogsquared 2019-10-26 20:52:52 +08:00
parent c5ec2b30df
commit 513d27830d
2 changed files with 42 additions and 56 deletions

View File

@ -1,16 +1,17 @@
#!/bin/sh #!/bin/sh
# Records the screen with OBS Studio. # Records a part of the screen.
# Note this is a toggle command.
# There could only have one recording instance running at a time.
# This script depends on the following # Minimum requirements:
# programs along with the indicated version: # awk - GNU Awk v5.0.1
# OBS Studio - 23.2.1-2 # date - v8.31 GNU implentation
# xdotool - version 3.20160805.1 # ffmpeg - version n4.2.1; built with GCC; based from the Arch Linux repo
# echo (part of coreutils 8.31) # slop - v7.4
# notify-send - 0.7.8 # xwininfo - v1.1.5
# Having a notify-send means you also need to # Having used `xwininfo`, it needs to have the legacy graphics stack (X11-based) on Linux
# have a notification server (such as dunst) to be up at the time.
function error_cleanup() { function error_cleanup() {
# rm "$pic_filepath" # rm "$pic_filepath"
@ -18,86 +19,72 @@ function error_cleanup() {
} }
help_section=" help_section="
Simply captures a recording with OBS Studio. Simply captures a recording with ffmpeg.
Note it requires you to set a hotkey for recording in This is more reliable than OBS Studio (since I don't how to fully utilize it yet).
OBS Studio in order to run this script properly.
The default hotkey set in this script is 'Shift+R'.
If your hotkey is any different,
change the OBS_RECORDING_HOTKEY variable
in this script accordingly.
Usage: $0 [-o/--output <OUTPUT_PATH>] [-s/--select] Usage: $0 [-o/--output <OUTPUT_PATH>] [-s/--selection]
[-d/--delay <SECONDS>] [--help]
Options: Options:
-h, --help - show the help section -h, --help - show the help section
-p, --profile <string> - indicate the profile (default: 'Untitled') -o, --output <string> - the path of the output (default: '~/Videos')
-s, --scene <string> - the scene to be used (default: 'Untitled')
" "
# setting up a exit trap in case of error # setting up a exit trap in case of error
trap 'error_cleanup $LINENO' ERR trap 'error_cleanup $LINENO' ERR
OUTPUT=${VIDEOS:-"$HOME/Videos"}
while [[ $# -gt 0 ]] while [[ $# -gt 0 ]]
do do
case $1 in case $1 in
-h|--help) -h|--help)
echo "$help_section" echo "$help_section"
exit 0;; exit 0;;
-p|--profile) -o|--output)
PROFILE="$2" OUTPUT="$2"
shift shift
shift;; shift;;
-s|--scene)
SCENE="$2"
shift;;
*) *)
shift;; shift;;
esac esac
done done
RECORDING_FILE="/tmp/currently-recording" # Constants
OBS_RECORDING_HOTKEY="shift+r" RECORDING_FILE="/tmp/fds-ffmpeg-currently-recording";
# The program simply checks for the
if [[ ! -f $RECORDING_FILE ]]; then if [[ ! -f $RECORDING_FILE ]]; then
obs_command="obs --startrecording --minimize-to-tray " dimensions=$(slop -f "%x %y %w %h %g %i") || exit 1;
read -r pos_x pos_y width height grid id <<< $dimensions
if [[ -n "$PROFILE" ]]; then date_format=$(date +%F-%H-%M-%S)
obs_command+="--profile $PROFILE "
fi
if [[ -n "$SCENE" ]]; then recording_command="ffmpeg -y -f x11grab -s ${width}x${height} -i :0.0+${pos_x},$pos_y ${OUTPUT}/$date_format.mkv -nostdin"
obs_command+="--scene $SCENE "
fi
eval $obs_command & $recording_command &
WINDOW_ID="$!"
RETURN_CODE="$?"
sleep 1; sleep 1;
if [[ $? != 0 ]]; then if [[ "$RETURN_CODE" != 0 ]]; then
notify-send "Recording starting failed" "Did not successfully started recording." notify-send "Recording start has failed";
exit 1; exit 1;
fi fi
WINDOW_ID=$(xdotool search --name "OBS [[:digit:]]+" | head -n1) notify-send "Recording started successfully" "Process ID is at "$WINDOW_ID"";
notify-send "Recording started." "Found a OBS Studio window instance with the window ID $WINDOW_ID." --expire-time=1000
touch $RECORDING_FILE touch "$RECORDING_FILE";
echo $WINDOW_ID >> $RECORDING_FILE echo "$WINDOW_ID" >> $RECORDING_FILE;
else else
current_window_id=$(xdotool getactivewindow)
WINDOW_ID=$(<"$RECORDING_FILE") WINDOW_ID=$(<"$RECORDING_FILE")
xdotool windowkill $WINDOW_ID kill $WINDOW_ID
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
notify-send "Recording stop process failed" "There's a problem with stopping recording. Might want to manually stop the recording yourself." notify-send "Recording stop failed" "There's a problem while trying to kill the process. Process ID is at $WINDOW_ID";
exit 1; exit 1;
fi fi
notify-send "Recording ended." "The recording should be cancelled now." notify-send "Recording stop successful"
xdotool windowactivate $current_window_id
rm "$RECORDING_FILE" rm "$RECORDING_FILE"
fi fi

View File

@ -158,8 +158,7 @@ bindsym Shift+Print exec ~/.scripts/maim-screenshot.sh --delay 3
bindsym Control+Shift+Print exec ~/.scripts/maim-screenshot.sh --select --delay 3 bindsym Control+Shift+Print exec ~/.scripts/maim-screenshot.sh --select --delay 3
# Screen record (Alt + Print) # Screen record (Alt + Print)
bindsym $mod+Print exec ~/.scripts/screen-record.sh --scene barless-screen bindsym $mod+Print exec ~/.scripts/ffmpeg-screen-record.sh --output ~/recordings
bindsym $mod+Shift+Print exec ~/.scripts/screen-record.sh --scene fullscreen
# Define names for default workspaces for which we configure key bindings later on. # Define names for default workspaces for which we configure key bindings later on.
# We use variables to avoid repeating the names in multiple places. # We use variables to avoid repeating the names in multiple places.