Skip to content

Instantly share code, notes, and snippets.

@olibartfast
Last active July 9, 2025 16:26
Show Gist options
  • Select an option

  • Save olibartfast/3985416a2f901d85713b7f0410ec873d to your computer and use it in GitHub Desktop.

Select an option

Save olibartfast/3985416a2f901d85713b7f0410ec873d to your computer and use it in GitHub Desktop.
Blackout a Region of Interest (ROI) in a video for a given time window.
#!/bin/bash
######################################################################
# blackout_roi.sh
#
# πŸ”³ Blackout a Region of Interest (ROI) in a video for a given time window.
#
# 🎯 Usage:
# ./blackout_roi.sh input.mp4 ROI t1 duration output.mp4 [x y w h]
#
# ROI options:
# Center – Vertical center strip (excluding 1/4 on each side)
# Left – Left half of the frame
# Right – Right half of the frame
# Top – Top half of the frame
# Bottom – Bottom half of the frame
# TopLeft – Upper-left quarter
# TopRight – Upper-right quarter
# BottomLeft – Lower-left quarter
# BottomRight – Lower-right quarter
# custom – Manually define ROI via x y w h (pixels)
#
# πŸ’‘ Examples:
# ./blackout_roi.sh input.mp4 Center 10 4 output.mp4
# ./blackout_roi.sh input.mp4 custom 12 5 output.mp4 100 50 300 200
######################################################################
if [ "$#" -lt 5 ]; then
echo "Usage: $0 input.mp4 ROI t1 duration output.mp4 [x y w h]"
exit 1
fi
INPUT="$1"
ROI="$2"
T1="$3"
DURATION="$4"
OUTPUT="$5"
T2=$(echo "$T1 + $DURATION" | bc)
# Get video resolution
RESOLUTION=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height \
-of csv=p=0:s=x "$INPUT")
WIDTH=$(echo "$RESOLUTION" | cut -d'x' -f1)
HEIGHT=$(echo "$RESOLUTION" | cut -d'x' -f2)
if [[ -z "$WIDTH" || -z "$HEIGHT" ]]; then
echo "❌ Error: Could not determine video resolution"
exit 1
fi
# ROI definitions
case "$ROI" in
Center)
X=$(($WIDTH / 4))
Y=0
W=$(($WIDTH / 2))
H=$HEIGHT
;;
Left)
X=0
Y=0
W=$(($WIDTH / 2))
H=$HEIGHT
;;
Right)
X=$(($WIDTH / 2))
Y=0
W=$(($WIDTH / 2))
H=$HEIGHT
;;
Top)
X=0
Y=0
W=$WIDTH
H=$(($HEIGHT / 2))
;;
Bottom)
X=0
Y=$(($HEIGHT / 2))
W=$WIDTH
H=$(($HEIGHT / 2))
;;
TopLeft)
X=0
Y=0
W=$(($WIDTH / 2))
H=$(($HEIGHT / 2))
;;
TopRight)
X=$(($WIDTH / 2))
Y=0
W=$(($WIDTH / 2))
H=$(($HEIGHT / 2))
;;
BottomLeft)
X=0
Y=$(($HEIGHT / 2))
W=$(($WIDTH / 2))
H=$(($HEIGHT / 2))
;;
BottomRight)
X=$(($WIDTH / 2))
Y=$(($HEIGHT / 2))
W=$(($WIDTH / 2))
H=$(($HEIGHT / 2))
;;
custom)
if [ "$#" -ne 9 ]; then
echo "❌ Error: For 'custom' ROI, you must provide x y w h"
echo "Usage: $0 input.mp4 custom t1 duration output.mp4 x y w h"
exit 1
fi
X="$6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment