|
|
@@ -362,59 +362,36 @@ def yolov12_predict(params: PredictParams):
|
|
|
for video_file in video_files:
|
|
|
output_mp4 = video_file.rsplit('.', 1)[0] + '.mp4'
|
|
|
try:
|
|
|
- import subprocess
|
|
|
-
|
|
|
- # 使用ffmpeg转换为MP4
|
|
|
- cmd = [
|
|
|
- 'ffmpeg', '-i', video_file,
|
|
|
- '-c:v', 'libx264',
|
|
|
- '-preset', 'ultrafast',
|
|
|
- '-crf', '28',
|
|
|
- '-pix_fmt', 'yuv420p',
|
|
|
- '-y', output_mp4
|
|
|
- ]
|
|
|
-
|
|
|
- logging.info(f"使用ffmpeg转换视频: {video_file} -> {output_mp4}")
|
|
|
- result = subprocess.run(cmd, capture_output=True, text=True)
|
|
|
-
|
|
|
- if result.returncode == 0:
|
|
|
- os.remove(video_file)
|
|
|
- logging.info(f"✓ 成功转换为MP4: {output_mp4}")
|
|
|
- else:
|
|
|
- logging.error(f"ffmpeg转换失败: {result.stderr}")
|
|
|
-
|
|
|
- except (FileNotFoundError, subprocess.SubprocessError) as e:
|
|
|
- logging.error(f"ffmpeg不可用: {e}")
|
|
|
-
|
|
|
- # 如果ffmpeg不可用,尝试使用OpenCV
|
|
|
- try:
|
|
|
- import cv2
|
|
|
- cap = cv2.VideoCapture(video_file)
|
|
|
- fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
-
|
|
|
- # 尝试使用H264编码器
|
|
|
- fourcc = cv2.VideoWriter_fourcc(*'H264')
|
|
|
+ # 直接用 OpenCV 尝试多种编码器
|
|
|
+ import cv2
|
|
|
+ cap = cv2.VideoCapture(video_file)
|
|
|
+ fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
+ try_codecs = ['H264', 'mp4v', 'MJPG']
|
|
|
+ out = None
|
|
|
+ for codec in try_codecs:
|
|
|
+ fourcc = cv2.VideoWriter_fourcc(*codec)
|
|
|
out = cv2.VideoWriter(output_mp4, fourcc, fps, (width, height))
|
|
|
-
|
|
|
if out.isOpened():
|
|
|
- while cap.isOpened():
|
|
|
- ret, frame = cap.read()
|
|
|
- if not ret:
|
|
|
- break
|
|
|
- out.write(frame)
|
|
|
-
|
|
|
- cap.release()
|
|
|
- out.release()
|
|
|
- os.remove(video_file)
|
|
|
- logging.info(f"使用OpenCV H264编码器生成MP4: {output_mp4}")
|
|
|
+ break
|
|
|
else:
|
|
|
- logging.error("OpenCV H264编码器不可用")
|
|
|
-
|
|
|
- except Exception as cv_error:
|
|
|
- logging.error(f"OpenCV处理失败: {cv_error}")
|
|
|
-
|
|
|
+ out.release()
|
|
|
+ out = None
|
|
|
+ if out is None:
|
|
|
+ raise RuntimeError("没有可用的 MP4 编码器,请安装 ffmpeg 或检查 OpenCV 编码支持。")
|
|
|
+ while cap.isOpened():
|
|
|
+ ret, frame = cap.read()
|
|
|
+ if not ret:
|
|
|
+ break
|
|
|
+ out.write(frame)
|
|
|
+ cap.release()
|
|
|
+ out.release()
|
|
|
+ os.remove(video_file)
|
|
|
+ logging.info(f"使用OpenCV生成MP4: {output_mp4}")
|
|
|
+ except Exception as cv_error:
|
|
|
+ logging.error(f"OpenCV处理失败: {cv_error}")
|
|
|
+
|
|
|
except Exception as e:
|
|
|
logging.error(f"转换视频格式时出错: {e}")
|
|
|
|