Skip to content

Instantly share code, notes, and snippets.

@Thomashighbaugh
Created November 22, 2023 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomashighbaugh/3edb389da3187ea0fbc777c07ddd45f6 to your computer and use it in GitHub Desktop.
Save Thomashighbaugh/3edb389da3187ea0fbc777c07ddd45f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Check if ffmpeg is installed
if ! command -v ffmpeg &>/dev/null; then
echo "Error: ffmpeg is not installed. Please install it before running this script."
exit 1
fi
# Check for the correct number of command-line arguments
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Input directory containing MP4 files to optimize
input_directory="$1"
# Check if the input directory exists
if [ ! -d "$input_directory" ]; then
echo "Error: The specified directory does not exist."
exit 1
fi
# Function to optimize an MP4 file
optimize_mp4() {
local input_file="$1"
local output_file="${input_file%.mp4}_optimized.mp4"
# Check if the output file already exists
if [ -e "$output_file" ]; then
echo "Output file already exists: $output_file"
return
fi
echo "Optimizing: $input_file"
# Use ffmpeg to optimize the MP4 file with lossless settings
ffmpeg -i "$input_file" -c:v copy -c:a copy "$output_file"
if [ $? -eq 0 ]; then
echo "Optimization successful: $output_file"
# Optionally, you can delete the original file to save space
# rm "$input_file"
else
echo "Optimization failed for: $input_file"
fi
}
# Find all MP4 files in the specified directory
find "$input_directory" -type f -name "*.mp4" | while read -r file; do
optimize_mp4 "$file"
done
echo "Optimization complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment