Embed artwork with batch file using ffmpeg

I've the below code that embeds artwork with folder.jpg in the folder but it's not looping correctly. It's copying the file multiple times. So if there's only two mp3 files it runs until I close the cmd window.

If I REM out the line "%ffmpegExe%" -i "%%F".. it works and only echos the files and exits.

So, the issue is with this line below:

"%ffmpegExe%" -i "%%F" -i %jpgNameExt% -map 0 -map 1 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" "%%~nF_with_artwork.mp3" -hide_banner -loglevel error

Please be aware only test this on a folder with a couple of files as it copies the files over and over again.

rem DO NOT USE THIS CODE. IT'S BROKEN. FIXED IN CODE BELOW
rem DO NOT USE THIS CODE. IT'S BROKEN. FIXED IN CODE BELOW
rem DO NOT USE THIS CODE. IT'S BROKEN. FIXED IN CODE BELOW
rem DO NOT USE THIS CODE. IT'S BROKEN. FIXED IN CODE BELOW

@echo off
setlocal enabledelayedexpansion

REM Set the directory to start processing
set "rootDir=F:\Music\&ME"
REM Set the location of ffmpeg.exe
set "ffmpegExe=%cd%ffmpeg.exe"
set "jpgNameExt="folder.jpg""

REM Loop through all folders
for /d /r "%rootDir%" %%A in (*) do (

pushd "%%A"

REM Check if folder.jpg exists
if exist %jpgNameExt% (

rem REM Embed artwork to MP3 files
for %%F in (*.mp3) do (

  echo Adding artwork to: "%%~nF_with_artwork.mp3"
  "%ffmpegExe%" -i "%%F" -i %jpgNameExt% -map 0 -map 1 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" "%%~nF_with_artwork.mp3" -hide_banner -loglevel error

  )
)

popd

)

echo Processing complete.

Solved. As usual you ask a question on a forum and you figure it out straight away.

The other for loop in my OG post was looping while there were .mp3 files in the folder. Something I should of realised.

@echo off
setlocal enabledelayedexpansion

rem ABOUT
rem First written on 2024.03.27 by stevehero
rem This code will add artwork to .mp3 files recursively through the root folder variable below.
rem This reason I wrote this was to add artwork to the thumb drive for the car as it only supports embedded artwork.
rem PREREQUISITES
rem You will need to download ffmpeg.exe in order for this to work.

rem VERSION HISTORY
rem v1.0 - 2024.03.27

rem Set the variables of the program
set "sourceDir=F:\Music"
set "ffmpegExe=F:\ffmpeg.exe"
set "tempFolder=%TEMP%"

rem Push to the source directory
pushd "%sourceDir%"

for /f "delims=" %%F in ('dir /s /b /a-d-h-s') do (

  rem Push to the directory of the current folder
  pushd "%%~dpF"

  if "%%~xF"==".mp3" (

    set "filenameOrg=%%F"
    set "filenameTemp=%TEMP%\%%~nF.mp3"

    rem Copy the file to the temporary folder e.g: C:\Users\%username%\AppData\Local\Temp
    rem This reason for this is because ffmpeg cannot overwrite the original file
    copy /y "%%F" "%tempFolder%" >NUL

    rem Add the artwork to the temp files and overwrite the original ones
    echo Adding artwork to: "!filenameOrg!"
    "%ffmpegExe%" -y -i "!filenameTemp!" -i "folder.jpg" -map 0 -map 1 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" "!filenameOrg!" -hide_banner -loglevel error
    )

    rem Clean up temp folder of any temp files
    if exist "!filenameTemp!" (del "!filenameTemp!")

   )

popd

I'm just curious:
Why would you do that outside Mp3tag with a 3rd party tool if you can import a cover easily using the Mp3tag-GUI or the Mp3tag-Action "Import Cover from file"?

I read your reason

and see the need for your car, but not why you do it with ffmpeg.

Because I can click it and forget it. I have to first load the files in mp3tag.

I'll only use this for the first run and then mp3tag for the missing ones instead of iterating the whole music folder again.

I've can up against a problem with the ! character in the path.