Batch convert videos using FFmpeg

Batch converting videos and media files is one of the common use cases that can be automated with software and online tools. There are many reasons you might want to batch convert videos, including:

  • Extracting audio tracks from videos to create MP3s audio only files. For example, you may want to create audio versions of lectures or presentations.
  • Converting videos from multiple formats to a uniform format, like MP4, for better compatibility across devices and applications.
  • Standardizing video settings by resizing dimensions and adjusting compression settings. This can help to free up disk space while maintaining high visual quality.

The Shotstack Ingest API makes it easy to upload, store and convert files in batch but sometimes you might want to convert files locally or build your own solution.

In this guide, we will show how to use FFmpeg to batch convert multiple videos for different use cases.

FFmpeg is an open-source tool for handling multimedia files. Through its command line interface, we will be able to script and automate the entire batch conversion process.

To follow the instructions in this guide, you will need a basic understanding of how to execute commands on a terminal. Also, ensure that you have FFmpeg installed on your system. If it isn't already installed, you can visit the official FFmpeg downloads page and install the latest stable version for your operating system.

Setting up the directory structure

Before you begin, ensure you have the appropriate directory structure in place:

  1. Create a main folder named batch.
  2. Inside the batch folder, create two subfolders named input and output.
  3. Place all the videos you want to convert in the input folder.
  4. Before executing any command, make sure to navigate to the batch parent directory.

To perform the batch conversions, we will use for loops to iteratively process each video in the input folder. The exact syntax will vary depending on your operating system.

Batch convert MP4 to MP3 using FFmpeg

For our first example, we will explore how to use FFmpeg to batch convert MP4 videos to MP3s. In a previous guide we showed you how to convert videos to MP3, this time we'll do it in batch.

On Windows

Navigate to the batch directory and execute the following command:

FOR /F "usebackq delims=|" %F IN (`dir /s /b /A:-D "input\*.mp4"`) DO ffmpeg -i "%F" -acodec mp3 "output\%~nF.mp3"

Here's what the command does:

  1. It loops through the .mp4 files in the input directory. %F represents each file in the loop. We use the dir /s /b /A:-D to fetch the .mp4 videos in the directory.
  2. For each iteration, it executes the ffmpeg -i "%F" -acodec mp3 "output\%~nF.mp3" command. We use the -i option to specify the input file, and the -acodec option to specify the mp3 audio codec. Finally, we use output\%~nF.mp3 to store the converted files in the output directory, and to name each file using the name of the original video.

The processing time may vary based on the number and duration of videos. Once it completes, you should be able to see the mp3 audio files in the output directory.

On Linux and Mac (OS X)

For Linux based systems, including Mac OS X, we can use this command:

for file in input/*.mp4; do ffmpeg -i "$file" -acodec mp3 "output/$(basename "$file" .mp4).mp3"; done

Here's what the command does:

  1. It initiates a for loop in the input directory.
  2. For each iteration, it executes this command: ffmpeg -i "$file" -acodec mp3 "output/$(basename "$file" .mp4).mp3. We use -i to specify the input file, -acodec to specify the mp3 audio codec, and output/$(basename "$file" .mp4).mp3 to create the output file name by replacing the .mp4 extension with .mp3.

Batch convert multiple formats to MP3 using FFmpeg

It's possible that the source files are not all mp4 files and might be different, such as a collection of .mp4, .mov, or .avi files. To batch convert multiple formats, we can modify our commands as follows:

On Windows

FOR /F "usebackq delims=|" %F IN (`dir /s /b /A:-D "input\*.mp4" "input\*.mov" "input\*.avi"`) DO ffmpeg -i "%F" -acodec mp3 "output\%~nF.mp3"

We have passed two additional file extensions to the directory listing command. You can incorporate as many extensions as required to convert multiple formats.

On Linux/OS X

for file in input/*.{mp4,mov,avi}; do ffmpeg -i "$file" -acodec mp3 -q:a 3 "output/$(basename "$file" .${file##*.}).mp3"; done

For the Linux command, we have specified the list of extensions inside a curly bracket. The ${file##*.} extracts the extension of each file, which is then replaces the original extension with .mp3 in the output file.

Batch convert using wildcards to MP3 using FFmpeg

It's also possible to convert all files in a directory, regardless of their extension. Let's see how we can do that:

On Windows

FOR /F "usebackq delims=|" %F IN (`dir /s /b /A:-D "input\*.*"`) DO ffmpeg -i "%F" -acodec mp3 "output\%~nF.mp3"

We use the input\*.* expression to process and convert all files inside the input directory, regardless of their expression.

On Linux/OS X

for file in input/*; do ffmpeg -i "$file" -acodec mp3 -q:a 3 "output/$(basename "$file" .${file##*.}).mp3"; done

In this command, we specify input/* to process all the files in the directory.

Note that using wildcards might try to convert files that are not compatible with FFmpeg.

Batch convert multiple formats to MP4 format using FFmpeg

For our second use case we'll convert multiple video formats to MP4. This might be useful where you want to edit all files using one format or you want top support the video files in a browser or specific players. FFmpeg makes it easy to convert file formats and you can run it in a loop to batch process the conversion.

On Windows

FOR /F "usebackq delims=|" %F IN (`dir /s /b /A:-D "input\*.mov" "input\*.mkv" "input\*.avi" "input\*.flv"`) DO ffmpeg -i "%F" -c:v libx264 -c:a aac "output\%~nF.mp4"

For each file, we execute this command (ffmpeg -i "%F" -c:v libx264 -c:a aac "output\%~nF.mp4") to convert to mp4 and save to the output directory. We use the libx264 video codec and AAC audio codec during the conversion.

On Linux/OS X

for file in input/*.{mov,mkv,avi,flv}; do ffmpeg -i "$file" -c:v libx264 -c:a aac "output/$(basename "$file" .${file##*.}).mp4"; done

For each input file, we execute the command ffmpeg -i "$file" -c:v libx264 -c:a aac "output/$(basename "$file" .${file##*.}).mp4 to convert to mp4 format and save to the output directory.

Batch compress multiple formats using FFmpeg

Video files can take up a lot of disk space. One way to clear up space is to convert videos, resize them and use a lossy compression codec. MP4 is one of the best formats for compression and when the resolution is standardised you can make the file size even smaller.

In the following example, we'll batch convert videos by resizing them to 720p, use the h264 MP4 compression algorithm with a Constant Rate Factor (CFR) of 23 (high compression with low loss of visual quality).

On Windows

FOR /F "usebackq delims=|" %F IN (`dir /s /b /A:-D "input\*.mov" "input\*.mkv" "input\*.avi" "input\*.flv"`) DO ffmpeg -i "%F" -vf "scale=-1:720" -c:v libx264 -crf 23 -c:a aac "output\%~nF_720p.%~xF"

In this command, we use the scale filter to resize the videos to 720p, -c:v to specify the video codec as libx264, -crf to set the CRF to 23, and -c:a to encode the audio using the AAC codec. "output\%~nF_720p.%~xF" appends the resolution to the name of the output file.

On Linux/OS X

for file in input/*.{mov,mkv,avi,flv}; do ffmpeg -i "$file" -vf "scale=-1:720" -c:v libx264 -crf 23 -c:a aac "output/$(basename "$file" .${file##*.})_720p.${file##*.}"; done

Similar to the Windows command, we use the scale, -c:v, -crf, and -c:a options to compress the input videos. The "output/$(basename "$file" .${file##*.})_720p.${file##*.}" part appends the resolution to the output file name.

Conclusion

FFmpeg is a powerful and versatile multimedia processing toolkit that simplifies batch processing of videos. In this guide, we explored using FFmpeg's command-line interface to convert a wide range of video formats on both Windows and Linux based systems.

If you need a way to automate video production and workflows at scale Shotstack is a viable FFmpeg alternative with services ranging from a video editing API to a no-code multimedia workflow tool.

Maab Saleem

BY MAAB SALEEM
6th December, 2023

Become an Automated Video Editing Pro

Every month we share articles like this one to keep you up to speed with automated video editing.


You might also like

 Convert videos to MP3 using FFmpeg

Convert videos to MP3 using FFmpeg

Jeff Shillitto
How to crop and resize videos using FFmpeg

How to crop and resize videos using FFmpeg

Kathy Calilao
How to trim a video using FFmpeg

How to trim a video using FFmpeg

Joyce Echessa