Batch mp3 to mp4 recursive

yusoffb01

Arch-Supremacy Member
Joined
Jun 17, 2008
Messages
16,702
Reaction score
1,715
Currently I use this script to convert mp3 inside mp3 folder into mp4 with album art
Code:
echo off
for %%a in ("*.mp3") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "E:\songs\mp3\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "E:\songs\mp3\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryfast -tune stillimage -crf 50 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "E:\songs\mp3\%%~na.mp4" )
pause

How do I change it so it can recursively process subfolders too?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Currently I use this script to convert mp3 inside mp3 folder into mp4 with album art
Code:
echo off
for %%a in ("*.mp3") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "E:\songs\mp3\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "E:\songs\mp3\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryfast -tune stillimage -crf 50 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "E:\songs\mp3\%%~na.mp4" )
pause

How do I change it so it can recursively process subfolders too?

There are a couple of different methods.
You can use powershell commands, you can also simply use the "FOR /R ..." command if you want to stay at a most outer level directory structure and perform your commands.

The following is a recursive approach using just windows batch command that will recursively go into each directory and process within that directory where your ffmpeg command will run in

That will allow keeping your input and output in the same directory

Code:
@echo off
setlocal enabledelayedexpansion

CALL :PROCESS_DIR "C:\Your Starting Directory"
pause

REM SUBROUTINE TO PROCESS THE DIRECTORY GIVEN AS FIRST PARAMETER
:PROCESS_DIR
pushd %1
echo INSIDE DIRECTORY !CD!
for %%F in ("*.mp3") do (
	REM THIS IS WHERE YOU DO YOUR PER FILE PROCESSING
	echo PROCESS FILE %%F TO %%~nF.mp4
)
REM FIND ALL DIRECTORIES IN THE CURRENT DIRECTORY AND RECURSIVELY CALL THE PROCESS_DIR SUBROUTINE
for /D %%D in ("*") do (
	CALL :PROCESS_DIR %%D
)
popd
EXIT /B

Here is a simpler one :)
Code:
@echo off
setlocal enabledelayedexpansion
pushd "C:\Your Starting Directory"

FOR /R %%F IN ("*.mp3") do (
	pushd %%~pF
	echo INSIDE DIRECTORY !CD!
	echo PROCESS FILE %%F TO %%~pF%%~nF.mp4
	popd
)

popd
pause

I will let others show you the powershell commands :)
 
Last edited:

yusoffb01

Arch-Supremacy Member
Joined
Jun 17, 2008
Messages
16,702
Reaction score
1,715
Thanks for the help!

I managed to change my initial command to work in any folder, but not recursively.
Code:
echo off
for %%a in ("*.flac") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "%~dp0\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "%~dp0\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryfast -tune stillimage -crf 20 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "%~dp0\%%~na.mp4" )
pause

So for files in the folder, it creates an image and combines with the audio into mp4.

when I tried to edit your command it did say file not found. How should I edit your command to fit in the 2 things.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Thanks for the help!

I managed to change my initial command to work in any folder, but not recursively.
Code:
echo off
for %%a in ("*.flac") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "%~dp0\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "%~dp0\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryfast -tune stillimage -crf 20 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "%~dp0\%%~na.mp4" )
pause

So for files in the folder, it creates an image and combines with the audio into mp4.

when I tried to edit your command it did say file not found. How should I edit your command to fit in the 2 things.

Maybe this will do it
Code:
@echo off
setlocal enabledelayedexpansion
pushd "C:\Your Starting Directory"
set FFMPEG="C:\ffmpeg\bin\ffmpeg.exe"

FOR /R %%F IN ("*.mp3") do (
	pushd %%~pF
	echo CUR DIR = !CD!
	echo PROCESS FILE %%~nxF TO %%~nF.mp4
	%FFMPEG% -i "%%~nxF" -an -y -vcodec copy "%%~nF.jpg"
	%FFMPEG% -loop 1 -i "%%~nF.jpg" -i "%%~nxF" -y -c:v libx264 -preset veryfast -tune stillimage -crf 20 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "%%~nF.mp4" )
	popd
)

popd
pause
 
Joined
May 11, 2017
Messages
62,346
Reaction score
11,768
Currently I use this script to convert mp3 inside mp3 folder into mp4 with album art
Code:
echo off
for %%a in ("*.mp3") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "E:\songs\mp3\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "E:\songs\mp3\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryfast -tune stillimage -crf 50 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "E:\songs\mp3\%%~na.mp4" )
pause

How do I change it so it can recursively process subfolders too?

how come never use spotify?
 

yusoffb01

Arch-Supremacy Member
Joined
Jun 17, 2008
Messages
16,702
Reaction score
1,715
Maybe this will do it
Code:
@echo off
setlocal enabledelayedexpansion
pushd "C:\Your Starting Directory"
set FFMPEG="C:\ffmpeg\bin\ffmpeg.exe"

FOR /R %%F IN ("*.mp3") do (
	pushd %%~pF
	echo CUR DIR = !CD!
	echo PROCESS FILE %%~nxF TO %%~nF.mp4
	%FFMPEG% -i "%%~nxF" -an -y -vcodec copy "%%~nF.jpg"
	%FFMPEG% -loop 1 -i "%%~nF.jpg" -i "%%~nxF" -y -c:v libx264 -preset veryfast -tune stillimage -crf 20 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 128k -shortest "%%~nF.mp4" )
	popd
)

popd
pause

THANKS! This works perfectly!
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top