Convert All Files Inside a Folder using FFmpeg (Batch Convert)

FFmpeg is a powerful tool that can perform various operations on audio and video files, such as transcoding, filtering, editing, and streaming. In this blog post, I will show you how to batch convert or convert all the files inside a folder using FFmpeg, with some tips and tricks to optimize the process and the output quality.

Batch Conversion: Why and When?

Imagine you have a folder with hundreds of video or audio files. Maybe they’re in an older format, or perhaps they’re too large for practical use. Manually converting each one would be time-consuming, to say the least. This is where the magic of batch conversion comes into play.

Batch conversion allows you to convert all these files at once, without manual intervention. Whether you’re aiming for a consistent format or need to compress files for storage, batch conversion can be a game changer.

  • You want to change the format of your files to make them compatible with a specific device or platform.
  • You want to reduce the size of your files to save storage space or bandwidth.
  • You want to apply some filters or effects to your files, such as cropping, scaling, rotating, or adding subtitles.
  • You want to merge or split your files, or extract audio or video streams from them.

Whatever your goal is, FFmpeg can help you achieve it with a single command line.

However, before you start converting your files, you need to install FFmpeg on your system. You can download the latest version of FFmpeg from the official website or use a package manager to install it on your operating system. For example, on Windows, you can use Chocolatey (https://chocolatey.org/) to install FFmpeg with the command:

Initial Setup: Installing FFmpeg

Prior to delving into file conversions, the first step is to ensure that you have FFmpeg installed on your machine. The official FFmpeg repository (FFmpeg Downloads) houses the latest version and you can pick up a static build from there for your operating system. However, OS-specific package managers are also available for obtaining the latest FFmpeg.

  • Windows:
    Utilize Chocolatey with: choco install ffmpeg
  • Linux:
    Deploy apt-get via: sudo apt-get install ffmpeg
  • Mac OS X:
    With Homebrew, simply execute: brew install ffmpeg

Following the installation, access the terminal or command prompt and traverse to the directory containing your target files.

Mastering Batch Conversions

Once you have installed FFmpeg, you can open a terminal or a command prompt and navigate to the folder where your files are located. To convert all the files inside a folder using FFmpeg, you need to use a for loop that iterates over each file and passes it as an input to the FFmpeg command. The syntax of the for loop may vary depending on your operating system and shell.

Windows

On Windows, you can execute the following –

for %f in (*.*) do ffmpeg -i "%f" [options] "%~nf_converted.%~xf"

This command will loop over all the files in the current folder (represented by .) and convert them using FFmpeg. The -i option specifies the input file name, which is %f in this case. The [options] part is where you can specify various parameters for the conversion, such as the output format, codec, bitrate, resolution, frame rate, etc. The output file name is composed of %~nf_new.%~xf, which means the original file name (%~nf) with a _new suffix and the original file extension (%~xf).

Linux and Mac

The same thing can be achieved in Linux or Mac using the following command –

mkdir converted
for f in *.mp4; do ffmpeg -i "$f" "converted/${f%.mp4}.avi"; done

Here’s the explanation:

  • mkdir converted: This creates a directory named ‘converted’ for our output files.
  • for f in *.mp4: This loops through all .mp4 files.
  • ffmpeg -i "$f" "converted/${f%.mp4}.avi": FFmpeg will convert each file and store it in the ‘converted’ folder with a .avi extension.

Here are some common examples that you can use directly in your projects. Before running them, please make sure that you have modified it appropriately for your operating system.

Convert all the MP4 files in your folder to MKV format

for %f in (*.mp4) do ffmpeg -i "%f" -c:v libx264 -c:a aac "%~nf_new.mkv"

This will convert all the MKV files in the folder to MP4 format using the H.264/AVC video codec and the AAC audio codec. Since we have not specified any transcoding parameters, FFmpeg will default to using the CRF rate control mode with a CRF value = 18. You can extend this command to use CBR, VBR, or Capped VBR depending on your requirements.

Convert all the WAV files in your folder to MP3 format

for %f in (*.wav) do ffmpeg -i "%f" -b:a 128k -ar 44100 "%~nf_new.mp3"

The above command line will convert all the WAV files to MP3 with a 128 kbps bitrate and 44.1 kHz sampling rate.

Convert all the JPG files in your folder to PNG format

for %f in (*.jpg) do ffmpeg -i "%f" -q:v 80 -vf format=gray "%~nf_new.png"

This will pick up all the images with a .jpg extension and convert them to PNG format with 80% quality and grayscale mode.

Using Wildcards and Regular Expressions

You can also use wildcards (*) or regular expressions (^) to match multiple file extensions or patterns. To drive home the point, here are some direct examples that you can use or modify for your particular use cases.

Convert all the video files (MP4, AVI, MOV) in your folder to MKV format

for %f in (*.mp4 *.avi *.mov) do ffmpeg -i "%f" -c:v libx264 -c:a aac "%~nf_new.mkv"

Convert all the audio files (WAV, MP3, OGG) in your folder to MP3 format

for %f in (*.wav *.mp3 *.ogg) do ffmpeg -i "%f" -b:a 128k -ar 44100 "%~nf_new.mp3"

Convert all the image files (JPG, PNG, BMP) in your folder to PNG format

for %f in (*.jpg *.png *.bmp) do ffmpeg -i "%f" -q:v 80 -vf format=gray "%~nf_new.png"

Conclusion and Cautions

As you can see, FFmpeg is a very versatile and flexible tool that can handle almost any file format and conversion task. However, there are some caveats and limitations that you should be aware of when using FFmpeg to convert all the files inside a folder. Here are some of them:

  • Before running a batch command on your entire folder, please run it on a small, representative subset. If the command works as per your needs, then you can execute it on your entire folder.
  • FFmpeg does not check if the output file already exists and will overwrite it without warning. If you want to avoid this, you need to use additional options or tools, such as -n or if exist (https://ss64.com/nt/if.html).
  • FFmpeg does not delete the original files after the conversion, and will leave them in the folder. If you want to delete them, you need to use additional options or tools, such as -y or del (https://ss64.com/nt/del.html).
  • FFmpeg does not show the progress or status of the conversion, and will run silently in the background. If you want to see the progress or status, you need to use additional options or tools, such as -stats or pv (https://www.ivarch.com/programs/pv.shtml).

These are some of the tips and tricks that I have learned from using FFmpeg to convert all the files inside a folder. I hope you find them useful and helpful for your own projects. If you have any questions or feedback, please leave a comment below.

To learn more about FFmpeg, head over our Recipes in FFmpeg section.

Until next time, happy streaming!

krishna rao vijayanagar
Krishna Rao Vijayanagar
Founder at OTTVerse

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

1200x200-Pallycon

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.