| 
					
				 | 
			
			
				@@ -12,7 +12,7 @@ from common import Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     def __init__(self, api_key): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """初始化类,配置 API 密钥和视频路径""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        genai.configure(api_key=api_key) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        genai.configure( api_key=api_key ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         self.video_file = None 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     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'): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """从给定的视频链接下载视频并保存到指定路径""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        random_filename = f"{uuid.uuid4()}.mp4" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         max_retries = 3  # 最大重试次数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         for attempt in range(max_retries): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                random_filename = f"{uuid.uuid4()}.mp4" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 save_path = os.path.join( save_directory, random_filename ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 # 发送 GET 请求获取视频内容 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 headers = {} 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -58,20 +58,20 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     async def upload_video(self, save_path): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """上传视频文件并获取视频文件对象""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             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: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             Common.logger("ai").info(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): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             generation_config = { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -106,7 +106,6 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             response = chat_session.send_message( message_content ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.video_file.delete() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             Common.logger("ai").info(f'视频处理请求失败:{e}') 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -130,15 +129,16 @@ async def main(video_path, api_key, prompt, sample_data): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             # 初始化视频分析类 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             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 ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             print( f"视频时长为{duration}秒" ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             if int( duration ) >= 600 or int( duration ) == 0: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 return f"视频时长过长/视频时长为:{duration}秒" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             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 ): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     os.remove( save_path ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     print( f"文件已删除: {save_path}" ) 
			 |