Batch Convert Files with FFmpeg and PowerShell
This guide provides a straightforward walkthrough on how to write and execute an FFmpeg command to batch convert media files on Windows using PowerShell. You will learn the exact script syntax required to loop through a directory of files and convert them to your desired format efficiently.
To batch convert files in Windows PowerShell, you combine the
Get-ChildItem cmdlet (which locates your files) with the
ForEach-Object loop (which runs the FFmpeg command on each
file).
Open PowerShell, navigate to the folder containing your media files
using the cd command, and run the following command:
Get-ChildItem -Filter *.mkv | ForEach-Object { ffmpeg -i $_.FullName -codec copy ($_.BaseName + ".mp4") }How the Command Works
Get-ChildItem -Filter *.mkv: This searches the current directory for all files with the.mkvextension. You can change*.mkvto any format you want to convert from, such as*.wavor*.flac.ForEach-Object { ... }: This instructs PowerShell to perform the action inside the brackets for every file found.ffmpeg -i $_.FullName: This calls FFmpeg.$_.FullNamerepresents the absolute file path of the current file in the loop, ensuring FFmpeg finds the correct input file.-codec copy: This is an optional FFmpeg argument that copies the audio and video streams without re-encoding them, making the conversion nearly instant. Replace this with specific codecs (like-c:v libx264 -c:a aac) if you need to transcode the files.($_.BaseName + ".mp4"): This defines the output filename.$_.BaseNamegets the filename without its original extension, and+ ".mp4"appends the new extension.
Batch Converting Files in Subfolders (Recursive)
If your files are organized in subfolders and you want to convert all
of them at once, add the -Recurse parameter to the search
cmdlet:
Get-ChildItem -Filter *.wav -Recurse | ForEach-Object { ffmpeg -i $_.FullName ($_.DirectoryName + "\" + $_.BaseName + ".mp3") }Using $_.DirectoryName + "\" ensures that the newly
converted .mp3 files are saved in the exact same subfolder
as their original .wav counterparts.