对视频,按黑屏检测然后分割为多个小视频的批处理:
@echo off
setlocal enabledelayedexpansion
:: 检查参数
if "%~1"=="" (
echo 用法: %~n0 输入文件 [输出前缀 [黑屏时长]]
echo 示例:
echo %~n0 input.mp4 :: 输出 output_0.mp4, output_1.mp4...
echo %~n0 input.mp4 segment :: 输出 segment_0.mp4, segment_1.mp4...
echo %~n0 input.mp4 segment 1.0 :: 黑屏检测长度为1秒钟
exit /b 1
)
:: 设置参数
set input_file=%~1
set output_prefix=%~n1
set sec=2.0
if not "%~2"=="" set output_prefix=%~2
if not "%~3"=="" set sec=%~3
:: 检查输入文件
if not exist "%input_file%" (
echo 错误: 输入文件 '%input_file%' 不存在!
exit /b 1
)
:: 临时黑屏检测结果文件
set black_file="%temp%\blackdetect_%random%.txt"
:: 检测黑屏并生成时间点
echo 输入文件: %input_file%, 输出前缀: %output_prefix%, 黑屏时长: %sec%
echo 检测黑屏中,请耐心等待...
ffmpeg -hide_banner -i "%input_file%" -vf "blackdetect=d=%sec%:pix_th=0.10" -an -f null - 2>&1 | find "blackdetect" > %black_file%
:: 检查是否检测到黑屏
for /f %%i in ('type "%black_file%" ^| find /c /v ""') do set linecount=%%i
if %linecount% equ 0 (
echo 未检测到黑屏片段!
del "%black_file%" >nul 2>&1
exit /b 0
)
echo 找到黑屏片段,开始切割...
:: 提取时间点并切割
set "prev_end=0"
set "i=1"
for /f "tokens=*" %%a in ('type "%black_file%" ^| find "blackdetect"') do (
set "line=%%a"
set "temp=!line:*black_start:=!"
set "temp=!temp: black_end:=:!"
set "temp=!temp: black_duration:=:!"
for /f "tokens=1,2 delims=: " %%b in ("!temp!") do (
set "start=%%b"
set "end=%%c"
)
:: 切割非黑屏部分
set "output_file=%output_prefix%_!i!.mp4"
echo ffmpeg -hide_banner -noaccurate_seek -i %input_file% -ss "!prev_end!" -to "!start!" -c copy -avoid_negative_ts 1 !output_file! -y
ffmpeg -hide_banner -noaccurate_seek -i %input_file% -ss "!prev_end!" -to "!start!" -c copy -avoid_negative_ts 1 !output_file! -y -v quiet
set "prev_end=!end!"
set /a i+=1
)
:: 切割最后一段(黑屏后的内容)
set "output_file=%output_prefix%_!i!.mp4"
echo ffmpeg -hide_banner -noaccurate_seek -i %input_file% -ss "!prev_end!" -c copy -avoid_negative_ts 1 !output_file! -y
ffmpeg -hide_banner -noaccurate_seek -i %input_file% -ss "!prev_end!" -c copy -avoid_negative_ts 1 !output_file! -y -v quiet
:: 清理临时文件
rem del "%black_file%" >nul 2>&1
echo 切割指令输出完成,共生成 !i! 个文件。
endlocal