| 
														
															@@ -12,7 +12,7 @@ from common import Common 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 class VideoAnalyzer: 
														 | 
														
														 | 
														
															 class VideoAnalyzer: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															     def __init__(self, api_key): 
														 | 
														
														 | 
														
															     def __init__(self, api_key): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         """初始化类,配置 API 密钥和视频路径""" 
														 | 
														
														 | 
														
															         """初始化类,配置 API 密钥和视频路径""" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        genai.configure(api_key=api_key) 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        genai.configure( api_key=api_key ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         self.video_file = None 
														 | 
														
														 | 
														
															         self.video_file = None 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															     async def process_and_delete_file(self, file_path): 
														 | 
														
														 | 
														
															     async def process_and_delete_file(self, file_path): 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -28,10 +28,10 @@ class VideoAnalyzer: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															     # async def download_video(self, video_url, save_directory='/Users/tzld/Desktop/google_ai_studio/path'): 
														 | 
														
														 | 
														
															     # async def download_video(self, video_url, save_directory='/Users/tzld/Desktop/google_ai_studio/path'): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															         """从给定的视频链接下载视频并保存到指定路径""" 
														 | 
														
														 | 
														
															         """从给定的视频链接下载视频并保存到指定路径""" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+        random_filename = f"{uuid.uuid4()}.mp4" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         max_retries = 3  # 最大重试次数 
														 | 
														
														 | 
														
															         max_retries = 3  # 最大重试次数 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         for attempt in range(max_retries): 
														 | 
														
														 | 
														
															         for attempt in range(max_retries): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             try: 
														 | 
														
														 | 
														
															             try: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-                random_filename = f"{uuid.uuid4()}.mp4" 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 save_path = os.path.join( save_directory, random_filename ) 
														 | 
														
														 | 
														
															                 save_path = os.path.join( save_directory, random_filename ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 # 发送 GET 请求获取视频内容 
														 | 
														
														 | 
														
															                 # 发送 GET 请求获取视频内容 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 headers = {} 
														 | 
														
														 | 
														
															                 headers = {} 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -58,20 +58,20 @@ class VideoAnalyzer: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															     async def upload_video(self, save_path): 
														 | 
														
														 | 
														
															     async def upload_video(self, save_path): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         """上传视频文件并获取视频文件对象""" 
														 | 
														
														 | 
														
															         """上传视频文件并获取视频文件对象""" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         try: 
														 | 
														
														 | 
														
															         try: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+ 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             self.video_file = genai.upload_file(save_path) 
														 | 
														
														 | 
														
															             self.video_file = genai.upload_file(save_path) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            await self._wait_for_processing() 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            print("upload_file 成功") 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            """等待视频文件处理完成""" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            while self.video_file.state.name == 'PROCESSING': 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                print( '等待视频处理完成...' ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                await asyncio.sleep( 2 )  # 使用异步睡眠代替阻塞睡眠 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                self.video_file = genai.get_file( self.video_file.name ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            print( f'视频处理完成: {self.video_file.uri}' ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         except Exception as e: 
														 | 
														
														 | 
														
															         except Exception as e: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+ 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             Common.logger("ai").info(f'上传视频文件并获取视频文件对象失败异常信息{e}') 
														 | 
														
														 | 
														
															             Common.logger("ai").info(f'上传视频文件并获取视频文件对象失败异常信息{e}') 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             return f"上传视频文件并获取视频文件对象失败:{e}" 
														 | 
														
														 | 
														
															             return f"上传视频文件并获取视频文件对象失败:{e}" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															  
														 | 
														
														 | 
														
															  
														 | 
													
												
											
												
													
														| 
														 | 
														
															-    async def _wait_for_processing(self): 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        """等待视频文件处理完成""" 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        while self.video_file.state.name == 'PROCESSING': 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            print( '等待视频处理完成...' ) 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            await asyncio.sleep(2)  # 使用异步睡眠代替阻塞睡眠 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            self.video_file = genai.get_file( self.video_file.name ) 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-        print( f'视频处理完成: {self.video_file.uri}' ) 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															- 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															     async def create_cache(self): 
														 | 
														
														 | 
														
															     async def create_cache(self): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         try: 
														 | 
														
														 | 
														
															         try: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             generation_config = { 
														 | 
														
														 | 
														
															             generation_config = { 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -106,7 +106,6 @@ class VideoAnalyzer: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 ] 
														 | 
														
														 | 
														
															                 ] 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             } 
														 | 
														
														 | 
														
															             } 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             response = chat_session.send_message( message_content ) 
														 | 
														
														 | 
														
															             response = chat_session.send_message( message_content ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            self.video_file.delete() 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             return response 
														 | 
														
														 | 
														
															             return response 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         except Exception as e: 
														 | 
														
														 | 
														
															         except Exception as e: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             Common.logger("ai").info(f'视频处理请求失败:{e}') 
														 | 
														
														 | 
														
															             Common.logger("ai").info(f'视频处理请求失败:{e}') 
														 | 
													
												
											
										
											
												
													
														 | 
														
															@@ -130,15 +129,16 @@ async def main(video_path, api_key, prompt, sample_data): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															         try: 
														 | 
														
														 | 
														
															         try: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             # 初始化视频分析类 
														 | 
														
														 | 
														
															             # 初始化视频分析类 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             analyzer = VideoAnalyzer(api_key ) 
														 | 
														
														 | 
														
															             analyzer = VideoAnalyzer(api_key ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            # for file in genai.list_files(): 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            #     file.delete() 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            #     print( "  ", file.name ) 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            for file in genai.list_files(): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                print( "  ", file.name ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+                file.delete() 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+ 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             duration = analyzer.video_duration( video_path ) 
														 | 
														
														 | 
														
															             duration = analyzer.video_duration( video_path ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             print( f"视频时长为{duration}秒" ) 
														 | 
														
														 | 
														
															             print( f"视频时长为{duration}秒" ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             if int( duration ) >= 600 or int( duration ) == 0: 
														 | 
														
														 | 
														
															             if int( duration ) >= 600 or int( duration ) == 0: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 return f"视频时长过长/视频时长为:{duration}秒" 
														 | 
														
														 | 
														
															                 return f"视频时长过长/视频时长为:{duration}秒" 
														 | 
													
												
											
												
													
														| 
														 | 
														
															             save_path = await analyzer.download_video(video_path) 
														 | 
														
														 | 
														
															             save_path = await analyzer.download_video(video_path) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															-            if not save_path or save_path == None: 
														 | 
														
														 | 
														
															 
														 | 
													
												
											
												
													
														| 
														 | 
														
															 
														 | 
														
														 | 
														
															+            if save_path == None: 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                 if os.path.exists( save_path ): 
														 | 
														
														 | 
														
															                 if os.path.exists( save_path ): 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                     os.remove( save_path ) 
														 | 
														
														 | 
														
															                     os.remove( save_path ) 
														 | 
													
												
											
												
													
														| 
														 | 
														
															                     print( f"文件已删除: {save_path}" ) 
														 | 
														
														 | 
														
															                     print( f"文件已删除: {save_path}" ) 
														 |