Convert All Files Inside a Folder using FFmpeg (Batch Convert) - OTTVerse (2024)

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.

Table of Contents

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 convertedfor 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 ourRecipes in FFmpeg section.

Until next time, happy streaming!

Convert All Files Inside a Folder using FFmpeg (Batch Convert) - OTTVerse (1)

Krishna Rao Vijayanagar

Founderat 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.

Related

Convert All Files Inside a Folder using FFmpeg (Batch Convert) - OTTVerse (2024)

FAQs

How to batch convert files in FFmpeg? ›

Here's the explanation:
  1. mkdir converted : This creates a directory named 'converted' for our output files.
  2. for f in *. mp4 : This loops through all . mp4 files.
  3. ffmpeg -i "$f" "converted/${f%. mp4}. avi" : FFmpeg will convert each file and store it in the 'converted' folder with a . avi extension.
Sep 1, 2023

How do I convert bulk files? ›

How to create PDF from BLK
  1. Go to the DocHub site and sign in to your account. ...
  2. Create the document you would like to convert: upload it from your device or link it from its cloud storage location.
  3. Use DocHub instruments to create PDF from BLK.

How does FFmpeg work? ›

ffmpeg calls the libavformat library (containing demuxers) to read input files and get packets containing encoded data from them. When there are multiple input files, ffmpeg tries to keep them synchronized by tracking lowest timestamp on any active input stream.

How to do batch files? ›

How do I create a batch file? To create a batch file, open a text editor like Notepad and write your commands one line at a time. Save the file with a . bat extension, and you've created a batch file.

Can you convert multiple files at once? ›

Save time and effort by using a single tool for batch conversions instead of processing files one by one. In the file browser, choose the files you want to convert. You can select multiple files at once for efficient batch processing.

How do I convert all files to text? ›

If you want absolutely every file within a given folder changed to a txt extension, you can open a command window in that folder and type ren *. * *. txt and it will change them all to . txt files, overwriting their original extensions.

How do I convert different storage amounts? ›

The base unit of data storage, representing a single character or 8 bits. For example, to convert 5 GB to MB using the binary system: Determine the conversion factor: 1 GB = 1024 MB. Multiply the original value (5 GB) by the conversion factor: 5 * 1024 = 5120 MB.

How to batch convert with VLC? ›

You can click the Media tab at the top menu bar to enter the Open Media window, select the Open Multiple Files… option or the Convert/Save… option, and click + Add to choose the MP4 files you want to convert. Then click the inverted arrow next to Play to select the Convert option to access the Convert window.

Can I convert file to video? ›

You can convert almost any file to MP4 and WEBM using Restream's free online video converter tool. Select the Choose File to get started.

What is better than FFmpeg? ›

Explore other competing options and alternatives. Other important factors to consider when researching alternatives to FFmpeg include videos. The best overall FFmpeg alternative is Handbrake. Other similar apps like FFmpeg are PotPlayer, VideoLan, AWS Elemental MediaTailor, and Compressor.

How to use FFmpeg to convert videos? ›

Convert video formats using FFmpeg
  1. -i input. mp4 : Replace input. ...
  2. -c:v video_codec : Specify the video codec for the output. Replace video_codec with the desired video codec (e.g., libx265 for H. ...
  3. -c:a audio_codec : Specify the audio codec for the output. ...
  4. output.
Feb 5, 2024

How to speed up FFmpeg conversion? ›

We can easily speed up a video using the setpts filter in FFmpeg as follows. This command reduces the timestamps by half, effectively doubling the video speed. You can adjust the multiplier according to your needs. A smaller value will speed up the video more.

How to convert a batch file? ›

How to Convert Batch Files to EXE With a Converter
  1. Download the free BAT to EXE Converter and install it on your PC.
  2. After you've launched the tool, click on Open and select the batch file you'd like to convert.
  3. Next, click on Convert and give a name to the location of your file.
  4. Finally, click on Save.
Jan 19, 2023

How to batch convert files to PDF? ›

Open Adobe Acrobat Pro. Check your conversion settings to make sure they match what you want for your batch. Choose File > Create > Create Multiple PDF Files. Choose Add Files and then use the dropdown menu at the top of the window to indicate if you'll be choosing files or folders.

References

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6006

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.