How can I convert multiple audio files with FFmpeg in PowerShell?

0
0
Asked By MellowCedar42 On

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?

3 Answers

Answered By SilverPine8 On

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.

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.

Answered By BrightHarbor7 On

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.

Answered By QuietMaple19 On

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.

Related Questions

Online Audio Cleanup Tool

Extract Audio From Video File

Compress MP3 File

Online Audio Converter

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.