- 🎬 智能转场检测:使用 TransNetV2 深度学习模型自动检测视频中的场景转换点
- ✂️ 自动视频分割:根据检测到的转场点自动将视频分割成多个场景片段
- 🚀 易于使用:提供命令行工具和 Python API 两种使用方式
- 💾 自动模型下载:首次使用时自动下载预训练模型
- 🖥️ GPU/CPU 支持:支持 CPU 和 GPU 推理,可根据环境自动选择
pip install transition_detectgit clone https://github.com/SWHL/TransitionDetect.git cd TransitionDetect pip install -e .- Python >= 3.6
- PyTorch
- ffmpeg-python
- 其他依赖见
requirements.txt
注意:使用前请确保系统已安装 FFmpeg。
transition_detect <视频路径> [--save_clips_dir <输出目录>]参数说明:
视频路径:要处理的视频文件路径(必需)--save_clips_dir:保存分割后视频片段的目录(可选)--threshold:float默认为0.2- 降低 threshold(如 0.3)→ 更敏感,检出更多转场(可能增加误报)。
- 提高 threshold(如 0.7)→ 更严格,只保留高置信度转场(可能漏检)。
示例:
# 仅检测转场点,不保存视频片段 transition_detect video.mp4 # 检测转场点并保存分割后的视频片段 transition_detect video.mp4 --save_clips_dir ./outputsfrom pathlib import Path from transition_detect.transnetv2 import TransNetV2Inference # 初始化模型(pypi安装时,自带模型) model = TransNetV2Inference() # 检测转场点(不保存视频片段) video_path = "video.mp4" result = model(video_path) print(result.scenes) # 转场点列表 print(result.predictions) # 预测结果 # 检测转场点并保存分割后的视频片段 # threshold:0.2 默认值。 output_dir = Path("outputs") result = model(video_path, save_video_clips_dir=output_dir, threshold=0.2)返回结果说明:
scenes:转场点列表,格式为[[start_frame, end_frame], ...]predictions:每帧的预测结果
# 指定 GPU ID(例如使用 GPU 0) model = TransNetV2Inference(gpu_id=0)查看 demo.py 文件了解完整的使用示例:
from pathlib import Path from transition_detect.transnetv2 import TransNetV2Inference model = TransNetV2Inference() video_path = "tests/test_files/demo.mp4" result = model(video_path, save_video_clips_dir="outputs") print("检测到的场景数量:", len(result.scenes)) print("场景列表:", result.scenes)TransitionDetect/ ├── transition_detect/ # 主包目录 │ ├── main.py # 命令行入口 │ ├── config.yaml # 配置文件 │ ├── transnetv2/ # TransNetV2 模型实现 │ │ ├── main.py # 推理接口 │ │ ├── network.py # 网络结构 │ │ └── typings.py # 类型定义 │ └── utils/ # 工具函数 │ ├── config.py # 配置加载 │ ├── download_file.py # 文件下载 │ ├── logger.py # 日志工具 │ └── utils.py # 通用工具 ├── tests/ # 测试文件 ├── docs/ # 文档 ├── requirements.txt # 依赖列表 └── setup.py # 安装配置 Apache-2.0 License
本项目基于 TransNetV2 模型实现,感谢原作者的贡献。