user8578415
user8578415

Reputation: 411

How do I batch convert files using ffmpeg?

I want to convert multiple .mp4 files to .mp3 using ffmpeg. Here is what I came up with:

cd (the folder I want to use)
set counter = 1
for /r %%file in (*) (
ffmpeg %%file -i "Track %counter%.mp3"
set counter = counter + 1
)

But it didn't work. I'm guessing I made some basic syntax errors, but I can't figure out what. What did I do wrong?

Upvotes: 1

Views: 15952

Answers (2)

Kencer
Kencer

Reputation: 11

  1. Create a file and save it with .bat extension
  2. Write the code below in it. You can change the beat rate as you like.
  3. Copy it in the directory containing your .mp4 files.
  4. Assuming the path to ffmpeg.exe is set in your environment variables, execute the batch file
  5. Locate converted files in the created folder "converted".

@echo off
mkdir converted
for %%a in ("*.mp4") do ffmpeg -i "%%a" -b:a 192K -vn "converted\%%~na.mp3"
pause

Upvotes: 1

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

  1. the spaces are meaningful in variable assigment, except in arithmetic /a expressions

    :: more correct set /a counter=1 :: works also because of /a set /a counter = 1

  2. the name of variable in for is limited to one alphabetic character a-z A-Z

    for /r %f

  3. the expansion in block (..) is done before the for command begins, to avoid this setlocal enabledelayedexpansions or calling a function, also using echo before command can help to debug

All together

:: cd (the folder I want to use)

set /a counter=1

for /r %%f in (*.*) do (
    call :proc %%f
    set /a counter=counter+1
)
:: to avoid to execute following functions
@goto :eof

:: functions

:proc
@echo ffmpeg -i %1 "Track %counter%.mp3"
@goto :eof

Upvotes: 1

Related Questions