zhangyong há 6 meses atrás
pai
commit
6bd3d88a00
2 ficheiros alterados com 24 adições e 16 exclusões
  1. 23 15
      google_ai/generativeai_video.py
  2. 1 1
      job_video_processing.py

+ 23 - 15
google_ai/generativeai_video.py

@@ -34,26 +34,34 @@ class VideoAnalyzer:
     # async def download_video(self, video_url, save_directory='/Users/tzld/Desktop/google_ai_studio/path'):
 
         """从给定的视频链接下载视频并保存到指定路径"""
-        try:
-            # 发送 GET 请求获取视频内容
-            random_filename = f"{uuid.uuid4()}.mp4"
-            save_path = os.path.join(save_directory, random_filename)
-            headers = {}
-            payload = {}
-            for i in range( 3 ):
+        random_filename = f"{uuid.uuid4()}.mp4"
+        save_path = os.path.join(save_directory, random_filename)
+
+        max_retries = 3  # 最大重试次数
+        for attempt in range(max_retries):
+            try:
+                # 发送 GET 请求获取视频内容
+                headers = {}
+                payload = {}
                 response = requests.request("GET", video_url, headers=headers, data=payload)
                 if response.status_code == 200:
                     # 以二进制写入模式打开文件
-                    with open(f"{save_path}", "wb") as file:
+                    with open(save_path, "wb") as file:
                         # 将响应内容写入文件
                         file.write(response.content)
-                        time.sleep(1)
-                print( f"视频已成功下载并保存到: {save_path}" )
-                return save_path
-            return None
-        except requests.exceptions.RequestException as e:
-            print( f"下载视频时出现错误: {e}" )
-            return None
+                    print(f"视频已成功下载并保存到: {save_path}")
+                    return save_path
+                else:
+                    print(f"请求失败,状态码: {response.status_code}")
+
+            except requests.exceptions.RequestException as e:
+                print(f"下载视频时出现错误: {e}")
+
+            # 如果下载失败,等待一段时间后重试
+            time.sleep(1)
+
+        print("达到最大重试次数,视频下载失败")
+        return None
 
     async def upload_video(self, save_path, mime_type = None):
         """上传视频文件并获取视频文件对象"""

+ 1 - 1
job_video_processing.py

@@ -8,7 +8,7 @@ max_workers = 10
 
 def video_ai_task_start():
     with ThreadPoolExecutor( max_workers=max_workers) as executor:
-        redis_task_list = ['task:video_ai_top', 'task:video_ai_recommend'] * 5
+        redis_task_list = ['task:video_ai_top', 'task:video_ai_recommend']
         # 任务索引
         task_index = 0
         while True: