Skip to content

Instantly share code, notes, and snippets.

@adeleke5140
Last active July 22, 2025 10:54
Show Gist options
  • Save adeleke5140/8cfeedde752a151642eddad7ae989195 to your computer and use it in GitHub Desktop.
Save adeleke5140/8cfeedde752a151642eddad7ae989195 to your computer and use it in GitHub Desktop.
Script to compress video with ffmpeg
#!/bin/bash
DIR="${1:-.}"
CRF="${2:-28}"
if ! command -v ffmpeg &> /dev/null; then
echo "Error: ffmpeg is not installed or in PATH"
fi
if [ ! -d "$DIR" ]; then
echo "Error: Directory $DIR does not exist"
exit 1
fi
echo "Compressing MP4 files in $DIR"
echo "Using CRF value: $CRF"
echo "------------------"
count=0
for file in "$DIR"/*.mp4; do
if [ ! -f "$file" ]; then
echo "No MP4 files found in $DIR"
exit 0
fi
filename=$(basename "$file" .mp4)
dir=$(dirname "$file")
if [[ "$filename" == *"compressed"* ]]; then
echo "Skipping $file (compressed version already exists)"
continue
fi
echo "Processing: $file"
original_size=$(du -h "$file" | cut -f1)
if ffmpeg -i "$file" -crf "$CRF" -c:v libx264 -c:a aac "$output" -y; then
compressed_size=$(du -h "output" | cut f1)
echo "✅ Completed $filename.mp4 ($original_size -> $compressed_size)"
((count++))
else
echo "x Failed to compress: $file"
[ -f "$output" ] && rm "$output"
fi
echo "------------------------"
done
echo "Compression complete! Processed $count files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment