84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
"""
|
|
测试 Gemini API 视频识别
|
|
"""
|
|
import base64
|
|
import requests
|
|
import json
|
|
|
|
# 配置
|
|
API_URL = "https://api.functen.cn/v1beta/models/gemini-3-pro-preview:generateContent"
|
|
API_KEY = "sk-NeOtq0kOU39x3LMqY09aKYLoOBJIgFkgGuDwVGgGEstXPn3M"
|
|
VIDEO_PATH = r"D:\project\shrobot\WechatHookBot\plugins\AIChat\265d0df9ea89578bcb86f824c5255a42.mp4"
|
|
|
|
def test_video():
|
|
# 读取视频并编码为 base64
|
|
print(f"读取视频: {VIDEO_PATH}")
|
|
with open(VIDEO_PATH, "rb") as f:
|
|
video_data = f.read()
|
|
|
|
video_base64 = base64.b64encode(video_data).decode()
|
|
print(f"视频大小: {len(video_data) / 1024 / 1024:.2f} MB")
|
|
print(f"Base64 长度: {len(video_base64)}")
|
|
|
|
# 构建 Gemini 原生格式请求
|
|
payload = {
|
|
"contents": [
|
|
{
|
|
"parts": [
|
|
{"text": "请描述这个视频的内容"},
|
|
{
|
|
"inline_data": {
|
|
"mime_type": "video/mp4",
|
|
"data": video_base64
|
|
}
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"generationConfig": {
|
|
"maxOutputTokens": 4096
|
|
}
|
|
}
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {API_KEY}"
|
|
}
|
|
|
|
print(f"\n发送请求到: {API_URL}")
|
|
print("请求中...")
|
|
|
|
try:
|
|
response = requests.post(
|
|
API_URL,
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=180
|
|
)
|
|
|
|
print(f"\n状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print("\n=== 响应内容 ===")
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
|
|
# 提取文本
|
|
if "candidates" in result:
|
|
for candidate in result["candidates"]:
|
|
content = candidate.get("content", {})
|
|
for part in content.get("parts", []):
|
|
if "text" in part:
|
|
print("\n=== AI 回复 ===")
|
|
print(part["text"])
|
|
else:
|
|
print(f"\n错误响应: {response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"\n请求失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
if __name__ == "__main__":
|
|
test_video()
|