video_prep.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import configparser
  2. import os
  3. import random
  4. import time
  5. from datetime import datetime
  6. from common import Material, Feishu, Common, Oss
  7. from common.ffmpeg import FFmpeg
  8. from common.piaoquan_utils import PQ
  9. from common.sql_help import sqlCollect
  10. config = configparser.ConfigParser()
  11. config.read('./config.ini')
  12. class getVideo:
  13. """
  14. 数据处理
  15. """
  16. @classmethod
  17. def get_video(cls):
  18. pass
  19. """
  20. 根据标示+任务标示创建目录
  21. """
  22. @classmethod
  23. def create_folders(cls, mark, task_mark):
  24. video_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/" + task_mark + "/"
  25. if not os.path.exists(video_path_url):
  26. os.makedirs(video_path_url)
  27. return video_path_url
  28. """
  29. 随机生成ID
  30. """
  31. @classmethod
  32. def random_id(cls):
  33. now = datetime.now()
  34. rand_num = random.randint(10000, 99999)
  35. oss_id = "{}{}".format(now.strftime("%Y%m%d%H%M%S"), rand_num)
  36. return oss_id
  37. """
  38. 飞书数据处理
  39. """
  40. @classmethod
  41. def video_task(cls, data):
  42. mark = data["mark"]
  43. name = data["name"]
  44. feishu_id = data["feishu_id"]
  45. feishu_sheet = data["feishu_sheet"]
  46. pw_sheet = data["pw_sheet"]
  47. task_data = Material.get_task_data(feishu_id, feishu_sheet)
  48. if len(task_data) == 0:
  49. Feishu.bot(mark, '机器自动改造消息通知', f'今日任务为空,请关注', name)
  50. return mark
  51. for task in task_data[0]:
  52. task_mark = task["task_mark"] # 任务标示
  53. old_id = str(task["old_id"])
  54. video_id = str(task["video_id"])
  55. new_id = str(task["new_id"])
  56. number = task["number"] # 指定条数
  57. title = task["title"]
  58. video_share = task["video_share"]
  59. video_ending = task["video_ending"]
  60. try:
  61. video_path_url = cls.create_folders(mark, str(task_mark)) # 创建目录
  62. if ',' in new_id:
  63. n_id = new_id.split(',')
  64. else:
  65. n_id = [new_id]
  66. if old_id and old_id != 'None':
  67. task_id = old_id.split(',')
  68. else:
  69. task_id = video_id.split(',')
  70. count = 0 # 初始化计数器
  71. for id in task_id:
  72. time.sleep(1)
  73. if old_id and old_id != 'None':
  74. data_list = PQ.get_user_url(task_mark, id, number, title, mark)
  75. else:
  76. data_list = PQ.get_audio_url(task_mark, id, title, mark)
  77. if not data_list:
  78. Common.logger("log").info(f"{task_mark}下的视频ID{id} 已经改造过了")
  79. Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下的视频ID{id},已经改造过,请关注', name)
  80. continue
  81. for video in data_list:
  82. v_id = video["video_id"]
  83. new_title = video["title"]
  84. cover = video["cover"]
  85. video_url = video["video_url"]
  86. new_video_path = PQ.download_video(video_url, video_path_url, v_id) # 下载视频地址
  87. if not new_video_path:
  88. Common.logger("log").info(f"{task_mark}下的视频ID{id},{new_video_path}视频下载失败")
  89. continue
  90. pw_list, zm_list = Material.get_pwsrt_data(feishu_id, pw_sheet) # 获取srt
  91. pws = random.choice(pw_list) # 随机选择 片尾srt+音频
  92. zm = random.choice(zm_list) # 随机选择 视频中字幕
  93. if video_share and video_share != 'None':
  94. new_video_path = FFmpeg.single_video(new_video_path, video_share, video_path_url, zm)
  95. if video_ending and video_ending != 'None':
  96. pw_id = pws["pw_id"]
  97. pw_srt = pws["pw_srt"]
  98. pw_url = PQ.get_audio_url(task_mark, pw_id, title, mark)
  99. jpg_path = FFmpeg.video_png(new_video_path, video_path_url) # 生成视频最后一帧jpg
  100. pw_path = FFmpeg.pw_video(jpg_path, video_path_url, pw_url[0]["video_url"], pw_srt) # 生成片尾视频
  101. video_list = [new_video_path, pw_path]
  102. video_path = FFmpeg.concatenate_videos(video_list, video_path_url) # 视频与片尾拼接到一起
  103. if video_share and video_share != 'None':
  104. new_video_path = FFmpeg.single_video(video_path, video_share, video_path_url, zm)
  105. else:
  106. new_video_path = video_path
  107. else:
  108. if video_share and video_share != 'None':
  109. new_video_path = FFmpeg.single_video(new_video_path, video_share, video_path_url, zm)
  110. oss_id = cls.random_id()
  111. oss_object_key = Oss.stitching_sync_upload_oss(new_video_path, oss_id) # 视频发送OSS
  112. status = oss_object_key.get("status")
  113. if status == 200:
  114. oss_object_key = oss_object_key.get("oss_object_key")
  115. time.sleep(1)
  116. code = PQ.insert_piaoquantv(oss_object_key, new_title, cover, n_id[count])
  117. if code:
  118. Common.logger("log").info(f"{task_mark}下的视频ID{v_id}发送成功")
  119. sqlCollect.insert_task(task_mark, v_id, mark) # 插入数据库
  120. current_time = datetime.now()
  121. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  122. values = [[name, task_mark, v_id, n_id[count], new_title, str(code), formatted_time]]
  123. Feishu.insert_columns("ILb4sa0LahddRktnRipcu2vQnLb", "a74fc4", "ROWS", 1, 2)
  124. time.sleep(0.5)
  125. Feishu.update_values("ILb4sa0LahddRktnRipcu2vQnLb", "a74fc4", "A2:Z2", values)
  126. if os.path.exists(video_path_url) and os.path.isdir(video_path_url):
  127. for root, dirs, files in os.walk(video_path_url):
  128. for file in files:
  129. file_path = os.path.join(root, file)
  130. os.remove(file_path)
  131. for dir in dirs:
  132. dir_path = os.path.join(root, dir)
  133. os.rmdir(dir_path)
  134. else:
  135. Common.logger("log").info(f"{task_mark}下的视频ID{id} 视频发送OSS失败 ")
  136. count += 1 # 每次迭代计数器加1
  137. if old_id and old_id != 'None':
  138. Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务改造完成,请关注', name)
  139. if video_id and video_id != 'None':
  140. Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务改造完成,请关注', name)
  141. except Exception as e:
  142. Common.logger("warning").warning(f"{name}的{task_mark}任务处理失败:{e}\n")
  143. return mark