Convert Video File Format in Batch Script

Convert Video File Format in Batch Script

  1. Script Without Providing the Directory
  2. Convert All the Files With One Batch File

You can convert files from one format to another in a directory. But you can automate this task in the Batch, which allows you to convert all the files into a directory using one Batch script.

This article will convert all the files in .avi format to .mp4 format. Here we use a special keyword, ffmpeg, that allows you to convert from one file format to another.

Also, we will see an example code with an explanation to make it easier to understand.

Script Without Providing the Directory

In the example shared below, we can convert files one by one, and you need to run the script on the directory where the files are. The code for our example will look like the below.

@
FOR i IN *.avi;
DO ffmpeg -i "$i" "${i%.*}.mp4";
DONE

Here we used a for loop to convert all files with format .avi to .mp4, and we used the keyword .ffmpeg that converts the files.

Convert All the Files With One Batch File

This example is a bit more advanced than our previous example. In this example, we will provide the directory of files in our Batch script, and you don’t have to run the script on the directory where the files are.

The code for our example will look like this:

@echo off
PUSHD "D:\YourDirectory\"
FOR %%a IN (*) DO (
  ffmpeg -i "%%~fa" "%%~na.mp3"
)

In the example, we convert all the file formats to the .mp3. Here, the keyword PUSHD is used to include the user-provided directory in the script.

We used the symbol * to indicate all file types.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Batch Script

  • Run Multiple Commands Simultaneously in Batch Script
  • Extract or Unzip File in Batch Script
  • Run Batch Script in C#
  • Replace Text From File in Batch Script
  • Run Batch Script in Silent Mode
  • Download File From URL in Batch Script