|
@@ -1,5 +1,6 @@
|
|
"""
|
|
"""
|
|
@author: luojunhui
|
|
@author: luojunhui
|
|
|
|
+todo: 加上多进程锁
|
|
"""
|
|
"""
|
|
import os
|
|
import os
|
|
import time
|
|
import time
|
|
@@ -13,11 +14,10 @@ from applications.api import GoogleAIAPI
|
|
from applications.db import DatabaseConnector
|
|
from applications.db import DatabaseConnector
|
|
from config import long_articles_config
|
|
from config import long_articles_config
|
|
|
|
|
|
|
|
+# 办公室网络调试需要打开代理
|
|
# os.environ["HTTP_PROXY"] = "http://192.168.100.20:1087"
|
|
# os.environ["HTTP_PROXY"] = "http://192.168.100.20:1087"
|
|
# os.environ["HTTPS_PROXY"] = "http://192.168.100.20:1087"
|
|
# os.environ["HTTPS_PROXY"] = "http://192.168.100.20:1087"
|
|
|
|
|
|
-PROCESSING_MAX_VIDEO_COUNT = 10
|
|
|
|
-
|
|
|
|
|
|
|
|
def download_file(pq_vid, video_url):
|
|
def download_file(pq_vid, video_url):
|
|
"""
|
|
"""
|
|
@@ -73,14 +73,19 @@ class GenerateTextFromVideo(object):
|
|
)
|
|
)
|
|
print(affected_rows)
|
|
print(affected_rows)
|
|
|
|
|
|
- def upload_video_to_google_ai(self):
|
|
|
|
|
|
+ def upload_video_to_google_ai(self, max_processing_video_count=100):
|
|
"""
|
|
"""
|
|
上传视频到Google AI
|
|
上传视频到Google AI
|
|
|
|
+ max_processing_video_count: 处理中的最大视频数量,默认1000
|
|
|
|
+ video_content_understanding 表status字段
|
|
|
|
+ 0: 未处理
|
|
|
|
+ 1: 处理中
|
|
|
|
+ 2: 处理完成
|
|
"""
|
|
"""
|
|
# 查询出在视频处于PROCESSING状态的视频数量
|
|
# 查询出在视频处于PROCESSING状态的视频数量
|
|
select_sql = "select count(1) as processing_count from video_content_understanding where status = 1;"
|
|
select_sql = "select count(1) as processing_count from video_content_understanding where status = 1;"
|
|
count = self.db.fetch(select_sql, cursor_type=DictCursor)[0]['processing_count']
|
|
count = self.db.fetch(select_sql, cursor_type=DictCursor)[0]['processing_count']
|
|
- rest_video_count = PROCESSING_MAX_VIDEO_COUNT - count
|
|
|
|
|
|
+ rest_video_count = max_processing_video_count - count
|
|
success_upload_count = 0
|
|
success_upload_count = 0
|
|
if rest_video_count:
|
|
if rest_video_count:
|
|
sql = f"""select pq_vid, video_oss_path from video_content_understanding where status = 0 limit {rest_video_count};"""
|
|
sql = f"""select pq_vid, video_oss_path from video_content_understanding where status = 0 limit {rest_video_count};"""
|
|
@@ -122,6 +127,7 @@ class GenerateTextFromVideo(object):
|
|
while task_list:
|
|
while task_list:
|
|
for task in tqdm(task_list, desc="convert video to text"):
|
|
for task in tqdm(task_list, desc="convert video to text"):
|
|
file_name = task['file_name']
|
|
file_name = task['file_name']
|
|
|
|
+ video_local_path = "static/{}.mp4".format(task['pq_vid'])
|
|
google_file = self.google_ai_api.get_google_file(file_name)
|
|
google_file = self.google_ai_api.get_google_file(file_name)
|
|
state = google_file.state.name
|
|
state = google_file.state.name
|
|
match state:
|
|
match state:
|
|
@@ -141,7 +147,7 @@ class GenerateTextFromVideo(object):
|
|
update_sql,
|
|
update_sql,
|
|
params=(2, video_text, state, task['pq_vid'])
|
|
params=(2, video_text, state, task['pq_vid'])
|
|
)
|
|
)
|
|
- os.remove("static/{}.mp4".format(task['pq_vid']))
|
|
|
|
|
|
+ os.remove(video_local_path)
|
|
tqdm.write("video transform to text success, delete local file, sleep 1 min...")
|
|
tqdm.write("video transform to text success, delete local file, sleep 1 min...")
|
|
task_list.remove(task)
|
|
task_list.remove(task)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
@@ -153,7 +159,17 @@ class GenerateTextFromVideo(object):
|
|
continue
|
|
continue
|
|
|
|
|
|
case 'FAILED':
|
|
case 'FAILED':
|
|
- tqdm.write("video process failed")
|
|
|
|
|
|
+ update_sql = f"""
|
|
|
|
+ update video_content_understanding
|
|
|
|
+ set status = %s, file_state = %s
|
|
|
|
+ where pq_vid = %s;
|
|
|
|
+ """
|
|
|
|
+ self.db.save(
|
|
|
|
+ update_sql,
|
|
|
|
+ params=(99, state, task['pq_vid'])
|
|
|
|
+ )
|
|
|
|
+ os.remove(video_local_path)
|
|
|
|
+ tqdm.write("video process failed, delete local file")
|
|
continue
|
|
continue
|
|
time.sleep(10)
|
|
time.sleep(10)
|
|
|
|
|