Skip to content

Commit

Permalink
feat(video_processor): 解决竖版视频解析出错;优化视频压缩处理
Browse files Browse the repository at this point in the history
- 添加对横版和竖版视频的区分,使用不同的缩放比例
- 获取原始视频的宽度和高度,用于确定缩放比例
- 改进 FFmpeg 命令的执行,增加错误处理和日志记录
  • Loading branch information
linyqh committed Nov 23, 2024
1 parent be20d9d commit 14e2d13
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/utils/video_processor_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def process_video(self, output_dir: str, skip_seconds: float = 0, threshold: int
Args:
output_dir: 输出目录
skip_seconds: 跳过视频开头的秒数
skip_seconds: 跳过视���开头的秒数
"""
skip_frames = int(skip_seconds * self.fps)

Expand Down Expand Up @@ -265,14 +265,30 @@ def process_video_pipeline(self,
video_name = os.path.splitext(os.path.basename(self.video_path))[0]
compressed_video = os.path.join(compressed_dir, f"{video_name}_compressed.mp4")

# 获取原始视频的宽度和高度
original_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
original_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

logger.info("步骤1: 压缩视频...")
if original_width > original_height:
# 横版视频
scale_filter = f'scale={compressed_width}:-1'
else:
# 竖版视频
scale_filter = f'scale=-1:{compressed_width}'

ffmpeg_cmd = [
'ffmpeg', '-i', self.video_path,
'-vf', f'scale={compressed_width}:-1',
'-vf', scale_filter,
'-y',
compressed_video
]
subprocess.run(ffmpeg_cmd, check=True)

try:
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
logger.error(f"FFmpeg 错误输出: {e.stderr}")
raise

# 2. 从压缩视频中提取关键帧
logger.info("\n步骤2: 从压缩视频提取关键帧...")
Expand Down

0 comments on commit 14e2d13

Please sign in to comment.