Batch Convert Files with FFmpeg and Zsh on macOS

This article provides a quick, step-by-step guide on how to batch convert audio and video files on macOS using FFmpeg and a Zsh loop. You will learn the exact command structure to run directly in your Terminal, how to handle file extensions properly, and how to customize the loop for different formats.

The Basic Zsh Loop Structure

To batch convert files in macOS (which uses Zsh as the default shell), you can run a simple for loop directly in the Terminal.

Navigate to the folder containing your files using the cd command, and then run the following loop:

for f in *.wav; do ffmpeg -i "$f" "${f%.wav}.mp3"; done

How the Command Works

  • for f in *.wav; do: This starts the loop, targeting every file in the current directory with a .wav extension and assigning it to the variable f.
  • ffmpeg -i "$f": This calls FFmpeg and passes the current file in the loop as the input (-i). Wrapping $f in double quotes ensures that filenames with spaces are handled correctly without errors.
  • "${f%.wav}.mp3": This is Zsh parameter expansion. It strips the .wav extension from the original filename and replaces it with .mp3. This prevents your output files from being named awkwardly (e.g., audio.wav.mp3).
  • ; done: This closes the loop, telling the Terminal to move on to the next file until all targeted files are processed.

Batch Converting Video Files

The same logic applies to video files. If you want to convert a folder of .mkv files to .mp4 while copying the video stream and re-encoding the audio to AAC for compatibility, use this command:

for f in *.mkv; do ffmpeg -i "$f" -c:v copy -c:a aac "${f%.mkv}.mp4"; done

Running the Loop as a One-Liner or Multi-Line

You can paste the entire loop as a single line into your Terminal and press Enter. If you prefer readability, you can also type it out line-by-line:

for f in *.mkv
do
  ffmpeg -i "$f" -c:v copy -c:a aac "${f%.mkv}.mp4"
done

Both methods achieve the exact same result, allowing you to convert dozens of files in seconds without needing third-party GUI software.