【问题标题】:ffmpeg: Batch convert anything in subfoldersffmpeg:批量转换子文件夹中的任何内容
【发布时间】:2016-03-20 23:51:53
【问题描述】:

我是新手,遇到了困难:

这是我的文件结构:

.\input\
.\output\
convert.bat

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

如何使用 FFMPEG 处理批处理文件?提前非常感谢!

【问题讨论】:

  • 明天我会看看这个......对我来说已经晚了,但我知道的是,如果我们使用通配符,我们可以实现你想要的代码。通配符可以让我们编辑字符串,从特定位置读取它们,或者不关心它之前的内容而是后面的内容。下面是一些可能对学习有用的通配符以及批处理语言的文档。 [1]:ss64.com/nt/syntax-wildcards.html
  • 对不起,应该是批处理文件
  • @Mofi 谢谢...我一直在查看其他帖子并尝试将其与您的命令放在一起,但未能将文件命名为原始目录 + 原始文件名。

标签: batch-file ffmpeg


【解决方案1】:

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

  • 输入
    • 文件夹.01
      • 第一个文件.wav
    • 我的文件夹
      • SecondFile.aac
    • 另一个.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\
--------

所以你可以看到如何从输入文件名中得到想要的输出文件名。在 call 之后添加运行 ffmpeg.exe 的行,而不是所有 echo 命令,并使用适当的参数将找到的音频文件转换为输出目录中的 MP3 文件。不要忘记输入和输出文件规范的双引号。

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

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

【讨论】:

    【解决方案2】:

    这是一个关于bash而不是ffmpeg的问题:

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

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

    dir/audio1.wav
    dir/whatever.wav
    

    它会打印出来:

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

    在这种情况下,您只需在 for 循环中使用 ffmpeg 而不是 echo

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

    【讨论】:

    • 不幸的是,这个问题最初是用bash 标记的,但用户实际上想要 Windows 批处理。
    猜你喜欢
    • 2013-04-15
    • 2019-08-02
    • 2015-12-19
    • 2016-09-10
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多