在Windows上维护元数据和目录结构时使用FFmpeg递归地将FLAC转换为MP3

时间:2017-03-18 12:17:30

标签: batch-file cmd ffmpeg

我尝试使用FFmpeg将音乐库中的所有.flac转换为.mp3,同时保留原始文件的元数据和目录结构。到目前为止,我已经完成了所有工作,除了维护目录结构

到目前为止,我有:

@echo off
rem Copies the original directory structure from F:\Music to D:\Music
xcopy /T %1 %2
rem Recursively converts files with .flac extension from F:\Music to .mp3 in D:\Music
for /R %1 %%i in (*.flac) do (
    echo Currently converting "%%~nxi"
    ffmpeg -v quiet -i "%%i" -q:a 0 -map_metadata 0 -id3v2_version 3 -write_id3v1 1 "%2\%%~ni.mp3"
)
echo Completed.
pause

我觉得我的问题来自于我的FFmpeg输出为"%2\%%~ni.mp3",但我不能保持原始目录结构。我已经使用dpni作为输出的选项,但它没有正确转换。

正在从命令行执行.bat convert F:\Music D:\Music

原始目录中的所有音乐都具有F:\ Music \ Artist Name \ Album Name \ Song.flac结构,我希望它最终为D:\ Music \ Artist Name \ Album Name \ Song.mp3。< / p>

或者,是否有办法将.flac转换为与源文件位于同一目录中的.mp3,然后删除flac?

2 个答案:

答案 0 :(得分:0)

当for / R递归到子翻牌时,原始%2不包含这些子文件夹名称,请尝试:

ffmpeg -v quiet -i "%%~fi" -q:a 0 -map_metadata 0 -id3v2_version 3 -write_id3v1 1 "%~d2%%~pni.mp3"

答案 1 :(得分:0)

这是一个通用的解决方案,当源和目标根目录的驱动器号不仅不同时,它们也有效:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=F:\Music"
set "_TARGET=D:\Music"

rem // Change to source directory temporarily:
pushd "%_SOURCE%" || exit /B 1
rem // Enumerate source files, return paths relative to the source directory:
for /F "delims=" %%F in ('xcopy /L /S /I ".\*.flac" "%_TARGET%" ^| find ".\"') do (
    echo Currently converting "%%F"...
    rem // Create destination directory, suppress error if it aready exists:
    mkdir "%_TARGET%\%%F\.." 2> nul
    rem // Perform actual file conversion, using paths relative to target directory:
    ffmpeg -v quiet -i "%%F" -q:a 0 -map_metadata 0 -id3v2_version 3 ^
        -write_id3v1 1 "%_TARGET%\%%F\..\%%~nF.mp3"
)
echo Completed.
popd

endlocal
exit /B