|
@@ -6,6 +6,7 @@ import requests
|
|
|
import google.generativeai as genai
|
|
|
import uuid
|
|
|
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
|
|
+from loguru import logger
|
|
|
from common import Common
|
|
|
|
|
|
env = os.getenv('env', 'dev')
|
|
@@ -21,11 +22,11 @@ class VideoAnalyzer:
|
|
|
async def process_and_delete_file(self, file_path):
|
|
|
"""删除文件"""
|
|
|
try:
|
|
|
- print(f"正在处理文件: {file_path}" )
|
|
|
+ logger.info(f"正在处理文件: {file_path}" )
|
|
|
os.remove( file_path )
|
|
|
- print( f"文件已删除: {file_path}" )
|
|
|
+ logger.info( f"文件已删除: {file_path}" )
|
|
|
except Exception as e:
|
|
|
- print( f"处理或删除文件时发生错误: {str( e )}" )
|
|
|
+ logger.error( f"处理或删除文件时发生错误: {str( e )}" )
|
|
|
|
|
|
async def download_video(self, video_url, save_directory=DIRECTORY):
|
|
|
# async def download_video(self, video_url, save_directory='/Users/tzld/Desktop/google_ai_studio/path'):
|
|
@@ -45,17 +46,17 @@ class VideoAnalyzer:
|
|
|
with open(save_path, "wb") as file:
|
|
|
# 将响应内容写入文件
|
|
|
file.write(response.content)
|
|
|
- print(f"视频已成功下载并保存到: {save_path}")
|
|
|
+ logger.success(f"视频已成功下载并保存到: {save_path}")
|
|
|
return save_path
|
|
|
else:
|
|
|
- print(f"请求失败,状态码: {response.status_code}")
|
|
|
+ logger.error(f"请求失败,状态码: {response.status_code}")
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
- print(f"下载视频时出现错误: {e}")
|
|
|
+ logger.error(f"下载视频时出现错误: {e}")
|
|
|
|
|
|
# 如果下载失败,等待一段时间后重试
|
|
|
time.sleep(1)
|
|
|
|
|
|
- print("达到最大重试次数,视频下载失败")
|
|
|
+ logger.error("达到最大重试次数,视频下载失败")
|
|
|
return None
|
|
|
|
|
|
async def upload_video(self, save_path):
|
|
@@ -63,13 +64,13 @@ class VideoAnalyzer:
|
|
|
try:
|
|
|
|
|
|
self.video_file = genai.upload_file(save_path)
|
|
|
- print("upload_file 成功")
|
|
|
+ logger.success("upload_file 成功")
|
|
|
"""等待视频文件处理完成"""
|
|
|
while self.video_file.state.name == 'PROCESSING':
|
|
|
- print( '等待视频处理完成...' )
|
|
|
+ logger.info( '等待视频处理完成...' )
|
|
|
await asyncio.sleep( 2 ) # 使用异步睡眠代替阻塞睡眠
|
|
|
self.video_file = genai.get_file( self.video_file.name )
|
|
|
- print( f'视频处理完成: {self.video_file.uri}' )
|
|
|
+ logger.success( f'视频处理完成: {self.video_file.uri}' )
|
|
|
except Exception as e:
|
|
|
|
|
|
Common.logger("ai").info(f'上传视频文件并获取视频文件对象失败异常信息{e}')
|
|
@@ -94,7 +95,7 @@ class VideoAnalyzer:
|
|
|
return model
|
|
|
except Exception as e:
|
|
|
Common.logger("ai").info(f'视频创建缓存内容,并返回生成模型异常信息{e}')
|
|
|
- print(f"视频创建缓存内容,并返回生成模型异常信息{e}")
|
|
|
+ logger.error(f"视频创建缓存内容,并返回生成模型异常信息{e}")
|
|
|
return f"视频创建缓存内容,并返回生成模型异常信息{e}"
|
|
|
|
|
|
async def analyze_video(self, model, questions, sample_data):
|
|
@@ -112,7 +113,7 @@ class VideoAnalyzer:
|
|
|
return response
|
|
|
except Exception as e:
|
|
|
Common.logger("ai").info(f'视频处理请求失败:{e}')
|
|
|
- print( f"视频处理请求失败:{e}" )
|
|
|
+ logger.error( f"视频处理请求失败:{e}" )
|
|
|
return f"视频处理请求失败:{e}"
|
|
|
|
|
|
def video_duration(self, filename):
|
|
@@ -133,19 +134,19 @@ async def main(video_path, api_key, prompt, sample_data):
|
|
|
# 初始化视频分析类
|
|
|
analyzer = VideoAnalyzer(api_key )
|
|
|
for file in genai.list_files():
|
|
|
- print( " ", file.name )
|
|
|
+ logger.info( " ", file.name )
|
|
|
file.delete()
|
|
|
|
|
|
duration = analyzer.video_duration( video_path )
|
|
|
- print( f"视频时长为{duration}秒" )
|
|
|
+ logger.info( f"视频时长为{duration}秒" )
|
|
|
if int( duration ) >= 600 or int( duration ) == 0:
|
|
|
return f"视频时长过长/视频时长为:{duration}秒"
|
|
|
save_path = await analyzer.download_video(video_path)
|
|
|
if save_path == None:
|
|
|
if os.path.exists( save_path ):
|
|
|
os.remove( save_path )
|
|
|
- print( f"文件已删除: {save_path}" )
|
|
|
- print("视频下载失败")
|
|
|
+ logger.info( f"文件已删除: {save_path}" )
|
|
|
+ logger.error("视频下载失败")
|
|
|
return "视频下载失败"
|
|
|
# 上传并处理视频
|
|
|
upload_response = await analyzer.upload_video( save_path )
|
|
@@ -155,7 +156,7 @@ async def main(video_path, api_key, prompt, sample_data):
|
|
|
model =await analyzer.create_cache()
|
|
|
if isinstance( model, str ):
|
|
|
return model
|
|
|
- print("创建缓存模型成功")
|
|
|
+ logger.success("创建缓存模型成功")
|
|
|
# sample_data = {
|
|
|
# "一、基础信息": {
|
|
|
# "视觉/音乐/文字": "",
|
|
@@ -192,18 +193,18 @@ async def main(video_path, api_key, prompt, sample_data):
|
|
|
response =await analyzer.analyze_video( model, prompt, sample_data )
|
|
|
if isinstance( response, str ):
|
|
|
return response
|
|
|
- print( response.usage_metadata )
|
|
|
- print(response.text)
|
|
|
+ logger.info( response.usage_metadata )
|
|
|
+ logger.info(response.text)
|
|
|
if os.path.exists( save_path ):
|
|
|
os.remove( save_path )
|
|
|
- print( f"文件已删除: {save_path}" )
|
|
|
+ logger.info( f"文件已删除: {save_path}" )
|
|
|
return response.text
|
|
|
except Exception as e:
|
|
|
attempt += 1 # 增加尝试次数
|
|
|
if attempt < max_attempts:
|
|
|
- print(f"重试第 {attempt} 次...")
|
|
|
+ logger.warning(f"重试第 {attempt} 次...")
|
|
|
else:
|
|
|
- print( "达到最大重试次数,处理失败" )
|
|
|
+ logger.error( "达到最大重试次数,处理失败" )
|
|
|
Common.logger( "ai" ).info( f'视频分析处理失败异常信息{e}' )
|
|
|
return f"视频分析处理失败:{e}"
|
|
|
|