將所有素材拖曳到這裡.bat 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. @ECHO OFF
  2. SETLOCAL EnableDelayedExpansion
  3. TITLE FFmpeg 壓製 Script
  4. FOR %%P IN ("ffmpeg.exe" "ffprobe.exe" "qaac64.exe") DO IF NOT EXIST "%~dp0bin\%%~P" (
  5. CLS
  6. ECHO 在 bin 目錄中找不到 %%~P。
  7. GOTO :error
  8. )
  9. IF EXIST "%~dp0\encoding.cfg" FOR /F "eol=; delims=" %%C IN ('TYPE "%~dp0\encoding.cfg"') DO SET "%%C"
  10. SET FFPROBECMD="%~dp0bin\ffprobe.exe" -show_entries stream=index^,codec_name^,bit_rate -select_streams a -of compact=p=0:nk=1 -loglevel error
  11. :nextarg
  12. IF NOT EXIST "%~1" (
  13. CLS
  14. ECHO 找不到檔案 %~1。
  15. GOTO :error
  16. )
  17. IF "%~x1"==".ass" SET "SUB=%~1" & GOTO :shift
  18. IF "%~x1"==".cfg" ( FOR /F "eol=; delims=" %%C IN ('TYPE "%~1"') DO SET "%%C" ) & GOTO :shift
  19. FOR %%X IN (".m4a" ".wav" ".mp3") DO IF "%~x1"=="%%~X" SET "AIN=%~1" & GOTO :shift
  20. IF "!VIN!"=="" (
  21. SET "VIN=%~1"
  22. SET "VDIR=%~dp1"
  23. SET "VNAME=%~n1"
  24. SET "VBASE=!VDIR!!VNAME!"
  25. ) ELSE SET "VIN_A=%~1" & GOTO :shift
  26. :shift
  27. SHIFT /1
  28. IF NOT "%~1"=="" GOTO :nextarg
  29. IF "%VIN%"=="" (
  30. CLS
  31. ECHO 找不到待處理的影片檔案。
  32. GOTO :error
  33. )
  34. IF "%AIN%"=="" (
  35. IF "%VIN_A%"=="" SET "VIN_A=%VIN%"
  36. SET "AIN=!VIN_A!"
  37. )
  38. :ffprobe
  39. FOR /F "tokens=1,2,3 delims=|" %%E IN ('"%FFPROBECMD% "%AIN%""') DO (
  40. IF "!AENC!"=="" SET "ADEC=%%F" & SET "ASTREAM=%%E" & SET "AENC=qaac"
  41. IF NOT "!AENC!"=="copy" IF "%%F"=="aac" IF %%G LEQ 192000 (
  42. SET "ADEC=%%F" & SET "ASTREAM=%%E" & SET "AENC=copy"
  43. )
  44. )
  45. IF "%ADEC%"=="" (
  46. IF "!AIN!"=="!VIN_A!" IF NOT "!VIN_A!"=="!VIN!" (
  47. SET "AIN=%VIN%" & SET "VIN=%VIN_A%"
  48. GOTO :ffprobe
  49. )
  50. CLS
  51. ECHO 沒有提供包含聲音的影片或獨立音訊檔案。
  52. GOTO :error
  53. )
  54. IF "%AENC%"=="copy" (
  55. SET "AINPUTCMD="%~dp0bin\ffmpeg" -y %IARGS% -i "%VIN%" -i "%AIN%" -map 0:v -map 1:%ASTREAM% %OARGS%"
  56. ) ELSE (
  57. SET "AINPUTCMD="%~dp0bin\ffmpeg" %IARGS% -i "%AIN%" -map 0:%ASTREAM% %OARGS% -vn -sn -c:a pcm_s16le -f wav - | "%~dp0bin\qaac64" -q 2 --ignorelength -c 192 - --adts -o - | "%~dp0bin\ffmpeg" -y %IARGS% -i "%VIN%" -i - -bsf:a aac_adtstoasc -map 0:v -map 1:0 %OARGS%"
  58. )
  59. FOR %%V IN (FPS IARGS OARGS VENCARGS VOUT) DO CALL SET "%%V=!%%V!"
  60. IF "%VOUT%"=="" IF NOT "%SUB%"=="" ( SET "VOUT=%VBASE%_subbed.mp4" ) ELSE ( SET "VOUT=%VBASE%_muxed.mp4" )
  61. IF "%FPS%"=="" SET FPS=30
  62. IF "%VENCARGS_X264%"=="" ( SET VENCARGS_X264=-preset fast -crf 20 ) ELSE ( ECHO 載入 x264 編碼參數預設:%VENCARGS_X264% )
  63. IF "%VENCARGS_QSV%"=="" ( SET VENCARGS_QSV=-c:v h264_qsv -global_quality 19 ) ELSE ( ECHO 載入 QSV 編碼參數預設:%VENCARGS_X264% )
  64. IF "%VENCARGS_NVENC%"=="" ( SET VENCARGS_NVENC=-c:v h264_nvenc -preset slow -cq 19 -profile high ) ELSE ( ECHO 載入 NVENC 編碼參數預設:%VENCARGS_X264% )
  65. FOR %%X IN ("1|X264|x264" "2|QSV|Intel QSV" "3|NVENC|NVENC") DO (
  66. FOR /F "tokens=1,2,3 delims=|" %%P IN (%%X) DO SET "ENCSNAME[%%P]=%%Q" & SET "ENCLNAME[%%P]=%%R"
  67. )
  68. ECHO 源影片檔案:%VIN%
  69. ECHO 源聲音檔案:%AIN% (%ASTREAM%: %ADEC% -^> %AENC%)
  70. IF NOT "%SUB%"=="" ECHO 字幕檔案:%SUB% (libass)
  71. IF NOT "%VENCARGS%"=="" (
  72. ECHO 自訂編碼參數:%VENCARGS%
  73. ) ELSE IF NOT "%VENCPRESET%"=="" (
  74. ECHO 編碼參數預設:%VENCPRESET% ^(!ENCLNAME[%VENCPRESET%]!^)
  75. )
  76. IF NOT "%VFILTERS%"=="" ECHO 附加濾鏡組合:%VFILTERS%
  77. IF NOT "%IARGS%"=="" ECHO 附加輸入參數:%IARGS%
  78. IF NOT "%OARGS%"=="" ECHO 附加輸出參數:%OARGS%
  79. ECHO 目標影格速率:%FPS% fps
  80. ECHO 目標影片檔案:%VOUT%
  81. ECHO 請先確認以上資訊正確無誤,否則請按 Ctrl-C 中止 Script。
  82. PAUSE
  83. IF NOT "%SUB%"=="" SET SUBOPTS=^,ass=~tmp.ass
  84. IF NOT "%VENCARGS%"=="" GOTO :job
  85. IF NOT "%VENCPRESET%"=="" GOTO :parse
  86. ECHO 視訊編碼器:1. x264(預設) 2. Intel QSV 3. NVENC
  87. SET /P VENCPRESET="(留空則使用預設值 1):"
  88. IF "%VENCPRESET%"=="" GOTO :default
  89. :parse
  90. SET "OTHERS=" & FOR /F "delims=123" %%I IN ("%VENCPRESET%") DO SET "OTHERS=%%I"
  91. IF DEFINED OTHERS GOTO :illegal
  92. CALL SET "VENCARGS=%%VENCARGS_!ENCSNAME[%VENCPRESET%]!%%"
  93. GOTO :job
  94. :illegal
  95. ECHO 輸入有誤,使用預設值。
  96. :default
  97. SET "VENCARGS=%VENCARGS_X264%"
  98. :job
  99. SET "VENCARGS=%VENCARGS% -keyint_min "%FPS%" -g "%FPS%*10.01" -movflags faststart"
  100. IF NOT "%SUB%"=="" COPY /Y "%SUB%" "~tmp.ass"
  101. ECHO ON
  102. %AINPUTCMD% -vf "%VFILTERS%fps=fps=%FPS%,format=yuv420p%SUBOPTS%" -c:a copy %VENCARGS% "%VOUT%"
  103. @ECHO OFF
  104. IF NOT "%SUB%"=="" DEL "~tmp.ass"
  105. ECHO.
  106. PAUSE
  107. GOTO :EOF
  108. :error
  109. ECHO.
  110. TITLE [錯誤] FFmpeg 壓製 Script
  111. PAUSE
  112. GOTO :EOF