google_ai_studio.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import os
  2. import time
  3. import uuid
  4. from typing import Optional, Tuple
  5. import cv2
  6. import google.generativeai as genai
  7. import orjson
  8. import requests
  9. from google.generativeai.types import (HarmBlockThreshold, HarmCategory)
  10. from loguru import logger
  11. CACHE_DIR = '/Users/z/Downloads/'
  12. PROXY_ADDR = 'http://localhost:1081'
  13. os.environ['http_proxy'] = PROXY_ADDR
  14. os.environ['https_proxy'] = PROXY_ADDR
  15. class GoogleAI(object):
  16. @classmethod
  17. def get_video_duration(cls, video_link: str) -> int:
  18. cap = cv2.VideoCapture(video_link)
  19. if cap.isOpened():
  20. rate = cap.get(5)
  21. frame_num = cap.get(7)
  22. duration = int(frame_num / rate)
  23. return duration
  24. return 0
  25. @classmethod
  26. def download_video(cls, video_link: str) -> Optional[str]:
  27. file_path = os.path.join(CACHE_DIR, f'{str(uuid.uuid4())}.mp4')
  28. for _ in range(3):
  29. try:
  30. response = requests.get(url=video_link, timeout=60)
  31. if response.status_code == 200:
  32. with open(file_path, 'wb') as f:
  33. f.write(response.content)
  34. logger.info(f'[内容分析] 视频链接: {video_link}, 存储地址: {file_path}')
  35. return file_path
  36. except Exception:
  37. time.sleep(1)
  38. continue
  39. return
  40. @classmethod
  41. def run(cls, api_key, video_url):
  42. try:
  43. genai.configure(api_key=api_key)
  44. video_path = cls.download_video(video_link=video_url)
  45. if not video_path:
  46. logger.error(f'[内容分析] 视频下载失败, 跳过任务')
  47. if os.path.exists(video_path):
  48. os.remove(video_path)
  49. logger.info(f"[内容分析] 文件已删除: {video_path}")
  50. return "视频下载失败"
  51. video = genai.upload_file(path=video_path, mime_type='video/mp4')
  52. while video.state.name == 'PROCESSING':
  53. time.sleep(1)
  54. video = genai.get_file(name=video.name)
  55. if video.state.name != 'ACTIVE':
  56. genai.delete_file(name=video.name)
  57. return
  58. model = genai.GenerativeModel(
  59. model_name='gemini-1.5-flash',
  60. generation_config=genai.GenerationConfig(response_mime_type='application/json'),
  61. safety_settings={
  62. HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
  63. },
  64. )
  65. response = model.generate_content(
  66. contents=[
  67. video,
  68. "你是一名专业的短视频分析师,请你输出这个视频的完整口播,只输出文字即可。使用一下JSON格式输出:{'text': string}",
  69. ],
  70. stream=False,
  71. request_options={
  72. 'timeout': 600,
  73. },
  74. )
  75. text = orjson.loads(response.text.strip())['text']
  76. genai.delete_file(name=video.name)
  77. os.remove(video_path)
  78. return text
  79. except Exception as e:
  80. logger.error(f"[内容分析] 处理异常,异常信息{e}")
  81. return
  82. if __name__ == '__main__':
  83. GoogleAI.run("AIzaSyAwGqthDADh5NPVe3BMcOJBQkJaf0HWBuQ",
  84. "http://rescdn.yishihui.com/jq_oss/video/2025012215472528213")