【问题标题】:Recursive ffmpeg batch script within WindowsWindows中的递归ffmpeg批处理脚本
【发布时间】:2013-09-30 15:36:57
【问题描述】:

彻底改变问题!

我现在有以下脚本:

@echo off
REM keep a counter for files converted
set /A nfile=0
REM do not copy empty folders or any files
@echo Copying directory structure from %0 to %1 ...
xcopy /T %1 %2
REM walk directory structure and convert each file in quiet mode
for /R %1 %%v in (*.mp4, *.aac, *.flv, *.m4a, *.mp3) do (
    echo converting "%%~nxv" ...
    ffmpeg -v quiet -i "%%v" -vcodec libx264 "%2\%%~nv-converted.mp4"
    set /A nfile+=1
)
echo Done! Converted %nfile% file(s)

取自:http://mostlybuggy.wordpress.com/2012/09/25/windows-batch-file-how-to-copy-and-convert-folders-recursively-with-ffmpeg/

当我运行以下命令时,视频正在按预期转换:

convert ..\..\folder ..\..\converted\

但是,转换后的文件最终会出现在“已转换”中,而不是在它们各自的子文件夹中。

有什么想法吗?

【问题讨论】:

    标签: windows batch-file video ffmpeg


    【解决方案1】:

    您需要将每个源文件的相对路径合并到您的目标中。如果您希望脚本是防弹的,这并非易事。

    下面未经测试的代码使用批处理函数来确定字符串的长度。 There are many techniques for computing string length in batch.

    一旦知道了根源路径的长度,就很容易对源文件的全路径进行子串操作,得到相对路径。

    @echo off
    setlocal disableDelayedExpansion
    
    REM get absolute path of source and destination roots
    for %%F in ("%~1\x") do set "src=%%~dpF"
    for %%F in ("%~2\x") do set "dst=%%~dpF"
    
    REM get length of root source path
    call :strlen src srcLen
    
    REM keep a counter for files converted
    set /A nfile=0
    REM do not copy empty folders or any files
    @echo Copying directory structure from %1 to %2 ...
    xcopy /T "%src%\." "%dst%\."
    
    REM walk directory structure and convert each file in quiet mode
    for /R "%src%" %%F in (*.mp4, *.aac, *.flv, *.m4a, *.mp3) do (
      set "file=%%~fF"
      set "file2=%%~dpnF"
      echo converting "%%~nxF" ...
      setlocal enableDelayedExpansion
      ffmpeg -v quiet -i "!file!" -vcodec libx264 "!dst!!file2:~%srcLen%!-converted.mp4"
      endlocal
      set /A nfile+=1
    )
    echo Done! Converted %nfile% file(s)
    exit /b
    
    
    :strlen  strVar  [rtnVar]
    setlocal EnableDelayedExpansion
    set "s=!%~1!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
      if "!s:~%%P,1!" NEQ "" (
        set /a "len+=%%P"
        set "s=!s:~%%P!"
      )
    )
    (
      endlocal
      if "%~2" equ "" (echo %len%) else set "%~2=%len%"
      exit /b
    )
    

    如果您有两个未分配的驱动器号可以映射到您的根源文件夹和目标文件夹,那么有一个非常简单的解决方案。这将允许您使用完整的绝对路径(没有驱动器号)。我假设 X: 和 Y: 可用

    @echo off
    setlocal disableDelayedExpansion
    
    REM map unused drive letters to source and destination
    for %%F in ("%~1\.") do subst x: "%%~fF"
    for %%F in ("%~2\.") do subst y: "%%~fF"
    
    REM keep a counter for files converted
    set /A nfile=0
    REM do not copy empty folders or any files
    @echo Copying directory structure from %1 to %2 ...
    xcopy /T x:\ y:\
    
    REM walk directory structure and convert each file in quiet mode
    for /R x:\ %%F in (*.mp4, *.aac, *.flv, *.m4a, *.mp3) do (
      echo converting "%%~nxF" ...
      ffmpeg -v quiet -i "%%F" -vcodec libx264 "y:%%~pnF-converted.mp4"
      set /A nfile+=1
    )
    echo Done! Converted %nfile% file(s)
    exit /b
    
    REM release mapped drive letters
    subst /d x:
    subst /d y:
    

    【讨论】:

    • 我将第二个示例用于驱动器映射,这是一个很棒的解决方案。然后我使用的命令是:so-convert ..\..\folder ..\..\converted\
    【解决方案2】:

    试试这个:

    @echo off &setlocal
    set /a nfile=0
    echo Copying directory structure from %1 to %2 ...
    xcopy /T "%~1" "%~2"
    REM walk directory structure and convert each file in quiet mode
    set "sourcefolder=%~1"
    set "targetfolder=%~2"
    for /R "%sourcefolder%" %%a in (*.mp4 *.aac *.flv *.m4a *.mp3) do (
        echo converting "%%~nxa" ...
        set "sourcefile=%%~fa"
        set "sourcepath=%%~dpa"
        set "targetfile=%%~na-converted.mp4"
        setlocal enabledelayedexpansion
        set "targetfolder=%targetfolder%!sourcepath:%sourcefolder%=!"
        ffmpeg -v quiet -i "!sourcefile!" -vcodec libx264 "!targetfolder!!targetfile!"
        endlocal
        set /A nfile+=1
    )
    echo Done! Converted %nfile% file(s)
    

    【讨论】:

    • 这只能在%1 包含完整的绝对源路径并且%1%2 都有尾部反斜杠时才能正常工作。即使解决了这个问题,如果 sourcepath 包含 =! 字符,搜索和替换技术也无法工作。
    猜你喜欢
    • 2021-05-30
    • 2022-07-02
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2016-05-13
    相关资源
    最近更新 更多