123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- @ECHO OFF
- SETLOCAL EnableDelayedExpansion
- TITLE FFmpeg 壓製 Script
- FOR %%P IN ("ffmpeg.exe" "ffprobe.exe" "qaac64.exe") DO IF NOT EXIST "%~dp0bin\%%~P" (
- CLS
- ECHO 在 bin 目錄中找不到 %%~P。
- GOTO :error
- )
- IF EXIST "%~dp0\encoding.cfg" FOR /F "eol=; delims=" %%C IN ('TYPE "%~dp0\encoding.cfg"') DO SET "%%C"
- SET FFPROBECMD="%~dp0bin\ffprobe.exe" -show_entries stream=index^,codec_name^,bit_rate -select_streams a -of compact=p=0:nk=1 -loglevel error
- :nextarg
- IF NOT EXIST "%~1" (
- CLS
- ECHO 找不到檔案 %~1。
- GOTO :error
- )
- IF "%~x1"==".ass" SET "SUB=%~1" & GOTO :shift
- IF "%~x1"==".cfg" ( FOR /F "eol=; delims=" %%C IN ('TYPE "%~1"') DO SET "%%C" ) & GOTO :shift
- FOR %%X IN (".m4a" ".wav" ".mp3") DO IF "%~x1"=="%%~X" SET "AIN=%~1" & GOTO :shift
- IF "!VIN!"=="" (
- SET "VIN=%~1"
- SET "VDIR=%~dp1"
- SET "VNAME=%~n1"
- SET "VBASE=!VDIR!!VNAME!"
- ) ELSE SET "VIN_A=%~1" & GOTO :shift
- :shift
- SHIFT /1
- IF NOT "%~1"=="" GOTO :nextarg
- IF "%VIN%"=="" (
- CLS
- ECHO 找不到待處理的影片檔案。
- GOTO :error
- )
- IF "%AIN%"=="" (
- IF "%VIN_A%"=="" SET "VIN_A=%VIN%"
- SET "AIN=!VIN_A!"
- )
- :ffprobe
- FOR /F "tokens=1,2,3 delims=|" %%E IN ('"%FFPROBECMD% "%AIN%""') DO (
- IF "!AENC!"=="" SET "ADEC=%%F" & SET "ASTREAM=%%E" & SET "AENC=qaac"
- IF NOT "!AENC!"=="copy" IF "%%F"=="aac" IF %%G LEQ 192000 (
- SET "ADEC=%%F" & SET "ASTREAM=%%E" & SET "AENC=copy"
- )
- )
- IF "%ADEC%"=="" (
- IF "!AIN!"=="!VIN_A!" IF NOT "!VIN_A!"=="!VIN!" (
- SET "AIN=%VIN%" & SET "VIN=%VIN_A%"
- GOTO :ffprobe
- )
- CLS
- ECHO 沒有提供包含聲音的影片或獨立音訊檔案。
- GOTO :error
- )
- IF "%AENC%"=="copy" (
- SET "AINPUTCMD="%~dp0bin\ffmpeg" -y %IARGS% -i "%VIN%" -i "%AIN%" -map 0:v -map 1:%ASTREAM% %OARGS%"
- ) ELSE (
- 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%"
- )
- FOR %%V IN (FPS IARGS OARGS VENCARGS VOUT) DO CALL SET "%%V=!%%V!"
- IF "%VOUT%"=="" IF NOT "%SUB%"=="" ( SET "VOUT=%VBASE%_subbed.mp4" ) ELSE ( SET "VOUT=%VBASE%_muxed.mp4" )
- IF "%FPS%"=="" SET FPS=30
- IF "%VENCARGS_X264%"=="" ( SET VENCARGS_X264=-preset fast -crf 20 ) ELSE ( ECHO 載入 x264 編碼參數預設:%VENCARGS_X264% )
- IF "%VENCARGS_QSV%"=="" ( SET VENCARGS_QSV=-c:v h264_qsv -global_quality 19 ) ELSE ( ECHO 載入 QSV 編碼參數預設:%VENCARGS_X264% )
- IF "%VENCARGS_NVENC%"=="" ( SET VENCARGS_NVENC=-c:v h264_nvenc -preset slow -cq 19 -profile high ) ELSE ( ECHO 載入 NVENC 編碼參數預設:%VENCARGS_X264% )
- FOR %%X IN ("1|X264|x264" "2|QSV|Intel QSV" "3|NVENC|NVENC") DO (
- FOR /F "tokens=1,2,3 delims=|" %%P IN (%%X) DO SET "ENCSNAME[%%P]=%%Q" & SET "ENCLNAME[%%P]=%%R"
- )
- ECHO 源影片檔案:%VIN%
- ECHO 源聲音檔案:%AIN% (%ASTREAM%: %ADEC% -^> %AENC%)
- IF NOT "%SUB%"=="" ECHO 字幕檔案:%SUB% (libass)
- IF NOT "%VENCARGS%"=="" (
- ECHO 自訂編碼參數:%VENCARGS%
- ) ELSE IF NOT "%VENCPRESET%"=="" (
- ECHO 編碼參數預設:%VENCPRESET% ^(!ENCLNAME[%VENCPRESET%]!^)
- )
- IF NOT "%VFILTERS%"=="" ECHO 附加濾鏡組合:%VFILTERS%
- IF NOT "%IARGS%"=="" ECHO 附加輸入參數:%IARGS%
- IF NOT "%OARGS%"=="" ECHO 附加輸出參數:%OARGS%
- ECHO 目標影格速率:%FPS% fps
- ECHO 目標影片檔案:%VOUT%
- ECHO 請先確認以上資訊正確無誤,否則請按 Ctrl-C 中止 Script。
- PAUSE
- IF NOT "%SUB%"=="" SET SUBOPTS=^,ass=~tmp.ass
- IF NOT "%VENCARGS%"=="" GOTO :job
- IF NOT "%VENCPRESET%"=="" GOTO :parse
- ECHO 視訊編碼器:1. x264(預設) 2. Intel QSV 3. NVENC
- SET /P VENCPRESET="(留空則使用預設值 1):"
- IF "%VENCPRESET%"=="" GOTO :default
- :parse
- SET "OTHERS=" & FOR /F "delims=123" %%I IN ("%VENCPRESET%") DO SET "OTHERS=%%I"
- IF DEFINED OTHERS GOTO :illegal
- CALL SET "VENCARGS=%%VENCARGS_!ENCSNAME[%VENCPRESET%]!%%"
- GOTO :job
- :illegal
- ECHO 輸入有誤,使用預設值。
- :default
- SET "VENCARGS=%VENCARGS_X264%"
- :job
- SET "VENCARGS=%VENCARGS% -keyint_min "%FPS%" -g "%FPS%*10.01" -movflags faststart"
- IF NOT "%SUB%"=="" COPY /Y "%SUB%" "~tmp.ass"
- ECHO ON
- %AINPUTCMD% -vf "%VFILTERS%fps=fps=%FPS%,format=yuv420p%SUBOPTS%" -c:a copy %VENCARGS% "%VOUT%"
- @ECHO OFF
- IF NOT "%SUB%"=="" DEL "~tmp.ass"
- ECHO.
- PAUSE
- GOTO :EOF
- :error
- ECHO.
- TITLE [錯誤] FFmpeg 壓製 Script
- PAUSE
- GOTO :EOF
|