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