|
@@ -190,17 +190,36 @@ class GoogleAI(object):
|
|
|
def _analyze_content_with_api(cls, video_url):
|
|
|
"""使用API分析视频内容"""
|
|
|
try:
|
|
|
+ # 检查视频URL是否有效
|
|
|
+ if not video_url or not video_url.startswith('http'):
|
|
|
+ raise Exception("无效的视频URL")
|
|
|
+
|
|
|
+ # 获取视频文件以确定正确的MIME类型
|
|
|
+ try:
|
|
|
+ response = requests.head(video_url, timeout=10)
|
|
|
+ content_type = response.headers.get('content-type', '')
|
|
|
+ if not content_type or 'video' not in content_type.lower():
|
|
|
+ # 如果无法从HEAD请求获取正确的content-type,尝试GET请求
|
|
|
+ response = requests.get(video_url, stream=True, timeout=10)
|
|
|
+ content_type = response.headers.get('content-type', '')
|
|
|
+ if not content_type or 'video' not in content_type.lower():
|
|
|
+ content_type = 'video/mp4' # 默认使用mp4
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"[内容分析] 获取视频MIME类型失败: {str(e)}, 使用默认类型video/mp4")
|
|
|
+ content_type = 'video/mp4'
|
|
|
+
|
|
|
# 使用API分析视频内容
|
|
|
response = requests.post(
|
|
|
'http://ai-api.piaoquantv.com/aigc-server/gemini/generateContent',
|
|
|
json={
|
|
|
- "mediaUrl": video_url,
|
|
|
- "type": 2,
|
|
|
- "prompt": VIDEO_ANALYSIS_PROMPT,
|
|
|
- "model":"gemini-2.0-flash",
|
|
|
- "temperature":"0.3"
|
|
|
+ "mediaUrl": video_url,
|
|
|
+ "type": 2,
|
|
|
+ "prompt": VIDEO_ANALYSIS_PROMPT,
|
|
|
+ "model": "gemini-2.0-flash",
|
|
|
+ "temperature": "0.3",
|
|
|
+ "mimeType": content_type # 添加正确的MIME类型
|
|
|
},
|
|
|
- timeout=300 # 添加超时设置
|
|
|
+ timeout=300
|
|
|
)
|
|
|
response.raise_for_status()
|
|
|
result = response.json()
|
|
@@ -211,6 +230,13 @@ class GoogleAI(object):
|
|
|
|
|
|
if result.get('code') != 0:
|
|
|
error_msg = result.get('msg', '未知错误')
|
|
|
+ if 'data' in error_msg and 'error' in error_msg:
|
|
|
+ try:
|
|
|
+ error_data = orjson.loads(error_msg)
|
|
|
+ if isinstance(error_data, dict) and 'error' in error_data:
|
|
|
+ error_msg = f"API错误: {error_data['error'].get('message', error_msg)}"
|
|
|
+ except:
|
|
|
+ pass
|
|
|
raise Exception(f"API返回错误: {error_msg}")
|
|
|
|
|
|
if not result.get('data') or not result['data'].get('result'):
|