fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -35,6 +35,7 @@ try:
|
||||
except ImportError:
|
||||
FFMPEG_AVAILABLE = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoFrame:
|
||||
"""视频关键帧数据类"""
|
||||
@@ -52,6 +53,7 @@ class VideoFrame:
|
||||
if self.entities_detected is None:
|
||||
self.entities_detected = []
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoInfo:
|
||||
"""视频信息数据类"""
|
||||
@@ -75,6 +77,7 @@ class VideoInfo:
|
||||
if self.metadata is None:
|
||||
self.metadata = {}
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoProcessingResult:
|
||||
"""视频处理结果"""
|
||||
@@ -87,6 +90,7 @@ class VideoProcessingResult:
|
||||
success: bool
|
||||
error_message: str = ""
|
||||
|
||||
|
||||
class MultimodalProcessor:
|
||||
"""多模态处理器 - 处理视频文件"""
|
||||
|
||||
@@ -122,8 +126,12 @@ class MultimodalProcessor:
|
||||
try:
|
||||
if FFMPEG_AVAILABLE:
|
||||
probe = ffmpeg.probe(video_path)
|
||||
video_stream = next((s for s in probe["streams"] if s["codec_type"] == "video"), None)
|
||||
audio_stream = next((s for s in probe["streams"] if s["codec_type"] == "audio"), None)
|
||||
video_stream = next(
|
||||
(s for s in probe["streams"] if s["codec_type"] == "video"), None
|
||||
)
|
||||
audio_stream = next(
|
||||
(s for s in probe["streams"] if s["codec_type"] == "audio"), None
|
||||
)
|
||||
|
||||
if video_stream:
|
||||
return {
|
||||
@@ -154,7 +162,9 @@ class MultimodalProcessor:
|
||||
return {
|
||||
"duration": float(data["format"].get("duration", 0)),
|
||||
"width": int(data["streams"][0].get("width", 0)) if data["streams"] else 0,
|
||||
"height": int(data["streams"][0].get("height", 0)) if data["streams"] else 0,
|
||||
"height": int(data["streams"][0].get("height", 0))
|
||||
if data["streams"]
|
||||
else 0,
|
||||
"fps": 30.0, # 默认值
|
||||
"has_audio": len(data["streams"]) > 1,
|
||||
"bitrate": int(data["format"].get("bit_rate", 0)),
|
||||
@@ -246,7 +256,9 @@ class MultimodalProcessor:
|
||||
|
||||
if frame_number % frame_interval_frames == 0:
|
||||
timestamp = frame_number / fps
|
||||
frame_path = os.path.join(video_frames_dir, f"frame_{frame_number:06d}_{timestamp:.2f}.jpg")
|
||||
frame_path = os.path.join(
|
||||
video_frames_dir, f"frame_{frame_number:06d}_{timestamp:.2f}.jpg"
|
||||
)
|
||||
cv2.imwrite(frame_path, frame)
|
||||
frame_paths.append(frame_path)
|
||||
|
||||
@@ -258,12 +270,26 @@ class MultimodalProcessor:
|
||||
Path(video_path).stem
|
||||
output_pattern = os.path.join(video_frames_dir, "frame_%06d_%t.jpg")
|
||||
|
||||
cmd = ["ffmpeg", "-i", video_path, "-vf", f"fps=1/{interval}", "-frame_pts", "1", "-y", output_pattern]
|
||||
cmd = [
|
||||
"ffmpeg",
|
||||
"-i",
|
||||
video_path,
|
||||
"-vf",
|
||||
f"fps=1/{interval}",
|
||||
"-frame_pts",
|
||||
"1",
|
||||
"-y",
|
||||
output_pattern,
|
||||
]
|
||||
subprocess.run(cmd, check=True, capture_output=True)
|
||||
|
||||
# 获取生成的帧文件列表
|
||||
frame_paths = sorted(
|
||||
[os.path.join(video_frames_dir, f) for f in os.listdir(video_frames_dir) if f.startswith("frame_")]
|
||||
[
|
||||
os.path.join(video_frames_dir, f)
|
||||
for f in os.listdir(video_frames_dir)
|
||||
if f.startswith("frame_")
|
||||
]
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error extracting keyframes: {e}")
|
||||
@@ -409,7 +435,9 @@ class MultimodalProcessor:
|
||||
if video_id:
|
||||
# 清理特定视频的文件
|
||||
for dir_path in [self.video_dir, self.frames_dir, self.audio_dir]:
|
||||
target_dir = os.path.join(dir_path, video_id) if dir_path == self.frames_dir else dir_path
|
||||
target_dir = (
|
||||
os.path.join(dir_path, video_id) if dir_path == self.frames_dir else dir_path
|
||||
)
|
||||
if os.path.exists(target_dir):
|
||||
for f in os.listdir(target_dir):
|
||||
if video_id in f:
|
||||
@@ -421,9 +449,11 @@ class MultimodalProcessor:
|
||||
shutil.rmtree(dir_path)
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
|
||||
|
||||
# Singleton instance
|
||||
_multimodal_processor = None
|
||||
|
||||
|
||||
def get_multimodal_processor(temp_dir: str = None, frame_interval: int = 5) -> MultimodalProcessor:
|
||||
"""获取多模态处理器单例"""
|
||||
global _multimodal_processor
|
||||
|
||||
Reference in New Issue
Block a user