How can I convert multiple audio files with FFmpeg in PowerShell?
Asked by
MellowCedar42
I'm new to programming and trying to convert WAV, MP3, and OGG files so they're recognized by iTunes. I tried running this command in PowerShell:
for (%f in (*.wav *.mp3 *.ogg)) {do {ffmpeg -i "%f" -b:a 128k -ar 44100 "%~nf_new.mp3"}}
PowerShell reports "Missing while or until keyword in do loop." What is the correct way to run this conversion?
Answers
Since your terminal opens PowerShell, use PowerShellโs file enumeration instead:
`Get-ChildItem -File -Include *.wav,*.mp3,*.ogg | ForEach-Object { ffmpeg -i $_.FullName -b:a 128k -ar 44100 (Join-Path $_.DirectoryName ($_.BaseName + '_new.mp3')) }`
Run it from the folder containing the audio files. `Get-ChildItem` finds the files, and `ForEach-Object` runs FFmpeg once for each one.
Reply to this answer
The command mixes Windows batch syntax with PowerShell syntax. In a regular Command Prompt or batch file, the loop would be written without the braces: `for %f in (*.wav *.mp3 *.ogg) do ffmpeg -i "%f" -b:a 128k -ar 44100 "%~nf_new.mp3"`. If you put it in a `.bat` file, use `%%f` instead of `%f`. Thereโs no need for a `do`/`while` loop here.
Reply to this answer
The error happens because PowerShell interprets `do` as the beginning of a do/while or do/until loop, but your command never provides the required `while` or `until` condition. More importantly, `%f` and `%~nf` are batch-file variables, not PowerShell variables. Use one shellโs syntax consistently instead of combining the two.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowCedar42: That explains itโI had combined an old batch tutorial with a little C++ syntax from memory. Iโll use the PowerShell version since thatโs what opens in the folder.