0

In Windows, how can I batch convert m4a files to mp3 using ffmpeg ? I use the following line in the command line:

FOR I in (*.m4a) DO ffmpeg -i %I -codec:v copy -codec:a libmp3lame -q:a 2 "mp3\%I"

But I get the error "I is not expected at this moment".

4
  • 1
    could you try to replace FOR I by FOR %I ?
    – S. Brottes
    Jan 31, 2021 at 15:05
  • @S.Brottes I get the error "Transaction: No such file or directory".
    – user270124
    Jan 31, 2021 at 15:09
  • does the subdirectory mp3 exist?
    – DavidPostill
    Jan 31, 2021 at 15:54
  • Yes, the subdir exists.
    – user270124
    Jan 31, 2021 at 15:59

1 Answer 1

0

I find Powershell much more legible for code like this, and you're using Windows anyways so Powershell is built-in to your OS.

For example, I have my albums organized artist\album\title.mp3

This is a slightly-generalized version of the script I just wrote to solve the same problem:

#assume the current directory is my music directory. So like `cd ~/music` or whatever.

$ffmpegPath = 'c:\tools\ffmpeg\bin\ffmpeg.exe' # or you can just call ffmpeg.exe directly if it's in the $Env:PATH

ls *.m4a -Recurse | foreach-object {
  $artist = $_.directory.parent.name
  $album = $_.directory.name
  $song = $_.baseName
  $outputDir = "./output/$artist/$album"
  mkdir $outputDir -Force | Out-Null #use -Force to prevent error if it already exists.

  & $ffmpegPath -i $_ -codec:v copy -codec:a libmp3lame -q:a 2 "$outputDir/$song.mp3"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .