|
|
@@ -359,19 +359,19 @@ def yolov12_predict(params: PredictParams):
|
|
|
for ext in ['*.avi', '*.webm', '*.mov']:
|
|
|
video_files.extend(glob.glob(os.path.join(save_dir, ext)))
|
|
|
|
|
|
- # 如果找到非MP4视频文件,直接使用ffmpeg转换为MP4
|
|
|
+ # 如果找到非MP4视频文件,尝试转换为MP4,失败则转换为WebM
|
|
|
for video_file in video_files:
|
|
|
output_mp4 = video_file.rsplit('.', 1)[0] + '.mp4'
|
|
|
+ output_webm = video_file.rsplit('.', 1)[0] + '.webm'
|
|
|
try:
|
|
|
import subprocess
|
|
|
|
|
|
- # 使用ffmpeg转换为浏览器兼容的MP4格式
|
|
|
+ # 使用ffmpeg转换为浏览器兼容的MP4格式(简化参数)
|
|
|
cmd = [
|
|
|
'ffmpeg', '-i', video_file,
|
|
|
'-c:v', 'libx264', # H.264编码器
|
|
|
- '-preset', 'fast', # 快速编码
|
|
|
- '-crf', '23', # 质量设置
|
|
|
- '-movflags', '+faststart', # 优化网络播放
|
|
|
+ '-preset', 'ultrafast', # 最快编码
|
|
|
+ '-crf', '28', # 稍低质量但更兼容
|
|
|
'-pix_fmt', 'yuv420p', # 兼容性像素格式
|
|
|
'-y', output_mp4
|
|
|
]
|
|
|
@@ -393,9 +393,37 @@ def yolov12_predict(params: PredictParams):
|
|
|
os.remove(video_file)
|
|
|
logging.info(f"✓ 成功转换为H.264 MP4: {output_mp4}")
|
|
|
else:
|
|
|
- logging.warning(f"生成的MP4可能不是H.264编码: {output_mp4}")
|
|
|
+ logging.warning(f"生成的MP4可能不是H.264编码,尝试转换为WebM")
|
|
|
+ # 尝试转换为WebM
|
|
|
+ webm_cmd = [
|
|
|
+ 'ffmpeg', '-i', video_file,
|
|
|
+ '-c:v', 'libvpx',
|
|
|
+ '-crf', '30',
|
|
|
+ '-b:v', '0',
|
|
|
+ '-y', output_webm
|
|
|
+ ]
|
|
|
+ webm_result = subprocess.run(webm_cmd, capture_output=True, text=True)
|
|
|
+ if webm_result.returncode == 0:
|
|
|
+ os.remove(video_file)
|
|
|
+ logging.info(f"✓ 成功转换为WebM: {output_webm}")
|
|
|
+ else:
|
|
|
+ logging.error(f"WebM转换也失败: {webm_result.stderr}")
|
|
|
else:
|
|
|
- logging.error(f"ffmpeg转换失败: {result.stderr}")
|
|
|
+ logging.error(f"MP4转换失败,尝试转换为WebM: {result.stderr}")
|
|
|
+ # 尝试转换为WebM
|
|
|
+ webm_cmd = [
|
|
|
+ 'ffmpeg', '-i', video_file,
|
|
|
+ '-c:v', 'libvpx',
|
|
|
+ '-crf', '30',
|
|
|
+ '-b:v', '0',
|
|
|
+ '-y', output_webm
|
|
|
+ ]
|
|
|
+ webm_result = subprocess.run(webm_cmd, capture_output=True, text=True)
|
|
|
+ if webm_result.returncode == 0:
|
|
|
+ os.remove(video_file)
|
|
|
+ logging.info(f"✓ 成功转换为WebM: {output_webm}")
|
|
|
+ else:
|
|
|
+ logging.error(f"WebM转换也失败: {webm_result.stderr}")
|
|
|
|
|
|
# 如果ffmpeg失败,尝试使用OpenCV作为备选方案
|
|
|
logging.info("尝试使用OpenCV作为备选方案")
|
|
|
@@ -472,12 +500,15 @@ def yolov12_predict(params: PredictParams):
|
|
|
except Exception as e:
|
|
|
logging.error(f"转换视频格式时出错: {e}")
|
|
|
|
|
|
- # 获取MP4文件
|
|
|
- mp4_files = glob.glob(os.path.join(save_dir, "*.mp4"))
|
|
|
- if mp4_files:
|
|
|
- latest_mp4 = max(mp4_files, key=os.path.getmtime)
|
|
|
- final_filename = os.path.basename(latest_mp4)
|
|
|
- logging.info(f"输入为视频,返回MP4文件: {final_filename}")
|
|
|
+ # 获取MP4或WebM文件
|
|
|
+ video_output_files = []
|
|
|
+ for ext in ['*.mp4', '*.webm']:
|
|
|
+ video_output_files.extend(glob.glob(os.path.join(save_dir, ext)))
|
|
|
+
|
|
|
+ if video_output_files:
|
|
|
+ latest_video = max(video_output_files, key=os.path.getmtime)
|
|
|
+ final_filename = os.path.basename(latest_video)
|
|
|
+ logging.info(f"输入为视频,返回文件: {final_filename}")
|
|
|
|
|
|
# 如果无法确定输入类型或未找到文件,返回最新文件
|
|
|
if not final_filename:
|