123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- @ECHO OFF
- SETLOCAL EnableDelayedExpansion
- TITLE FFmpeg 压制脚本
- 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 中止脚本。
- 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 压制脚本
- PAUSE
- GOTO :EOF
|