Search code examples
batch-filecmdffmpeg

Batch Album Art embedding with FFmpeg


I want to convert all Flacs in a folder to ALAC m4a and embed the Album Art

With this code it embeds the same jpg into every m4a, how can I instead extract the album art from each flac and then have it embedded into the corresponding m4a?

@echo off
for %%G in (*.flac) do ffmpeg -i "%%G" -map 0:v -codec copy cover.jpg
for %%F in (*.flac) do ffmpeg -i "%%F" -vn -acodec alac "%%~nF.m4a"
(FOR /F "tokens=*" %%E IN ('dir /b *.m4a') DO atomicparsley "%%E" --artwork cover.jpg --overWrite)
(del cover.jpg)
pause

Solution

  • One solution is replacing cover.jpg in second, fourth and fifth line as shown below:

    @echo off
    for %%G in (*.flac) do ffmpeg -i "%%G" -map 0:v -codec copy "%%~nG.jpg"
    for %%F in (*.flac) do ffmpeg -i "%%F" -vn -acodec alac "%%~nF.m4a"
    for /F "eol=| delims=*" %%E in ('dir /b *.m4a 2^>nul') do atomicparsley "%%E" --artwork "%%~nE.jpg" --overWrite
    del /Q "*.jpg"
    pause
    

    But a better solution is using just one FOR loop instead of three FOR loops with three different loop variables.

    @echo off
    for %%I in (*.flac) do (
        ffmpeg.exe -i "%%I" -map 0:v -codec copy "%%~nI.jpg"
        if exist "%%~nI.jpg" ffmpeg.exe -i "%%I" -vn -acodec alac "%%~nI.m4a"
        if exist "%%~nI.ma4" atomicparsley.exe "%%I" --artwork "%%~nI.jpg" --overWrite
        if exist "%%~nI.jpg" del "%%~nI.jpg"
    )
    pause
    

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • del /?
    • echo /?
    • for /?
    • if /?
    • pause /?

    The documentations of FFmpeg and AtomicParsley should be also read by everyone using these two applications which was not done by me because I don't use them.

    PS: It would be better to specify ffmpeg.exe and atomicparsley.exe with full qualified file name (drive + path + name + extension) enclosed in double quotes. Then Windows command processor cmd.exe would not need to search for these two executables using the environment variables PATHEXT and PATH in the loop before every execution.