#!/usr/bin/env python3 """ 视频拆分服务命令行接口 """ from typing import Dict, Any from dataclasses import asdict from .types import DetectionConfig, DetectorType from .service import VideoSplitterService from python_core.utils.commander import JSONRPCCommander class VideoSplitterCommander(JSONRPCCommander): """视频拆分服务命令行接口""" def __init__(self): self.service = None super().__init__("video_splitter") def _register_commands(self) -> None: """注册命令""" # 注册analyze命令 self.register_command( name="analyze", description="分析视频场景", required_args=["video_path"], optional_args={ "threshold": {"type": float, "default": 30.0, "description": "检测阈值"}, "detector": {"type": str, "default": "content", "choices": ["content", "threshold"], "description": "检测器类型"}, "min-scene-length": {"type": float, "default": 1.0, "description": "最小场景长度(秒)"}, "output-base": {"type": str, "default": None, "description": "输出基础目录"} } ) # 注册detect_scenes命令 self.register_command( name="detect_scenes", description="检测视频场景(仅返回场景信息)", required_args=["video_path"], optional_args={ "threshold": {"type": float, "default": 30.0, "description": "检测阈值"}, "detector": {"type": str, "default": "content", "choices": ["content", "threshold"], "description": "检测器类型"}, "min-scene-length": {"type": float, "default": 1.0, "description": "最小场景长度(秒)"}, "output-base": {"type": str, "default": None, "description": "输出基础目录"} } ) def _setup_service(self, output_base: str = None) -> None: """设置服务""" if self.service is None: self.service = VideoSplitterService(output_base_dir=output_base) def execute_command(self, command: str, args: Dict[str, Any]) -> Any: """执行命令""" # 设置服务 self._setup_service(args.get("output_base")) # 创建配置 config = DetectionConfig( threshold=args.get("threshold", 30.0), detector_type=DetectorType(args.get("detector", "content")), min_scene_length=args.get("min_scene_length", 1.0) ) video_path = args["video_path"] if command == "analyze": # 完整的视频分析 result = self.service.analyze_video(video_path, config) return result.to_dict() elif command == "detect_scenes": # 仅检测场景 result = self.service.analyze_video(video_path, config) # 只返回场景信息 scenes_result = { "success": result.success, "video_path": result.video_path, "total_scenes": result.total_scenes, "scenes": [asdict(scene) for scene in result.scenes], "detection_settings": asdict(config), "detection_time": result.analysis_time } if not result.success: scenes_result["error"] = result.error return scenes_result else: raise ValueError(f"Unknown command: {command}") def main(): """主函数""" commander = VideoSplitterCommander() commander.run() if __name__ == "__main__": main()