-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrop_video.bat
More file actions
116 lines (99 loc) · 3.4 KB
/
Copy pathCrop_video.bat
File metadata and controls
116 lines (99 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
rem 片头裁剪开始时间(秒)
set StartTime=95
rem 片尾需裁掉时长(秒)
set TailTrimSeconds=126
rem === 以下配置可由用户修改 ===
rem FFmpeg 路径
set ffmpeg=.\ffmpeg.exe
rem 源、输出、临时 目录
set source_folder=.\Videos
set output_folder=.\Output
set Temp_folder=.\Temp
rem 创建输出与临时目录
if not exist "%output_folder%" mkdir "%output_folder%"
if not exist "%Temp_folder%" mkdir "%Temp_folder%"
rem 校验 FFmpeg 可用
set "FFMPEG_BIN="
if exist "%ffmpeg%" (
set "FFMPEG_BIN=%ffmpeg%"
) else (
where ffmpeg >nul 2>&1 && set "FFMPEG_BIN=ffmpeg"
)
if not defined FFMPEG_BIN (
echo [ERROR] 未找到 FFmpeg;请配置 ffmpeg.exe 路径或加入 PATH
goto :end
)
rem 校验源目录与 MP4 文件
if not exist "%source_folder%" (
echo [ERROR] 源目录不存在: %source_folder%
goto :end
)
dir /b "%source_folder%\*.mp4" >nul 2>&1
if errorlevel 1 (
echo [WARN] 未找到 MP4 文件: %source_folder%
goto :end
)
set /a processed=0
rem 遍历处理每个视频文件
for %%i in ("%source_folder%\*.mp4") do (
set source_file=%%i
set Temp_file=!Temp_folder!\temp_%%~nxi
set output_file="%output_folder%\%%~nxi"
set "skip_current="
if exist "!Temp_file!" del /q "!Temp_file!" >nul 2>&1
rem 裁剪片头到临时文件
"%FFMPEG_BIN%" -v error -hide_banner -y -i "!source_file!" -ss %StartTime% -c copy "!Temp_file!"
if errorlevel 1 (
echo [ERROR] 片头裁剪失败: "!source_file!"
if exist "!Temp_file!" del /q "!Temp_file!" >nul 2>&1
set "skip_current=1"
)
if not defined skip_current (
rem 使用 FFmpeg 读取视频时长
set "duration_line="
for /f "tokens=*" %%d in ('"%FFMPEG_BIN%" -i "!Temp_file!" 2^>^&1 ^| find "Duration"') do (
set duration_line=%%d
)
if not defined duration_line (
echo [ERROR] 无法获取视频时长: "!Temp_file!"
set "skip_current=1"
)
)
if not defined skip_current (
set duration=!duration_line:Duration=!
set duration=!duration:~,12!
rem 将 hh:mm:ss 转为秒
for /f "tokens=1-3 delims=:" %%a in ("!duration!") do (
set /a total_seconds=%%a * 3600 + %%b * 60 + %%c
)
set /a ClipDuration=total_seconds-TailTrimSeconds
if !ClipDuration! LEQ 0 (
echo [WARN] 尾部裁剪时长不合理,直接复制输出文件: "!output_file!"
copy /y "!Temp_file!" !output_file! >nul
if errorlevel 1 (
echo [ERROR] 复制文件失败: "!output_file!"
) else (
echo [OK] 已输出: "!output_file!"
)
) else (
"%FFMPEG_BIN%" -v error -hide_banner -y -i "!Temp_file!" -ss 0 -t !ClipDuration! -c copy !output_file!
if errorlevel 1 (
echo [ERROR] 尾部裁剪失败: "!source_file!"
) else (
echo [OK] 已输出: "!output_file!"
)
)
)
rem 清理临时文件
if exist "!Temp_file!" del /q "!Temp_file!" >nul 2>&1
set /a processed+=1
)
if %processed% EQU 0 (
echo 未处理任何文件
) else (
echo 全部处理完成,共处理 %processed% 个文件
)
pause