Last active
July 9, 2025 16:26
-
-
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.
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 | |
| ###################################################################### | |
| # 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