2013-09-30 36 views
0

完整更改問題!Windows中的遞歸ffmpeg批處理腳本

我現在有以下腳本:

@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\ 

然而,轉換文件最終處於「已轉換」狀態,而不在其各自的子文件夾中。

任何想法?

回答

3

你需要把每一個源文件的相對路徑到你的目的地。如果你想要腳本是防彈的,那麼做這件事並不是微不足道的。

下面未經測試的代碼使用批處理函數來確定字符串的長度。 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 
) 

有一個非常簡單的解決方案,如果你有一個可以映射到你的根源和目標文件夾2未分配驅動器號。這將允許您使用完整的絕對路徑(不帶驅動器盤符)。我假設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: 
@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: 
+0

我用驅動器映射的第二個例子,這是一個奇妙的解決方案。然後我使用的命令是:so-convert .. \ .. \ folder .. \ .. \ converted \ – Tisch

0

試試這個:

@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) 
+0

如果'%1'包含完整的絕對源路徑並且'%1'和'%2'都有反斜槓,則只能按寫入方式工作。即使問題得到解決,如果sourcepath包含「=」或「!」字符,搜索和替換技術也無法工作。 – dbenham

+0

@dbenham謝謝。 – Endoro