Last active
August 7, 2026 15:17
-
-
Save davekobrenski/8532a23238d83f019e1104217f41839e to your computer and use it in GitHub Desktop.
Batch Optimize Videos for WEB with FFmpeg
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/sh | |
| # colors | |
| green='\033[0;32m' | |
| purple='\033[0;35m' | |
| cyan='\033[0;36m' | |
| yellow='\033[0;33m' | |
| clear='\033[0m' | |
| # loop through immediate subdirectories | |
| for dir in */ | |
| do | |
| MYDIR=$(basename "$dir") | |
| NEWDIR="${dir}compressed" | |
| printf "%bConverting files in: %s\n---------------------------------------------\n%b\n" "$green" "$MYDIR" "$clear" | |
| if [ ! -d "$NEWDIR" ] | |
| then | |
| printf " Creating new dir: %s\n" "$NEWDIR" | |
| mkdir "$NEWDIR" | |
| else | |
| printf " Dir already exists: %s\n" "$NEWDIR" | |
| fi | |
| printf "%b Preparing files in %s:%b\n\n" "$yellow" "$MYDIR" "$clear" | |
| for file in "./$dir"*.mov "./$dir"*.MOV | |
| do | |
| # skip if no files matched the pattern | |
| [ -e "$file" ] || continue | |
| FNAME=$(basename "${file%.*}") | |
| NEWFILE="./${NEWDIR}/${FNAME}_1080p_H264_CRF-23.mp4" | |
| printf " %s\n" "$file" | |
| printf "%b %s %b\n" "$cyan" "$NEWFILE" "$clear" | |
| # Standard web: | |
| # -crf 23 -preset medium | |
| # Higher quality: | |
| # -crf 21 -preset slow | |
| # Smaller files: | |
| # -crf 24 -preset medium | |
| ffmpeg -n -i "$file" \ | |
| -vf "scale=-2:'min(1080,ih)'" \ | |
| -c:v libx264 \ | |
| -crf 23 \ | |
| -preset medium \ | |
| -pix_fmt yuv420p \ | |
| -c:a aac \ | |
| -b:a 128k \ | |
| -movflags +faststart \ | |
| "$NEWFILE" | |
| done | |
| printf "\n" | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Video Compression Script
This script batch-compresses
.mov/.MOVvideo files for web delivery using FFmpeg.It is designed to be run from a parent directory that contains one or more subfolders of videos. The script looks inside each immediate subfolder, finds
.movand.MOVfiles, and creates optimized.mp4versions inside a newcompressedfolder within each subfolder.Requirements
FFmpeg must already be installed on your machine and available from the command line.
To check whether FFmpeg is installed, run:
ffmpeg -versionWhat this script produces
The script creates web-friendly H.264
.mp4files using the following settings:In practical terms, this means:
.mp4files suitable for web playback..movexports, are compressed to much smaller H.264 files.faststartis enabled so web videos can begin playback before the entire file has downloaded.Output files are named using this pattern:
OriginalFilename_1080p_H264_CRF-23.mp4For example:
Video1.movbecomes:
Video1_1080p_H264_CRF-23.mp4Compression settings
The current FFmpeg command is:
CRF
The script currently uses:
-crf 23CRF controls the visual quality / file size balance. Lower numbers produce higher quality and larger files. Higher numbers produce smaller files and lower quality.
Useful options to test:
-crf 21Higher quality, larger file size.
-crf 23Good general-purpose web compression.
-crf 24Smaller files, still often acceptable depending on the source video.
Preset
The script currently uses:
-preset mediumThe preset controls encoding speed and compression efficiency. It does not directly set quality; CRF does that.
Useful options:
-preset mediumGood default balance of speed and file size.
-preset slowSlower encode, potentially smaller files at similar visual quality.
Instructions
This script is non-destructive and will leave original files intact. Optimized videos will be placed in a folder called
compressed.For example:
cdinto the parent directory:cd Path/To/MyDirchmod +x ./convert.sh./convert.shcompressedfolder inside each video directory for optimized files.For example:
Notes
The script uses
-n, which tells FFmpeg not to overwrite existing output files. This makes it safer to rerun the script on the same folder later.If an output file already exists, FFmpeg will skip that file instead of replacing it.