C.W.K.
Stream
Lesson 10 of 10 · published

Batch Conversion — Run Anything on a Folder

~12 min · batch, shell, automation, parallel

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Same command, many files

Once a single FFmpeg command works on one file, batching it across a folder is a 3-line shell loop. There are three patterns worth knowing.

  1. Bash for-loop — readable, sequential, easy to interrupt.
  2. find + -exec — robust to weird filenames, recursive, single command per file.
  3. GNU parallel — runs N jobs at once. Speedup is dramatic when each encode is CPU-bound.

Always quote filenames

Spaces and special characters in filenames are the #1 source of broken batch scripts. Always quote "$f", never bare $f. Test on a folder with one file containing a space before you point it at 200 files.

Code

Three batch patterns·bash
# 1) Bash loop — convert every .mov in current dir to .mp4
for f in *.mov; do
  ffmpeg -i "$f" -c:v libx264 -crf 20 -preset slow \
         -c:a aac -b:a 192k "${f%.mov}.mp4"
done

# 2) find — recursive, handles odd filenames
find . -type f -name '*.mov' -exec sh -c '
  ffmpeg -i "$1" -c:v libx264 -crf 20 -preset slow \
         -c:a aac -b:a 192k "${1%.mov}.mp4"
' _ {} \;

# 3) GNU parallel — N jobs at once (N = your core count by default)
# brew install parallel
ls *.mov | parallel \
  'ffmpeg -i {} -c:v libx264 -crf 20 -preset slow \
          -c:a aac -b:a 192k {.}.mp4'
Skip already-converted outputs·bash
# Idempotent loop — only convert files whose .mp4 doesn't exist
for f in *.mov; do
  out="${f%.mov}.mp4"
  [ -f "$out" ] && { echo "skip $out"; continue; }
  ffmpeg -i "$f" -c:v libx264 -crf 20 -preset slow \
         -c:a aac -b:a 192k "$out" || echo "FAIL: $f"
done

External links

Exercise

Make a folder with 5 video files. Write an idempotent shell loop that converts each to MP4 (libx264 CRF 22) only if the output doesn't already exist. Run it once, delete one output file, run again — verify only the missing file is rebuilt. Bonus: make the same loop run 4 jobs in parallel with GNU parallel.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.