Skip to content

Instantly share code, notes, and snippets.

@tonkku107
Last active July 20, 2026 14:10
Show Gist options
  • Select an option

  • Save tonkku107/d170f2faaa6ee09a1fbf08b260f8d179 to your computer and use it in GitHub Desktop.

Select an option

Save tonkku107/d170f2faaa6ee09a1fbf08b260f8d179 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -m
#**
# Screenrecorder script by Tonkku
# Credit goes to lots of Googling which finally brought me to
# https://www.reddit.com/r/i3wm/comments/7ahbvv/what_do_you_use_to_make_gif_screen_capture/dpavjo7
# Which was basically what I was doing originally but in a nicer form.
# Then I expanded it according to my original plans.
#
# Depends on
# - ffmpeg
# - curl
# - kdialog
# On X11:
# - xclip
# - slop (and the boxzoom shader)
# On Wayland:
# - ~~slurp~~ slop (without the shader, sad)
# - gpu-screen-recorder
# - wl-clipboard
#**
# Check if there's already a running recorder
if [ -f /tmp/recorder.pid ]; then
notify-send --icon=dialog-error "Recorder is already running"
exit 0
fi
# Set variables
TMP="$(mktemp -t screenrecording-XXXXXXXXXX.mp4)"
FILENAME="$(date '+%Y-%m-%d_%H-%M-%S')"
TMPGIF="/tmp/$FILENAME.gif"
UPLOADERKEY=""
# Clean up on exit
trap "rm -f /tmp/recorder.pid && rm -f '$TMP' && rm -f '$TMPGIF' && rm /tmp/palette.png" 0
function copyText {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
wl-copy $1
else
echo -n $1 | xclip -selection clipboard
fi
}
rm $TMP
# Select region and start recording
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
# read -r X Y W H < <(slurp -f "%x %y %w %h")
read -r X Y W H < <(slop -f "%x %y %w %h")
## ffmpeg requires `sudo setcap cap_sys_admin+ep /usr/bin/ffmpeg`
## and either produces garbled output or doesn't support a color format
# ffmpeg -y -device /dev/dri/card1 -f kmsgrab -i - -vf "hwmap=derive_device=vaapi,hwdownload,format=yuv444p,crop=$W:$H:$X:$Y" -framerate 30 "$TMP" &
RECORDING_OPTIONS="-w portal -restore-portal-session no"
if [ ! -z "${X}" ] && [ ! -z "${Y}" ] && [ ! -z "${W}" ] && [ ! -z "${H}" ]; then
RECORDING_OPTIONS="-w "$W"x"$H"+$X+$Y"
fi
gpu-screen-recorder $RECORDING_OPTIONS -f 30 -o "$TMP" &
else
read -r X Y W H < <(slop -f "%x %y %w %h" -r boxzoom)
ffmpeg -y -f x11grab -s "$W"x"$H" -i :0.0+$X,$Y -framerate 30 "$TMP" &
fi
echo $! > /tmp/recorder.pid
fg
# Check if recording was canceled
if [ ! -f $TMP ]; then
exit 0
fi
# Convert video to gif
notify-send --icon=dialog-information "Post-processing..."
ffmpeg -y -i "$TMP" -vf fps=10,palettegen /tmp/palette.png
ffmpeg -i "$TMP" -i /tmp/palette.png -filter_complex "paletteuse" $TMPGIF
notify-send --icon=dialog-information "Done. $(du -h $TMPGIF | awk '{print $1}')"
# Upload / Save
choice=$(kdialog --title "Choose action" --radiolist "Choose action" 1 "Save as..." on 2 "Upload to example.com" off)
if [ "${choice:-0}" -eq 1 ]; then
# Save
location=$(kdialog --title "Where should this recording be saved?" --getsavefilename $HOME/Desktop/$FILENAME.gif "GIF File (*.gif)")
mv $TMPGIF $location
elif [ "${choice:-0}" -eq 2 ]; then
notify-send --icon=dialog-information "Uploading..."
REMOTE=$(curl -F "file=@$TMPGIF" -H "key: $UPLOADERKEY" https://example.com/upload)
notify-send --icon=dialog-information "Uploaded. $REMOTE"
copyText $REMOTE
else
echo "Invalid option / cancel"
fi
#!/bin/bash
#**
# Script used to stop the recording when using with keyboard shortcuts
#**
kill $(cat /tmp/recorder.pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment