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

Batch 변환 — 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

같은 명령, 많은 파일

한 파일에 FFmpeg 명령 동작하면, folder 전체에 batch는 3줄 shell loop. 알아둘 패턴 셋.

  1. Bash for-loop — 읽기 좋음, 순차, interrupt 쉬움.
  2. find + -exec — 이상한 파일이름 robust, recursive, 파일당 명령 하나.
  3. GNU parallel — N 작업 동시. 각 encode 가 CPU-bound 일 때 dramatic 속도.

항상 파일이름 quote

파일이름의 space와 special character 가 깨진 batch script #1 source. $f bare 안 되고 항상 "$f" quote. 200 파일 가리키기 전에 space 든 파일 하나 있는 folder에 테스트.

Code

세 batch 패턴·bash
# 1) Bash loop — 현재 dir의 모든 .mov를 .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, 이상한 파일이름 처리
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 작업 동시 (default N = core count)
# brew install parallel
ls *.mov | parallel \
  'ffmpeg -i {} -c:v libx264 -crf 20 -preset slow \
          -c:a aac -b:a 192k {.}.mp4'
이미 변환된 output skip·bash
# Idempotent loop — .mp4 없는 파일만 변환
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

Video 파일 5개로 folder 만들어. Output 이미 없으면만 각각 MP4 (libx264 CRF 22) 로 변환하는 idempotent shell loop 작성. 한 번 돌리고, output 하나 지우고, 다시 돌려 — missing 파일만 rebuild 되는지 확인. 보너스: 같은 loop을 GNU parallel 로 4 작업 병렬로.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.