Created
June 26, 2025 21:59
-
-
Save dewstouh/0203a649ffaf15c6feea6e8b10491e01 to your computer and use it in GitHub Desktop.
Linux Screen recorder script which allows you to select the recording area. As soon as the recording stops it gets automatically uploaded to imgur. Made for X11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CLIENT_ID="your_imgur_client_id" | |
TEMP_DIR="/tmp" | |
TIMESTAMP=$(date +%s) | |
VIDEO_FILE="$TEMP_DIR/screen_recording_$TIMESTAMP.mp4" | |
STOP_FILE="$TEMP_DIR/stop_$TIMESTAMP" | |
DISPLAY=":1.0" # Put your real display here | |
cleanup() { | |
[[ -f "$STOP_FILE" ]] && rm -f "$STOP_FILE" | |
[[ -n "$FFMPEG_PID" ]] && kill -9 "$FFMPEG_PID" &>/dev/null | |
} | |
trap cleanup EXIT | |
for cmd in slop ffmpeg yad notify-send curl jq xclip xdpyinfo; do | |
command -v "$cmd" &>/dev/null || { echo "β Missing: $cmd"; exit 1; } | |
done | |
record_screen() { | |
notify-send "πΉ Screen Recorder" "Select the area to record" -t 2000 | |
SELECTION=$(slop \ | |
-f "%x %y %w %h" \ | |
-b 6 \ | |
-c 0,0.4,1,0.9 \ | |
-l \ | |
-q) | |
[[ -z "$SELECTION" ]] && { notify-send "β Screen Recorder" "Selection cancelled"; exit 1; } | |
read -r X Y W H <<< "$SELECTION" | |
W=$(( (W+1)/2*2 )) | |
H=$(( (H+1)/2*2 )) | |
notify-send "π΄ Screen Recorder" "Recording ${W}x${H} on $DISPLAY..." -t 1000 | |
# Start recording BEFORE launching the STOP button | |
ffmpeg -v warning \ | |
-f x11grab -probesize 100M -analyzeduration 100M \ | |
-video_size ${W}x${H} -framerate 30 \ | |
-i "${DISPLAY}+${X},${Y}" \ | |
-c:v libx264 -preset fast -crf 20 \ | |
-pix_fmt yuv420p -movflags +faststart \ | |
"$VIDEO_FILE" & | |
FFMPEG_PID=$! | |
# Launch STOP button | |
screen_w=$(xdpyinfo | awk '/dimensions/{print $2}' | cut -d'x' -f1) | |
btn_x=$(( screen_w - 160 )) | |
yad --undecorated --skip-taskbar --on-top \ | |
--geometry=150x40+${btn_x}+10 \ | |
--button="π΄ STOP":0 &>/dev/null & | |
BUTTON_PID=$! | |
# Wait for button to close and stop ffmpeg | |
wait $BUTTON_PID | |
kill -TERM $FFMPEG_PID 2>/dev/null | |
wait $FFMPEG_PID | |
kill $BUTTON_PID &>/dev/null | |
[[ ! -s "$VIDEO_FILE" ]] && { notify-send "β Screen Recorder" "Nothing was recorded"; exit 1; } | |
notify-send "β Screen Recorder" "Recording finished: $VIDEO_FILE" -t 2000 | |
} | |
upload_video() { | |
size_mb=$(( $(stat -c%s "$VIDEO_FILE")/1024/1024 )) | |
if (( size_mb > 200 )); then | |
notify-send "β Screen Recorder" "File too large: ${size_mb}MB (>200MB)"; return 1 | |
fi | |
notify-send "β¬οΈ Screen Recorder" "Uploading ${size_mb}MB to Imgur..." -t 3000 | |
RESPONSE=$(curl -s -H "Authorization: Client-ID ${CLIENT_ID}" \ | |
-F "video=@${VIDEO_FILE}" \ | |
-F "type=video" \ | |
https://api.imgur.com/3/upload) | |
LINK=$(echo "$RESPONSE" | jq -r 'try .data.link // empty') | |
if [[ -n "$LINK" ]]; then | |
echo -n "$LINK" | xclip -selection clipboard | |
notify-send "β Screen Recorder" "Uploaded! Link copied:\n${LINK}" -t 5000 | |
echo "π ${LINK}" | |
else | |
ERR=$(echo "$RESPONSE" | jq -r 'try .data.error.message // .data.error // "Upload failed"') | |
notify-send "β Screen Recorder" "Upload failed: ${ERR}" -t 5000 | |
echo "Error: ${ERR}" >&2 | |
fi | |
rm -f "$VIDEO_FILE" | |
} | |
record_screen && upload_video |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment