How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?
Of course, now PowerShell has come along, specifically designed to make something exactly like this extremely easy.
And, yes, PowerShell is also available on other operating systems other than just Windows, but it comes pre-installed on Windows, so this should be useful to everyone.
First, you'll want to list all of the files within the current directory, so, we'll start off with:
ls
You can also use ls -Recurse
if you want to recursively convert all files in subdirectories too.
Then, we'll filter those down to only the type of file we want to convert - e.g. "avi".
ls | Where { $_.Extension -eq ".avi" }
After that, we'll pass that information to FFmpeg through a ForEach
.
For FFmpeg's input, we will use the FullName
- that's the entire path to the file. And for FFmpeg's output we will use the Name
- but replacing the .avi
at the end with .mp3
. So, it will look something like this:
$_.Name.Replace(".avi", ".mp3")
So, let's put all of that together and this is the result:
ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
That will convert all ".avi" files into ".mp3" files through FFmpeg, just replace the three things in quotes to decide what type of conversion you want, and feel free to add any other arguments to FFmpeg within the ForEach
.
You could take this a step further and add Remove-Item
to the end to automatically delete the old files.
If ffmpeg
isn't in your path, and it's actually in the directory you're currently in, write ./ffmpeg
there instead of just ffmpeg
.
Hope this helps anyone.