Skip to content

Instantly share code, notes, and snippets.

@kuldar
Last active June 24, 2025 11:03
Show Gist options
  • Save kuldar/a5b7c6728cda0694c94c29295da3854e to your computer and use it in GitHub Desktop.
Save kuldar/a5b7c6728cda0694c94c29295da3854e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Full path to ffmpeg
FFMPEG="/opt/homebrew/bin/ffmpeg"
# Get dropped files
files=("$@")
# Init variables
mp4=""
srt=""
# Find MP4 and SRT among dragged files
for f in "${files[@]}"; do
ext="${f##*.}"
if [[ "$ext" == "mp4" ]]; then
mp4="$f"
elif [[ "$ext" == "srt" ]]; then
srt="$f"
fi
done
# Check both files were provided
if [[ -z "$mp4" || -z "$srt" ]]; then
osascript -e 'display dialog "Please drag both an MP4 and an SRT file." buttons {"OK"} default button "OK"'
exit 1
fi
# Function to normalize show names using regex patterns
get_show_name() {
local filename="$1"
local basename=$(basename "$filename" .srt)
# Convert to lowercase for case-insensitive matching
local lower_name=$(echo "$basename" | tr '[:upper:]' '[:lower:]')
# Define regex patterns for each show (case insensitive)
if [[ "$lower_name" =~ kids?[_[:space:]]*news? ]]; then
echo "Kidsnews"
elif [[ "$lower_name" =~ mitenand ]]; then
echo "Mitenand"
elif [[ "$lower_name" =~ puls[_[:space:]]*check ]]; then
echo "Pulscheck"
elif [[ "$lower_name" =~ mona ]]; then
echo "Mona"
elif [[ "$lower_name" =~ rec ]]; then
echo "Rec"
elif [[ "$lower_name" =~ clip ]] && [[ "$lower_name" =~ klar ]]; then
echo "ClipKlar"
elif [[ "$lower_name" =~ reporter ]]; then
echo "Reporter"
else
# If no match found, return the original basename without extension
echo "$basename"
fi
}
# Function to extract abbreviation from SRT filename
get_abbreviation() {
local filename="$1"
local basename=$(basename "$filename" .srt)
# Find all _XX patterns and get the last one
local matches=($(echo "$basename" | grep -oE '_[A-Z]{2}'))
if [[ ${#matches[@]} -gt 0 ]]; then
# Return the last match
echo "${matches[-1]}"
else
echo ""
fi
}
# Get the normalized show name and abbreviation
show_name=$(get_show_name "$srt")
abbreviation=$(get_abbreviation "$srt")
# Build output filename
dir=$(dirname "$mp4")
output_name="${show_name}${abbreviation}.mp4"
output="$dir/$output_name"
# Display what we're about to do
echo "Processing:"
echo " MP4: $(basename "$mp4")"
echo " SRT: $(basename "$srt")"
echo " Output: $output_name"
# Run ffmpeg to burn subtitles
"$FFMPEG" -i "$mp4" \
-vf "subtitles='${srt}':force_style='FontName=Verdana,FontSize=29,BackColour=&H00000000,BorderStyle=4,MarginL=50,MarginR=50'" \
-c:v libx264 -crf 23 -preset medium -c:a copy "$output"
# Check if ffmpeg succeeded
if [[ $? -eq 0 ]]; then
echo "✅ Successfully created: $output_name"
else
echo "❌ Error occurred during processing"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment