Creating GIF in swaywm by keyboard-driven method
where can I find a simple GIF-making app?
I’ve been looking for a simple app to make GIFs, however most of the exising apps are a bit bloated.
To make matters worse, most of them do not support Wayland natively 😕.
While I was searching, I came across this simple script which’s just what I needed. Hence this post.
See more info, Please visit an elegant approach using wlrecorder and slurp
1. Prerequisite
- zenity
- slurp
- wf-recorder
- ffmpeg
use your favorite package manager to install dependencies mentioned above.
pacman -S zentiy slurp wf-recorder ffmpeg
2. Bash Script
#!/usr/bin/env bash
# If an instance of wf-recorder is running under this user kill it with SIGINT and exit
pkill --euid "$USER" --signal SIGINT wf-recorder && exit
# Define paths
DefaultSaveDir='/home/nobody/GIFs'
TmpPathPrefix='/tmp/gif-record'
TmpRecordPath=$TmpPathPrefix'-cap.mp4'
TmpPalettePath=$TmpPathPrefix'-palette.png'
# Trap for cleanup on exit
OnExit() {
[[ -f $TmpRecordPath ]] && rm -f "$TmpRecordPath"
[[ -f $TmpPalettePath ]] && rm -f "$TmpPalettePath"
}
trap OnExit EXIT
# Set umask so tmp files are only acessible to the user
umask 177
# Get selection and honor escape key
Coords=$(slurp) || exit
# Capture video using slup for screen area
# timeout and exit after 10 minutes as user has almost certainly forgotten it's running
timeout 600 wf-recorder -g "$Coords" -f "$TmpRecordPath" || exit
# Get the filename from the user and honor cancel
SavePath=$( zenity \
--file-selection \
--save \
--confirm-overwrite \
--file-filter=*.gif \
--filename="$DefaultSaveDir"'/.gif' \
) || exit
# Append .gif to the SavePath if it's missing
[[ $SavePath =~ \.gif$ ]] || SavePath+='.gif'
# Produce a pallete from the video file
ffmpeg -i "$TmpRecordPath" -filter_complex "palettegen=stats_mode=full" "$TmpPalettePath" -y || exit
# Return umask to default
umask 022
# Use pallete to produce a gif from the video file
ffmpeg -i "$TmpRecordPath" -i "$TmpPalettePath" -filter_complex "paletteuse=dither=sierra2_4a" "$SavePath" -y || exit
3. Set shortcut
Make sure this script is executable.
chmod +x wgifcap
In swaywm, binding to a shortcut.
bindsym $mod2+q exec wgifcap
4. Experiment
- Type $mod2+q will let you select a region to be recorded.
- Do whatever you want. (please note that the region will not show border lines.)
- Type $mod2+q again will stop recording, and will let you type the GIF filename to be saved.