mxivideo/scripts/install_scenedetect.py

162 lines
5.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
安装PySceneDetect的脚本
用于改进视频场景检测功能
"""
import subprocess
import sys
import os
from pathlib import Path
def install_scenedetect():
"""安装PySceneDetect库"""
print("🎬 安装PySceneDetect...")
try:
# 尝试导入,检查是否已安装
import scenedetect
print("✅ PySceneDetect已经安装")
print(f"版本: {scenedetect.__version__}")
return True
except ImportError:
print("📦 PySceneDetect未安装开始安装...")
try:
# 安装PySceneDetect
subprocess.check_call([
sys.executable, '-m', 'pip', 'install', 'scenedetect[opencv]'
])
print("✅ PySceneDetect安装成功")
# 验证安装
try:
import scenedetect
print(f"✅ 验证成功,版本: {scenedetect.__version__}")
return True
except ImportError:
print("❌ 安装验证失败")
return False
except subprocess.CalledProcessError as e:
print(f"❌ 安装失败: {e}")
return False
except Exception as e:
print(f"❌ 安装过程中出现错误: {e}")
return False
def test_scenedetect():
"""测试PySceneDetect功能"""
print("\n🧪 测试PySceneDetect功能...")
try:
from scenedetect import VideoManager, SceneManager
from scenedetect.detectors import ContentDetector
print("✅ 导入成功")
# 查找测试视频文件
project_root = Path(__file__).parent.parent
test_video_paths = [
project_root / "assets" / "templates" / "template1" / "resources" / "video1.mp4",
project_root / "assets" / "templates" / "template2" / "resources" / "video1.mp4",
"/root/.mixvideo/temp/video_segments",
]
test_video = None
for path in test_video_paths:
if isinstance(path, str):
# 查找目录中的视频文件
if os.path.exists(path):
for file in os.listdir(path):
if file.lower().endswith(('.mp4', '.avi', '.mov')):
test_video = os.path.join(path, file)
break
else:
if path.exists():
test_video = str(path)
break
if test_video:
print(f"🎬 使用测试视频: {test_video}")
# 创建视频管理器
video_manager = VideoManager([test_video])
scene_manager = SceneManager()
# 添加检测器
scene_manager.add_detector(ContentDetector(threshold=30.0))
# 开始检测
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
# 获取结果
scene_list = scene_manager.get_scene_list()
print(f"✅ 检测到 {len(scene_list)} 个场景")
if scene_list:
print("📋 场景列表:")
for i, scene in enumerate(scene_list[:5]): # 只显示前5个
start = scene[0].get_seconds()
end = scene[1].get_seconds()
print(f" 场景 {i+1}: {start:.2f}s - {end:.2f}s")
video_manager.release()
print("✅ PySceneDetect功能测试成功")
return True
else:
print("⚠️ 没有找到测试视频文件,跳过功能测试")
return True
except Exception as e:
print(f"❌ 功能测试失败: {e}")
import traceback
traceback.print_exc()
return False
def show_usage_info():
"""显示使用信息"""
print("\n📖 PySceneDetect使用信息:")
print("1. PySceneDetect是一个专业的视频场景检测库")
print("2. 相比OpenCV的简单帧差检测它提供更准确的场景分割")
print("3. 支持多种检测算法ContentDetector、ThresholdDetector等")
print("4. 可以自动检测淡入淡出、硬切等转场类型")
print("\n🔧 配置参数:")
print("- threshold: 场景变化敏感度 (默认30.0)")
print(" - 较低值 (10-20): 更敏感,检测更多场景变化")
print(" - 较高值 (40-50): 不太敏感,只检测明显的场景变化")
print("\n📚 更多信息:")
print("- 官方文档: https://pyscenedetect.readthedocs.io/")
print("- GitHub: https://github.com/Breakthrough/PySceneDetect")
def main():
"""主函数"""
print("🎬 PySceneDetect安装和测试工具")
print("=" * 50)
# 1. 安装PySceneDetect
if not install_scenedetect():
print("❌ 安装失败,退出")
return
# 2. 测试功能
if not test_scenedetect():
print("❌ 功能测试失败")
return
# 3. 显示使用信息
show_usage_info()
print("\n✅ 所有步骤完成!")
print("\n🚀 现在可以使用改进的场景检测功能了:")
print("- 重新导入视频素材将使用PySceneDetect进行更准确的分镜")
print("- 如果PySceneDetect不可用系统会自动回退到OpenCV方法")
if __name__ == "__main__":
main()