Skip to content

Instantly share code, notes, and snippets.

@seaque
Created February 15, 2025 12:45
Show Gist options
  • Save seaque/2c36fb2ed995d65d5271cdd439d59405 to your computer and use it in GitHub Desktop.
Save seaque/2c36fb2ed995d65d5271cdd439d59405 to your computer and use it in GitHub Desktop.
Bash drag and drop script to detect black borders on dropped videos and remove them.
#!/bin/bash
# Check if at least one file is provided
if [ "$#" -eq 0 ]; then
echo "No files provided. Drag and drop video files onto this script."
exit 1
fi
# Temporary file to store crop parameters
TEMP_FILE=$(mktemp)
# Loop through all dropped files
for FILE in "$@"; do
# Check if the file exists
if [ ! -f "$FILE" ]; then
echo "File not found: $FILE"
continue
fi
# Get crop parameters using cropdetect
ffmpeg -i "$FILE" -vf cropdetect -f null - 2> "$TEMP_FILE"
# Extract the last crop value from the temp file
CROP_PARAMS=$(awk '/crop=/ {a=$NF} END{print a}' "$TEMP_FILE")
# Check if crop parameters were found
if [ -z "$CROP_PARAMS" ]; then
echo "No crop parameters detected for $FILE."
continue
fi
# Generate output filename by replacing the extension with .cropped.mp4
OUTPUT_FILE="${FILE%.*}-NOLTBX.mp4"
# Apply cropping and save the output file
ffmpeg -i "$FILE" -vf "$CROP_PARAMS" -c:a copy "$OUTPUT_FILE" -y
echo "Processed $FILE -> $OUTPUT_FILE"
done
# Clean up temporary file
rm "$TEMP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment