Skip to content

Instantly share code, notes, and snippets.

@TitusRobyK
Last active February 4, 2024 05:15
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 TitusRobyK/263780b41b8f00d112c44d85401877b5 to your computer and use it in GitHub Desktop.
Save TitusRobyK/263780b41b8f00d112c44d85401877b5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is a comprehensive Bash script that automates the process of installing FFmpeg (if not already installed),
# then recursively searches through a directory and its subdirectories for .mov files.
# For each .mov file found, it uses FFmpeg to convert the file into a compressed .mp4 format,
# aiming to significantly reduce the file size with minimal loss in quality.
# Function to install ffmpeg
install_ffmpeg() {
echo "ffmpeg is not installed. Attempting to install using Homebrew..."
# Install Homebrew if it's not installed
if ! command -v brew &> /dev/null; then
echo "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Install ffmpeg using Homebrew
brew install ffmpeg
}
# Function to process files in a directory
process_directory() {
local directory=$1
local count=0
# Iterate over all files in the directory
for file in "$directory"/*; do
# Check if it's a directory
if [ -d "$file" ]; then
# It's a directory, so process it recursively
process_directory "$file"
elif [ -f "$file" ]; then
# It's a file, check if it's an MOV
if [[ "$file" == *.mov ]]; then
# Get the base name without the extension
local base_name=$(basename "$file" .mov)
local output_file="${directory}/${base_name}-compressed.mp4"
# Execute ffmpeg command to compress the mp4 file
ffmpeg -i "$file" -vcodec libx264 -crf 24 -preset slow -acodec copy "$output_file" &
# Increment the count
((count++))
# If the number of jobs reaches the limit, wait for all to complete
if ((count % num_jobs == 0)); then
wait # Wait for all background jobs to finish
fi
fi
fi
done
}
# Check if ffmpeg is installed
if ! command -v ffmpeg &> /dev/null; then
install_ffmpeg
# Check if ffmpeg was successfully installed
if ! command -v ffmpeg &> /dev/null; then
echo "Failed to install ffmpeg. Please install it manually."
exit 1
fi
fi
num_jobs=12 # Adjust based on your system's capabilities
# Start processing from the current directory
process_directory "."
wait # Wait for any remaining background jobs to finish
echo "Conversion complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment