Skip to content

Instantly share code, notes, and snippets.

@eioo
Created August 21, 2024 07:39
Show Gist options
  • Save eioo/488806d0af51baa7cbee83f95c51ec4e to your computer and use it in GitHub Desktop.
Save eioo/488806d0af51baa7cbee83f95c51ec4e to your computer and use it in GitHub Desktop.
Record area of screen with tray icon (Linux Mint/bash)
#!/bin/bash
# This script is used to record area of the screen (using slop) and save it to a file
# Script can be stopped in tray by clicking on the icon
# Dependencies: slop, ffmpeg, xclip, notify-send, yad, nemo
# Options
icon="/usr/share/icons/Papirus/48x48/apps/record-desktop.svg"
dir="$HOME/Videos/recordings"
file="$dir/$(date +%Y-%m-%d_%H-%M-%S).mp4"
notify="notify-send -i $icon -t 3000"
slop="slop -f %x,%y,%w,%h"
# Check if dependencies are installed
if ! command -v slop &> /dev/null || ! command -v ffmpeg &> /dev/null || ! command -v xclip &> /dev/null || ! command -v notify-send &> /dev/null || ! command -v yad &> /dev/null || ! command -v nemo &> /dev/null; then
$notify "Error" "Please install slop, ffmpeg, xclip, notify-send, yad and nemo"
exit 1
fi
# Check if directory exists
if [ ! -d "$dir" ]; then
$notify "Error" "Directory $dir does not exist"
exit 1
fi
# Record area of the screen
# Get area
$notify "Select area to record"
area=$(eval $slop)
notify-send -t 1 ""
# Check if area is empty
if [ -z "$area" ]; then
exit 1
fi
# Get area coordinates
IFS=',' read -r x y w h <<< "$area"
# Ensure width and height are even
w=$((w / 2 * 2))
h=$((h / 2 * 2))
# Write the stop_recording function to a temporary script file
tmp_script=$(mktemp)
chmod +x "$tmp_script"
cat << 'EOF' > "$tmp_script"
#!/bin/bash
pkill -f "ffmpeg -f x11grab"
EOF
# Create tray icon
(
yad --notification --image="$icon" --text="Recording..." --command="$tmp_script" &
tray_pid=$!
wait $tray_pid
) &
# Record screen
# Audio can be recorded with: "-f alsa -i pulse -c:a aac -strict experimental -b:a 128k -ar 44100"
ffmpeg -f x11grab -s "$w"x"$h" -i :0.0+$x,$y -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -crf 23 -r 30 -y "$file"
# Kill yad
pkill yad
# Copy file path to clipboard
echo -n "$file" | xclip -selection clipboard
# Notify
$notify "Recording saved" "File: $file"
# Open output directory
nemo $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment