Created
February 15, 2026 22:46
-
-
Save VehpuS/42fb080d17e93d209dd1f2d1c06f2af2 to your computer and use it in GitHub Desktop.
Use ffmpeg to batch widen vertical videos with a blur effect (i.e. to upload vertical videos as non-shorts in Youtube)
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 | |
| # Target directory from the first argument, or current directory if none is provided | |
| TARGET_DIR="${1:-.}" | |
| # Define the complex filter as a variable to keep the code clean | |
| FILTER='split[original][copy];[copy]scale=ih*16/9:-1,crop=h=iw*9/16,gblur=sigma=20[blurred];[blurred][original]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2' | |
| for input_file in "$TARGET_DIR"/*.[mM][pP]4 "$TARGET_DIR"/*.[mM][oO][vV]; do | |
| [ -e "$input_file" ] || continue | |
| if [[ "$input_file" == *-wide.* ]]; then | |
| echo "Skipping already processed file: $input_file" | |
| continue | |
| fi | |
| ext="${input_file##*.}" | |
| base="${input_file%.*}" | |
| output_file="${base}-wide.${ext}" | |
| echo "--------------------------------------------------------" | |
| echo "Processing: $input_file" | |
| echo "Outputting: $output_file" | |
| echo "--------------------------------------------------------" | |
| # 1. Attempt Hardware Acceleration (h264_videotoolbox) | |
| echo "Attempting hardware-accelerated encoding..." | |
| # Added -nostdin to prevent ffmpeg from interacting with the shell loop | |
| if ffmpeg -nostdin -i "$input_file" -vf "$FILTER" -c:v h264_videotoolbox -c:a copy "$output_file"; then | |
| echo "✅ Success: Hardware acceleration used." | |
| else | |
| echo "⚠️ Hardware acceleration failed. Falling back to standard software encoding..." | |
| # Remove the partially generated, broken file from the failed attempt | |
| rm -f "$output_file" | |
| # 2. Fallback to Standard Software Encoding (libx264 implicitly) | |
| if ffmpeg -nostdin -i "$input_file" -vf "$FILTER" -c:a copy "$output_file"; then | |
| echo "✅ Success: Software encoding used." | |
| else | |
| echo "❌ Error: Both hardware and software encoding failed for $input_file." | |
| # Clean up the broken file from the software attempt, if any | |
| rm -f "$output_file" | |
| fi | |
| fi | |
| done | |
| echo "Batch processing complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment