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:
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.
Before you begin, ensure you have the appropriate directory structure in place:
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.
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.
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:
%F
represents each file in the loop. We use the dir /s /b /A:-D
to fetch the .mp4 videos in the directory.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.
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:
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.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:
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.
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.
It's also possible to convert all files in a directory, regardless of their extension. Let's see how we can do that:
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.
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.
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.
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.
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.
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).
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.
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.
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.
Every month we share articles like this one to keep you up to speed with automated video editing.