From 513d27830de1ae41cf1709c3d5215c3b025d2461 Mon Sep 17 00:00:00 2001 From: foo-dogsquared Date: Sat, 26 Oct 2019 20:52:52 +0800 Subject: [PATCH] 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. --- .scripts/screen-record.sh | 95 +++++++++++++++++---------------------- i3/config | 3 +- 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/.scripts/screen-record.sh b/.scripts/screen-record.sh index b0b7be6..8c9cc95 100644 --- a/.scripts/screen-record.sh +++ b/.scripts/screen-record.sh @@ -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 -# programs along with the indicated version: -# OBS Studio - 23.2.1-2 -# xdotool - version 3.20160805.1 -# echo (part of coreutils 8.31) -# notify-send - 0.7.8 +# Minimum requirements: +# awk - GNU Awk v5.0.1 +# date - v8.31 GNU implentation +# ffmpeg - version n4.2.1; built with GCC; based from the Arch Linux repo +# slop - v7.4 +# xwininfo - v1.1.5 -# Having a notify-send means you also need to -# have a notification server (such as dunst) to be up at the time. +# Having used `xwininfo`, it needs to have the legacy graphics stack (X11-based) on Linux function error_cleanup() { # rm "$pic_filepath" @@ -18,86 +19,72 @@ function error_cleanup() { } 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 -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. +This is more reliable than OBS Studio (since I don't how to fully utilize it yet). -Usage: $0 [-o/--output ] [-s/--select] -[-d/--delay ] [--help] +Usage: $0 [-o/--output ] [-s/--selection] Options: -h, --help - show the help section --p, --profile - indicate the profile (default: 'Untitled') --s, --scene - the scene to be used (default: 'Untitled') +-o, --output - the path of the output (default: '~/Videos') " # setting up a exit trap in case of error trap 'error_cleanup $LINENO' ERR +OUTPUT=${VIDEOS:-"$HOME/Videos"} + while [[ $# -gt 0 ]] do case $1 in -h|--help) echo "$help_section" exit 0;; - -p|--profile) - PROFILE="$2" + -o|--output) + OUTPUT="$2" shift shift;; - -s|--scene) - SCENE="$2" - shift;; *) shift;; esac done -RECORDING_FILE="/tmp/currently-recording" -OBS_RECORDING_HOTKEY="shift+r" +# Constants +RECORDING_FILE="/tmp/fds-ffmpeg-currently-recording"; -# The program simply checks for the 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 - obs_command+="--profile $PROFILE " - fi + date_format=$(date +%F-%H-%M-%S) + + recording_command="ffmpeg -y -f x11grab -s ${width}x${height} -i :0.0+${pos_x},$pos_y ${OUTPUT}/$date_format.mkv -nostdin" - if [[ -n "$SCENE" ]]; then - obs_command+="--scene $SCENE " - fi - - eval $obs_command & + $recording_command & + WINDOW_ID="$!" + RETURN_CODE="$?" sleep 1; - if [[ $? != 0 ]]; then - notify-send "Recording starting failed" "Did not successfully started recording." + if [[ "$RETURN_CODE" != 0 ]]; then + notify-send "Recording start has failed"; exit 1; fi - WINDOW_ID=$(xdotool search --name "OBS [[:digit:]]+" | head -n1) - notify-send "Recording started." "Found a OBS Studio window instance with the window ID $WINDOW_ID." --expire-time=1000 - - touch $RECORDING_FILE - echo $WINDOW_ID >> $RECORDING_FILE -else - current_window_id=$(xdotool getactivewindow) + notify-send "Recording started successfully" "Process ID is at "$WINDOW_ID""; + touch "$RECORDING_FILE"; + echo "$WINDOW_ID" >> $RECORDING_FILE; +else WINDOW_ID=$(<"$RECORDING_FILE") - xdotool windowkill $WINDOW_ID - - if [[ $? != 0 ]]; then - notify-send "Recording stop process failed" "There's a problem with stopping recording. Might want to manually stop the recording yourself." + kill $WINDOW_ID + + if [[ $? != 0 ]]; then + notify-send "Recording stop failed" "There's a problem while trying to kill the process. Process ID is at $WINDOW_ID"; exit 1; fi - notify-send "Recording ended." "The recording should be cancelled now." - xdotool windowactivate $current_window_id + notify-send "Recording stop successful" rm "$RECORDING_FILE" -fi +fi \ No newline at end of file diff --git a/i3/config b/i3/config index cb82387..7253550 100644 --- a/i3/config +++ b/i3/config @@ -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 # Screen record (Alt + Print) -bindsym $mod+Print exec ~/.scripts/screen-record.sh --scene barless-screen -bindsym $mod+Shift+Print exec ~/.scripts/screen-record.sh --scene fullscreen +bindsym $mod+Print exec ~/.scripts/ffmpeg-screen-record.sh --output ~/recordings # Define names for default workspaces for which we configure key bindings later on. # We use variables to avoid repeating the names in multiple places.