ffmpeg:批量转换子文件夹中的任何内容

时间:2016-03-20 23:51:53

标签: batch-file ffmpeg

我对此很陌生并且很难过:

这是我的文件结构:

.\input\
.\output\
convert.bat

.\input\包含子文件夹,这些子文件夹又包含.wav和其他音频格式。我试图让convert.bat将这些子文件夹中的所有文件转换为.mp3中的.\output\。此外,我需要将转换后的文件命名为子文件夹的名称,它来自+它的原始文件名。

如何使用FFMPEG对批处理文件执行此操作?非常感谢提前!

2 个答案:

答案 0 :(得分:3)

让我们假设包含批处理文件的文件夹C:\Temp具有以下文件夹结构和文件:

  • 输入
    • Folder.01
      • First File.wav
    • 我的文件夹
      • SecondFile.aac
    • Another.ac3
  • 输出
  • Convert.bat

批处理文件Convert.bat包含以下行:

@echo off
setlocal EnableDelayedExpansion

rem Input and output folder are in same directory as batch file.
set "InputFolder=%~dp0input"
set "OutputFolder=%~dp0output"
echo  Input folder is: %InputFolder%
echo Output folder is: %OutputFolder%
echo/

rem Search in input folder and all subfolders for the specified types
rem of audio files and output what is found with the output file name
rem in output folder. Delayed environment variable expansion is needed
rem for all variables set or modified within the body of the FOR loop.

for /R "%InputFolder%" %%I in (*.aac *.ac3 *.wav) do (
    set "InputFileName=%%~nxI"
    set "InputFilePath=%%~dpI"
    set "OutputFileName=!InputFilePath:~0,-1!"
    call :GetOutputFileName "!OutputFileName!" "%%~nI"
    echo  Input file name is: !InputFileName!
    echo  Input file path is: !InputFilePath!
    echo Output file name is: !OutputFileName!
    echo Output file path is: %OutputFolder%\
    echo --------
)

endlocal
echo/
pause
rem Exit batch processing and return to command process.
exit /B

rem Subroutine to get last folder name from first argument and append an
rem underscore, the file name of the input file without file extension
rem passed as second argument and the file extension MP3. But if the path
rem to the file is identical with input folder path, get just name of file
rem with different file extension.

:GetOutputFileName
if "%~1" == "%InputFolder%" (
    set "OutputFileName=%~2.mp3"
) else (
    set "OutputFileName=%~nx1_%~2.mp3"
)
rem Exit subroutine.
exit /B

执行此批处理文件会产生输出:

 Input folder is: C:\Temp\input
Output folder is: C:\Temp\output

 Input file name is: Another.ac3
 Input file path is: C:\Temp\input\
Output file name is: Another.mp3
Output file path is: C:\Temp\output\
--------
 Input file name is: SecondFile.aac
 Input file path is: C:\Temp\input\My Folder\
Output file name is: My Folder_SecondFile.mp3
Output file path is: C:\Temp\output\
--------
 Input file name is: First File.wav
 Input file path is: C:\Temp\input\Folder.01\
Output file name is: Folder.01_First File.mp3
Output file path is: C:\Temp\output\
--------

因此,您可以看到如何从输入文件名中获取所需的输出文件名。在调用之后,而不是所有 echo 命令,添加运行ffmpeg.exe的行,其中包含用于将找到的音频文件转换为输出目录中的MP3文件的相应参数。不要忘记输入和输出文件规范的双引号。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

答案 1 :(得分:2)

这是关于bash而不是ffmpeg

的问题
for f in dir/*.wav; do echo "./output/"$(echo "${f%.yml}.mp4"|tr / _); done

这会将dir目录中的所有文件以.wav结尾,并将其输出为mp3前缀为dir_的文件。例如,如果您有文件:

dir/audio1.wav
dir/whatever.wav

它会打印出来:

./output/dir_audio1.mp3
./output/dir_whatever.mp3

在你的情况下,你只需在ffmpeg循环中使用echo而不是for

for f in dir/*.wav; do ffmpeg -i "$f" ... "./output/"$(echo "${f%.yml}.mp4"|tr / _); done