将文件合并为 MKV 时,我的批处理脚本无法匹配封面 JPG 图像

将文件合并为 MKV 时,我的批处理脚本无法匹配封面 JPG 图像

我编写了代码,我想将其用于我的档案。感谢您帮助我改进它。

包含文件的文件夹如下(例如):

得到文件 夹/S01,,/s02.../s0n

/S01/01.mkv, 02.mkv,..., cover.jpg
/S02/01.mkv, 02.mkv,..., cover.jpg

每个子文件夹中都有一个名为的文件cover.jpg,我想将其附加到该文件夹​​中的所有文件,但我无法修改代码以使其正常工作。

编写的代码无法识别cover.jpg每个文件夹中的。

以下是代码:

@ECHO OFF
For /R %%A IN (*.mkv
) Do (
  "C:/Program Files/MKVToolNix\mkvmerge.exe" -o "%%~dpnxAtmp" --no- ^
  attachments --no-chapters --no-global-tags --no-subtitles --title ^"^" ^
  --language 0:und --language 1:en --track-name 0: --track-name 1: "%%~fA" ^
  --attachment-name cover.jpg --attachment-mime-type image/jpeg --attach-file ^ 
  "%*cover.jpg"
  if errorlevel 2 ( 
      echo errors generated during remuxing, original file not deleted
    ) else if errorlevel 1 (
      del/f "%%~fA" echo Warnings generated during remuxing to "%%~A", original file deleted
    ) else (
      del/f "%%~fA" echo Successfully remuxed to "%%~A", original file deleted
    ) & for /R %%A IN (*.mkvtmp) do ren "%%~fA" "%**.mkv" & CLS
)
pause

答案1

@echo off && setlocal 

cd /d "%~dp0" & title <nul & title .\%~nx0 

for /f tokens^=* %%A IN ('where /r . *.mkv^|%__AppDir__%findstr.exe/vei mkvtmp
')Do (call ) &<con: "C:\Program Files\MKVToolNix\mkvmerge.exe" -o "%%~fAtmp" ^
 --no-attachments --no-chapters --no-global-tags --no-subtitles --title ^"^" ^
 --language 0:und --language 1:en --track-name 0: --track-name 1: "%%~dpnxA" ^
 --attachment-name cover.jpg --attachment-mime-type image/jpeg --attach-file ^
 "%%~dpAcover.jpg" & call %:^) %errorlevel% "%%~fA"

for /R %%A IN (*.mkvtmp)do >nul move /y "%%~fA" "%%~dpnA.mkv"
<con: endlocal && %__AppDir__%timeout.exe /t -1 && goto :EOF 
     
%:^)
if %~1 equ 0 ( 
      del /q /f "%~2" && echo[Successfully remuxed to "%~nx2", original file deleted
    ) else if %~1 equ 1 ( echo[errors generated during remuxing, original file not deleted
    ) else if %~1 equ 2 echo[Warnings generated during remuxing to "%~nx2", original file deleted
      exit /b 

1.你需要SETLOCAL

ErrorLevel
When run from a batch file, SETLOCAL will always set an ERRORLEVEL.
If given a valid argument or no arguments, a new environment is created %ERRORLEVEL% = 0
If bad parameters given, %ERRORLEVEL% = 1

2.替换for /R为(for /F递归where /r)列出所有文件.mkv避免.mkvtmp文件findstr /v

for /f tokens^=* %%A IN ('where /r . *.mkv^|%__AppDir__%findstr.exe/vei mkvtmp

3.errorlevel使用(call )// credit to开始循环重置@aschipfl

... ')Do (call ) &<con: ... 

4.实现一个函数,您可以将当前文件errorlevel作为参数传递(%~1 == %errorlevel%),也可以将当前文件作为参数传递(%~2 == %%~fA

%:^)
if %~1 equ 0 ( 
      ...
    ) else if %~1 equ 1 ( ...
    ) else if %~1 equ 2 ...
      exit /b 

5.在这个函数(标签:^))内部,让errorlevel/%1参数按条件处理if else if else

%:^)
if %~1 equ 0 ( 
      del /q /f "%~2" && echo[Successfully remuxed to "%~nx2", original file deleted
    ) else if %~1 equ 1( echo[errors generated during remuxing, original file not deleted
    ) else if %~1 equ 2 echo[Warnings generated during remuxing to "%~nx2", original file deleted
      exit /b 

观察:1"%*cover.jpg"在您的代码中替换您的"%%~dpAcover.jpg"循环for /f (or /R)mkvmerge.exe不幸的是他们不会根据这些字符串翻译或选择文件%*cover.jpg

观察:2最后一个命令(ren)不会替换现有文件,您可以尝试使用目标名称ren替换move /y"%**.mkv" "%%~dpnA.mkv"

for /R %%A IN (*.mkvtmp) do ren "%%~fA" "%**.mkv"

for /R %%A IN (*.mkvtmp)do >nul move /y "%%~fA" "%%~dpnA.mkv"

其他资源:

相关内容