generate_text_from_video.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import os
  6. import time
  7. import datetime
  8. import traceback
  9. import requests
  10. from pymysql.cursors import DictCursor
  11. from tqdm import tqdm
  12. from applications.api import GoogleAIAPI
  13. from applications.const import VideoToTextConst
  14. from applications.db import DatabaseConnector
  15. from config import long_articles_config
  16. from config import apolloConfig
  17. # 办公室网络调试需要打开代理
  18. # os.environ["HTTP_PROXY"] = "http://192.168.100.20:1087"
  19. # os.environ["HTTPS_PROXY"] = "http://192.168.100.20:1087"
  20. const = VideoToTextConst()
  21. # config = apolloConfig(env="prod")
  22. # pool_size
  23. # POOL_SIZE = int(config.getConfigValue("video_extract_pool_size"))
  24. POOL_SIZE = 20
  25. # batch_size
  26. # BATCH_SIZE = int(config.getConfigValue("video_extract_batch_size"))
  27. BATCH_SIZE = 10
  28. def download_file(task_id, oss_path):
  29. """
  30. 下载视频文件
  31. """
  32. video_url = "https://rescdn.yishihui.com/" + oss_path
  33. file_name = "static/{}.mp4".format(task_id)
  34. if os.path.exists(file_name):
  35. return file_name
  36. proxies = {
  37. "http": None,
  38. "https": None
  39. }
  40. with open(file_name, 'wb') as f:
  41. response = requests.get(video_url, proxies=proxies)
  42. f.write(response.content)
  43. return file_name
  44. class GenerateTextFromVideo(object):
  45. """
  46. 从视频中生成文本
  47. """
  48. def __init__(self):
  49. self.google_ai_api = GoogleAIAPI()
  50. self.db = DatabaseConnector(db_config=long_articles_config)
  51. def connect_db(self):
  52. """
  53. 连接数据库
  54. """
  55. self.db.connect()
  56. def update_task_status(self, task_id, process, ori_status, new_status):
  57. """
  58. 回滚长时间处于处理中的任务
  59. """
  60. match process:
  61. case "upload":
  62. status = 'upload_status'
  63. update_timestamp = 'upload_status_ts'
  64. case "understanding":
  65. status = 'understanding_status'
  66. update_timestamp = 'understanding_status_ts'
  67. case "summary":
  68. status = 'summary_status'
  69. update_timestamp = 'summary_status_ts'
  70. case "rewrite":
  71. status = 'rewrite_status'
  72. update_timestamp = 'rewrite_status_ts'
  73. case _:
  74. raise ValueError(f"Unexpected task: {process}")
  75. update_sql = f"""
  76. update video_content_understanding
  77. set {status} = %s, {update_timestamp} = %s
  78. where {status} = %s and id = %s;
  79. """
  80. roll_back_rows = self.db.save(
  81. query=update_sql,
  82. params=(
  83. new_status,
  84. datetime.datetime.now(),
  85. ori_status,
  86. task_id,
  87. )
  88. )
  89. return roll_back_rows
  90. def upload_video_to_google_ai(self, max_processing_video_count=POOL_SIZE):
  91. """
  92. 上传视频到Google AI
  93. max_processing_video_count: 处理中的最大视频数量,默认20
  94. video_content_understanding 表status字段
  95. 0: 未处理
  96. 1: 处理中
  97. 2: 处理完成
  98. """
  99. select_sql = f"""
  100. select count(1) as processing_count
  101. from video_content_understanding
  102. where understanding_status = {const.PROCESSING_STATUS};
  103. """
  104. count = self.db.fetch(select_sql, cursor_type=DictCursor)[0]['processing_count']
  105. rest_video_count = max_processing_video_count - count
  106. success_upload_count = 0
  107. if rest_video_count:
  108. sql = f"""
  109. select t1.id, t1.video_oss_path
  110. from video_content_understanding t1
  111. join publish_single_video_source t2
  112. on t1.content_trace_id = t2.content_trace_id
  113. where t1.upload_status = {const.INIT_STATUS} and t2.video_pool_audit_status = {const.AUDIT_SUCCESS_STATUS} and t2.bad_status = 0
  114. limit {rest_video_count};
  115. """
  116. task_list = self.db.fetch(sql, cursor_type=DictCursor)
  117. for task in tqdm(task_list, desc="upload_video_task"):
  118. lock_rows = self.update_task_status(
  119. task_id=task['id'],
  120. process='upload',
  121. ori_status=const.INIT_STATUS,
  122. new_status=const.PROCESSING_STATUS
  123. )
  124. if not lock_rows:
  125. continue
  126. try:
  127. file_path = download_file(task['id'], task['video_oss_path'])
  128. google_upload_result = self.google_ai_api.upload_file(file_path)
  129. if google_upload_result:
  130. file_name, file_state, expire_time = google_upload_result
  131. update_sql = f"""
  132. update video_content_understanding
  133. set
  134. upload_status = %s,
  135. upload_status_ts = %s,
  136. file_name = %s,
  137. file_state = %s,
  138. file_expire_time = %s
  139. where id = %s and upload_status = %s;
  140. """
  141. self.db.save(
  142. update_sql,
  143. params=(
  144. const.SUCCESS_STATUS,
  145. datetime.datetime.now(),
  146. file_name,
  147. file_state,
  148. expire_time,
  149. task['id'],
  150. const.PROCESSING_STATUS
  151. )
  152. )
  153. success_upload_count += 1
  154. except Exception as e:
  155. print("task upload failed because of {}".format(e))
  156. print("trace_back: ", traceback.format_exc())
  157. # roll back status
  158. self.update_task_status(
  159. task_id=task['id'],
  160. process='upload',
  161. ori_status=const.PROCESSING_STATUS,
  162. new_status=const.FAIL_STATUS
  163. )
  164. return success_upload_count
  165. def delete_video_from_google(self, file_name):
  166. """
  167. 删除视频文件
  168. """
  169. self.google_ai_api.delete_video(file_name)
  170. def get_task_list(self):
  171. """
  172. 获取处理视频转文本任务
  173. """
  174. sql = f"""
  175. select id, file_name, video_ori_title
  176. from video_content_understanding
  177. where upload_status = {const.SUCCESS_STATUS} and understanding_status = {const.INIT_STATUS}
  178. order by file_expire_time
  179. limit {BATCH_SIZE};
  180. """
  181. task_list = self.db.fetch(sql, cursor_type=DictCursor)
  182. return task_list
  183. def convert_video_to_text_with_google_ai(self):
  184. """
  185. 处理视频转文本任务
  186. """
  187. task_list = self.get_task_list()
  188. while task_list:
  189. for task in tqdm(task_list, desc="convert video to text"):
  190. # LOCK TASK
  191. lock_row = self.update_task_status(
  192. task_id=task['id'],
  193. process='understanding',
  194. ori_status=const.INIT_STATUS,
  195. new_status=const.PROCESSING_STATUS
  196. )
  197. if not lock_row:
  198. print("Lock")
  199. continue
  200. file_name = task['file_name']
  201. video_local_path = "static/{}.mp4".format(task['id'])
  202. google_file = self.google_ai_api.get_google_file(file_name)
  203. state = google_file.state.name
  204. match state:
  205. case 'ACTIVE':
  206. try:
  207. video_text = self.google_ai_api.get_video_text(
  208. prompt="分析我上传的视频的画面和音频,用叙述故事的风格将视频所描述的事件进行总结,需要保证视频内容的完整性,并且用中文进行输出,直接返回生成的文本",
  209. video_file=google_file
  210. )
  211. if video_text:
  212. print(type(video_text))
  213. print(video_text)
  214. update_sql = f"""
  215. update video_content_understanding
  216. set understanding_status = %s, video_text = %s, file_state = %s
  217. where id = %s and understanding_status = %s;
  218. """
  219. self.db.save(
  220. update_sql,
  221. params=(
  222. const.SUCCESS_STATUS,
  223. video_text,
  224. state,
  225. task['id'],
  226. const.PROCESSING_STATUS
  227. )
  228. )
  229. # # delete local file and google file
  230. # if os.path.exists(video_local_path):
  231. # os.remove(video_local_path)
  232. #
  233. # tqdm.write("video transform to text success, delete local file")
  234. task_list.remove(task)
  235. #
  236. # self.google_ai_api.delete_video(file_name)
  237. tqdm.write("delete video from google success: {}".format(file_name))
  238. else:
  239. # roll back status
  240. self.update_task_status(
  241. task_id=task['id'],
  242. process='understanding',
  243. ori_status=const.PROCESSING_STATUS,
  244. new_status=const.INIT_STATUS
  245. )
  246. except Exception as e:
  247. # roll back status
  248. self.update_task_status(
  249. task_id=task['id'],
  250. process='understanding',
  251. ori_status=const.PROCESSING_STATUS,
  252. new_status=const.FAIL_STATUS
  253. )
  254. tqdm.write(str(e))
  255. continue
  256. case 'PROCESSING':
  257. tqdm.write("video is still processing")
  258. case 'FAILED':
  259. self.update_task_status(
  260. task_id=task['id'],
  261. process='understanding',
  262. ori_status=const.PROCESSING_STATUS,
  263. new_status=const.FAIL_STATUS
  264. )
  265. if os.path.exists(video_local_path):
  266. os.remove(video_local_path)
  267. self.google_ai_api.delete_video(file_name)
  268. task_list.remove(task)
  269. tqdm.write("video process failed, delete local file")
  270. time.sleep(const.SLEEP_SECONDS)
  271. tqdm.write("执行完一轮任务,剩余数量:{}".format(len(task_list)))
  272. time.sleep(const.SLEEP_SECONDS)