agc_video.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import configparser
  2. import os
  3. import random
  4. import subprocess
  5. import sys
  6. import time
  7. import shutil
  8. from datetime import datetime
  9. from common.sql_help import sqlHelp
  10. sys.path.append(os.getcwd())
  11. from common.material import Material
  12. from common import Common, Oss, Feishu, PQ
  13. from common.srt import SRT
  14. config = configparser.ConfigParser()
  15. config.read('./config.ini') # 替换为您的配置文件路径
  16. class AGC():
  17. """清除文件下所有mp4文件"""
  18. @classmethod
  19. def clear_mp4_files(cls, mark):
  20. video_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/"
  21. # 获取文件夹中所有扩展名为 '.mp4' 的文件路径列表
  22. if os.path.exists(video_path_url):
  23. # 列出目录中的所有文件和文件夹
  24. for filename in os.listdir(video_path_url):
  25. file_path = os.path.join(video_path_url, filename)
  26. try:
  27. # 如果是文件,则删除
  28. if os.path.isfile(file_path) or os.path.islink(file_path):
  29. os.unlink(file_path)
  30. # 如果是文件夹,则删除文件夹及其内容
  31. elif os.path.isdir(file_path):
  32. shutil.rmtree(file_path)
  33. except Exception as e:
  34. print(f"Failed to delete {file_path}. Reason: {e}")
  35. print(f"{mark}的文件已清空。")
  36. """
  37. 站外视频拼接
  38. """
  39. @classmethod
  40. def zw_concatenate_videos(cls, videos, audio_duration, audio_video, platform, s_path, v_path, mark, v_oss_path):
  41. Common.logger("video").info(f"{mark}的{platform}渠道获取待拼接视频")
  42. video_files = cls.concat_videos_with_subtitles(videos, audio_duration, platform, mark)
  43. Common.logger("video").info(f"{mark}的{platform}视频文件:{video_files}")
  44. if video_files == "":
  45. return ""
  46. print(f"{mark}的{platform}:开始拼接视频喽~~~")
  47. Common.logger("video").info(f"{mark}的{platform}:开始拼接视频喽~~~")
  48. if os.path.exists(s_path):
  49. # subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=11,Fontname=Hiragino Sans GB,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  50. subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=12,Fontname=wqy-zenhei,Bold=1,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  51. else:
  52. start_time = cls.seconds_to_srt_time(0)
  53. end_time = cls.seconds_to_srt_time(audio_duration)
  54. with open(s_path, 'w') as f:
  55. f.write(f"1\n{start_time} --> {end_time}\n分享、转发给群友\n")
  56. # subtitle_cmd = "drawtext=text='分享、转发给群友':fontsize=28:fontcolor=black:x=(w-text_w)/2:y=h-text_h-15"
  57. subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=12,Fontname=wqy-zenhei,Bold=1,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  58. # 背景色参数
  59. background_cmd = "drawbox=y=ih-65:color=yellow@1.0:width=iw:height=0:t=fill"
  60. VIDEO_COUNTER = 0
  61. FF_INPUT = ""
  62. FF_SCALE = ""
  63. FF_FILTER = ""
  64. ffmpeg_cmd = ["ffmpeg"]
  65. for videos in video_files:
  66. Common.logger("video").info(f"{mark}的{platform}视频:{videos[3]}")
  67. # 添加输入文件
  68. FF_INPUT += f" -i {videos[3]}"
  69. # 为每个视频文件统一长宽,并设置SAR(采样宽高比)
  70. FF_SCALE += f"[{VIDEO_COUNTER}:v]scale=320x480,setsar=1[v{VIDEO_COUNTER}];"
  71. # 为每个视频文件创建一个输入流,并添加到-filter_complex参数中
  72. FF_FILTER += f"[v{VIDEO_COUNTER}][{VIDEO_COUNTER}:a]"
  73. # 增加视频计数器
  74. VIDEO_COUNTER += 1
  75. # 构建最终的FFmpeg命令
  76. ffmpeg_cmd.extend(FF_INPUT.split())
  77. ffmpeg_cmd.extend(["-filter_complex", f"{FF_SCALE}{FF_FILTER}concat=n={VIDEO_COUNTER}:v=1:a=1[v][a]",
  78. "-map", "[v]", "-map", "[a]", v_path])
  79. # 多线程数
  80. num_threads = 4
  81. # 构建 FFmpeg 命令,生成视频
  82. ffmpeg_cmd_oss = [
  83. "ffmpeg",
  84. "-i", v_path, # 视频文件列表
  85. "-i", audio_video, # 音频文件
  86. "-c:v", "libx264", # 复制视频流
  87. "-c:a", "aac", # 编码音频流为AAC
  88. "-threads", str(num_threads),
  89. "-vf", f"{background_cmd},{subtitle_cmd}", # 添加背景色和字幕
  90. "-t", str(int(audio_duration)), # 保持与音频时长一致
  91. "-map", "0:v:0", # 映射第一个输入的视频流
  92. "-map", "1:a:0", # 映射第二个输入的音频流
  93. "-y", # 覆盖输出文件
  94. v_oss_path
  95. ]
  96. try:
  97. timeout_seconds = 25 * 60
  98. subprocess.run(ffmpeg_cmd, timeout=timeout_seconds)
  99. if os.path.isfile(v_path):
  100. subprocess.run(ffmpeg_cmd_oss, timeout=timeout_seconds)
  101. print("视频处理完成!")
  102. except subprocess.TimeoutExpired:
  103. print("视频处理超时,处理失败!")
  104. return ""
  105. except subprocess.CalledProcessError as e:
  106. print(f"视频处理失败:{e}")
  107. return ""
  108. print(f"{mark}的{platform}:视频拼接成功啦~~~")
  109. Common.logger("video").info(f"{mark}的{platform}:视频拼接成功啦~~~")
  110. return video_files
  111. """视频秒数转换"""
  112. @classmethod
  113. def seconds_to_srt_time(cls, seconds):
  114. hours = int(seconds // 3600)
  115. minutes = int((seconds % 3600) // 60)
  116. seconds = seconds % 60
  117. milliseconds = int((seconds - int(seconds)) * 1000)
  118. return f"{hours:02d}:{minutes:02d}:{int(seconds):02d},{milliseconds:03d}"
  119. """
  120. 获取视频文件的时长(秒)
  121. """
  122. @classmethod
  123. def get_video_duration(cls, video_file):
  124. result = subprocess.run(
  125. ["ffprobe", "-v", "error", "-show_entries", "format=duration",
  126. "-of", "default=noprint_wrappers=1:nokey=1", video_file],
  127. capture_output=True, text=True)
  128. try:
  129. return float(result.stdout.strip())
  130. except ValueError:
  131. print(f"{video_file}视频无法从输出中解析时长:'{result.stdout.strip()}'")
  132. return 0
  133. """计算需要拼接的视频"""
  134. @classmethod
  135. def concat_videos_with_subtitles(cls, videos, audio_duration, platform, mark):
  136. # 计算视频文件列表总时长
  137. if platform == "爆款" or platform == "跟随":
  138. total_video_duration = sum(cls.get_video_duration(video_file) for video_file in videos)
  139. else:
  140. total_video_duration = sum(cls.get_video_duration(video_file[3]) for video_file in videos)
  141. Common.logger("video").info(f"{mark}的{platform}渠道总时长{total_video_duration}")
  142. if platform == "爆款" or platform == "跟随":
  143. # 视频时长大于音频时长
  144. if total_video_duration > audio_duration:
  145. return videos
  146. # 计算音频秒数与视频秒数的比率,然后加一得到需要的视频数量
  147. video_audio_ratio = audio_duration / total_video_duration
  148. videos_needed = int(video_audio_ratio) + 2
  149. trimmed_video_list = videos * videos_needed
  150. return trimmed_video_list
  151. else:
  152. # 如果视频总时长小于音频时长,则不做拼接
  153. if total_video_duration < audio_duration:
  154. Common.logger("video").info(f"{mark}的{platform}渠道时长小于等于目标时长,不做视频拼接")
  155. return ""
  156. # 如果视频总时长大于音频时长,则截断视频
  157. trimmed_video_list = []
  158. remaining_duration = audio_duration
  159. for video_file in videos:
  160. video_duration = cls.get_video_duration(video_file[3])
  161. if video_duration <= remaining_duration:
  162. # 如果视频时长小于或等于剩余时长,则将整个视频添加到列表中
  163. trimmed_video_list.append(video_file)
  164. remaining_duration -= video_duration
  165. else:
  166. trimmed_video_list.append(video_file)
  167. break
  168. return trimmed_video_list
  169. """
  170. text文件没有则创建目录
  171. """
  172. @classmethod
  173. def bk_text_folders(cls, mark):
  174. oss_id = cls.random_id()
  175. v_text_url = config['PATHS']['VIDEO_PATH'] + mark + "/text/"
  176. if not os.path.exists(v_text_url):
  177. os.makedirs(v_text_url)
  178. # srt 文件地址
  179. text_path = v_text_url + mark + f"{str(oss_id)}.text"
  180. return text_path
  181. """
  182. 站内视频拼接
  183. """
  184. @classmethod
  185. def zn_concatenate_videos(cls, videos, audio_duration, audio_video, platform, s_path, mark, v_oss_path):
  186. text_ptah = cls.bk_text_folders(mark)
  187. video_files = cls.concat_videos_with_subtitles(videos, audio_duration, platform, mark)
  188. if video_files == "":
  189. return ""
  190. with open(text_ptah, 'w') as f:
  191. for file in video_files:
  192. f.write(f"file '{file}'\n")
  193. Common.logger("video").info(f"{mark}的{platform}视频文件:{video_files}")
  194. print(f"{mark}的{platform}:开始拼接视频喽~~~")
  195. Common.logger("video").info(f"{mark}的{platform}:开始拼接视频喽~~~")
  196. if os.path.exists(s_path):
  197. # subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=11,Fontname=Hiragino Sans GB,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  198. subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=12,Fontname=wqy-zenhei,Bold=1,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  199. else:
  200. start_time = cls.seconds_to_srt_time(0)
  201. end_time = cls.seconds_to_srt_time(audio_duration)
  202. with open(s_path, 'w') as f:
  203. f.write(f"1\n{start_time} --> {end_time}\n分享、转发给群友\n")
  204. # subtitle_cmd = "drawtext=text='分享、转发给群友':fontsize=28:fontcolor=black:x=(w-text_w)/2:y=h-text_h-15"
  205. subtitle_cmd = f"subtitles={s_path}:force_style='Fontsize=12,Fontname=wqy-zenhei,Bold=1,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000'"
  206. # 背景色参数
  207. background_cmd = "drawbox=y=ih-65:color=yellow@1.0:width=iw:height=0:t=fill"
  208. # 多线程数
  209. num_threads = 4
  210. # 构建 FFmpeg 命令,生成视频
  211. ffmpeg_cmd_oss = [
  212. "ffmpeg",
  213. "-f", "concat",
  214. "-safe", "0",
  215. "-i", f"{text_ptah}", # 视频文件列表
  216. "-i", audio_video, # 音频文件
  217. "-c:v", "libx264",
  218. "-c:a", "aac",
  219. "-threads", str(num_threads),
  220. "-vf", f"scale=320x480,{background_cmd},{subtitle_cmd}", # 添加背景色和字幕
  221. "-t", str(int(audio_duration)), # 保持与音频时长一致
  222. "-map", "0:v:0", # 映射第一个输入的视频流
  223. "-map", "1:a:0", # 映射第二个输入的音频流
  224. "-y", # 覆盖输出文件
  225. v_oss_path
  226. ]
  227. try:
  228. subprocess.run(ffmpeg_cmd_oss)
  229. print("视频处理完成!")
  230. if os.path.isfile(text_ptah):
  231. os.remove(text_ptah)
  232. except subprocess.CalledProcessError as e:
  233. print(f"视频处理失败:{e}")
  234. print(f"{mark}:视频拼接成功啦~~~")
  235. Common.logger("video").info(f"{mark}:视频拼接成功啦~~~")
  236. return v_oss_path
  237. """
  238. 获取视频时长
  239. """
  240. @classmethod
  241. def get_audio_duration(cls, video_url):
  242. ffprobe_cmd = [
  243. "ffprobe",
  244. "-i", video_url,
  245. "-show_entries", "format=duration",
  246. "-v", "quiet",
  247. "-of", "csv=p=0"
  248. ]
  249. output = subprocess.check_output(ffprobe_cmd).decode("utf-8").strip()
  250. return float(output)
  251. """
  252. 创建临时字幕
  253. """
  254. @classmethod
  255. def create_subtitle_file(cls, srt, s_path):
  256. with open(s_path, 'w') as f:
  257. f.write(srt)
  258. """
  259. 随机生成id
  260. """
  261. @classmethod
  262. def random_id(cls):
  263. now = datetime.now()
  264. rand_num = random.randint(10000, 99999)
  265. oss_id = "{}{}".format(now.strftime("%Y%m%d%H%M%S"), rand_num)
  266. return oss_id
  267. """
  268. 文件没有则创建目录
  269. """
  270. @classmethod
  271. def create_folders(cls, mark):
  272. oss_id = cls.random_id()
  273. video_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/"
  274. # srt 目录
  275. s_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/srt/"
  276. # oss 目录
  277. v_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/oss/"
  278. if not os.path.exists(video_path_url):
  279. os.makedirs(video_path_url)
  280. if not os.path.exists(s_path_url):
  281. os.makedirs(s_path_url)
  282. if not os.path.exists(v_path_url):
  283. os.makedirs(v_path_url)
  284. # srt 文件地址
  285. s_path = s_path_url + mark + f"{str(oss_id)}.srt"
  286. # 最终生成视频地址
  287. v_path = v_path_url + mark + f"{str(oss_id)}.mp4"
  288. v_oss_path = v_path_url + mark + f"{str(oss_id)}oss.mp4"
  289. return s_path, v_path, video_path_url, v_oss_path
  290. """
  291. 获取未使用的数据
  292. """
  293. @classmethod
  294. def get_unique_uid_data(cls, data, count):
  295. unique_data_dict = {item['uid']: item for item in data}
  296. unique_data = list(unique_data_dict.values())
  297. if count >= len(unique_data):
  298. return unique_data
  299. else:
  300. selected_items = []
  301. selected_uids = set()
  302. while len(selected_items) < count:
  303. # 随机选择一个元素
  304. item = random.choice(unique_data)
  305. uid = item['uid']
  306. if uid not in selected_uids:
  307. # 如果该uid还未被选择过,则将该元素添加到选中项列表中,并记录已选择的uid
  308. selected_items.append(item)
  309. selected_uids.add(uid)
  310. return selected_items
  311. """
  312. 任务处理
  313. """
  314. @classmethod
  315. def video(cls, data, platform):
  316. mark_name = data['mark_name'] # 负责人
  317. mark = data["mark"] # 标示
  318. if "-" in mark:
  319. name = mark.split("-")[0]
  320. else:
  321. name = mark
  322. if platform == "爆款":
  323. pq_ids = data["pq_id"]
  324. pq_ids_list = pq_ids.split(',') # 账号ID
  325. feishu_id = data["feishu_id"] # 飞书文档ID
  326. video_call = data["video_call"] # 脚本sheet
  327. list_data = Material.get_allbk_data(feishu_id, video_call)
  328. if len(list_data) == 0:
  329. Feishu.bot('recommend', 'AGC完成通知', f'{platform}任务数为0,不做拼接', name, mark_name)
  330. return
  331. elif platform == "常规":
  332. pq_ids = data["pq_id"]# 账号ID
  333. pq_ids_list = pq_ids.split(',')
  334. feishu_id = data["feishu_id"] # 飞书文档ID
  335. video_call = data["video_call"] # 脚本sheet
  336. video_count = data["video_count"]
  337. if int(video_count) == 0:
  338. Feishu.bot('recommend', 'AGC完成通知', f'{platform}任务数为{video_count},不做拼接', name, mark_name)
  339. return mark
  340. data_list, videos_mark = Material.get_all_data(feishu_id, video_call, mark)
  341. list_data = cls.get_unique_uid_data(data_list, int(video_count))
  342. elif platform == "跟随":
  343. pq_ids = data["pq_id"]
  344. pq_ids_list = pq_ids.split(',') # 账号ID
  345. feishu_id = data["feishu_id"] # 飞书文档ID
  346. video_call = data["video_call"]
  347. video_count = data["video_count"]
  348. if int(video_count) == 0:
  349. Feishu.bot('recommend', 'AGC完成通知', f'{platform}任务数为{video_count},不做拼接', name, mark_name)
  350. return mark
  351. data_list, videos = Material.get_all_data(feishu_id, video_call, mark)
  352. list_data = cls.get_unique_uid_data(data_list, int(video_count))
  353. count = 0
  354. pj_count = 0
  355. error_count = 0
  356. total_count = 0
  357. while True:
  358. # 清空所有文件
  359. cls.clear_mp4_files(mark)
  360. time.sleep(10)
  361. s_path, v_path, video_path_url, v_oss_path = cls.create_folders(mark)
  362. if pj_count == 10:
  363. Feishu.bot('recommend', 'AGC拼接画面不足通知', f'{platform}任务视频画面不足,请及时补充!!', name, mark_name)
  364. return
  365. if count == len(list_data):
  366. break
  367. try:
  368. d_list = list_data[count]
  369. uid = d_list['uid'] # 音频id
  370. cover = d_list['cover']
  371. audio_title = d_list['title']
  372. srt_new = SRT.getSrt(int(uid))
  373. Common.logger("video").info(f"S{mark}的{platform}渠道音频ID{uid}")
  374. if error_count == 5:
  375. Common.logger("video").info(f"{platform}渠道音频id为{uid},任务处理异常5次,该任务跳过,请检查格式,{name}")
  376. # Feishu.bot('recommend', 'AGC异常通知', f'{platform}渠道音频id为{uid},任务处理异常5次,该任务跳过,请检查格式!', name, mark_name)
  377. count += 1
  378. if platform == "常规":
  379. time.sleep(300)
  380. continue
  381. if srt_new:
  382. # 创建临时字幕文件
  383. cls.create_subtitle_file(srt_new, s_path)
  384. Common.logger("video").info(f"S{mark}的{platform}渠道SRT 文件目录创建成功")
  385. try:
  386. # 获取音频
  387. audio_video = PQ.get_audio_url(uid)
  388. Common.logger("video").info(f"{mark}的{platform}渠道获音频成功{audio_video}")
  389. audio_duration = cls.get_audio_duration(audio_video)
  390. Common.logger("video").info(f"{mark}的{platform}渠道获音频秒数{audio_duration}")
  391. if int(audio_duration) >= 300:
  392. print(f"{mark}的{platform}渠道获取需要拼接的音频秒数为:{audio_duration},超过300秒不做拼接")
  393. count += 1
  394. continue
  395. Common.logger("video").info(f"{mark}的{platform}渠道获取需要拼接的音频秒数为:{audio_duration}")
  396. except Exception as e:
  397. count += 1
  398. if platform == "常规":
  399. time.sleep(150)
  400. # 清空所有mp4数据
  401. cls.clear_mp4_files(mark)
  402. Common.logger("video1").info(f"{platform}任务下,{uid}音频格式填写错误该任务跳过,请关注,{name},异常信息:{e}")
  403. # Feishu.bot('recommend', 'AGC异常通知', f'{platform}任务下,{uid}音频格式填写错误该任务跳过,请关注', name, mark_name)
  404. continue
  405. if platform != "常规":
  406. if platform == "爆款":
  407. videos = str(d_list['video'])
  408. if ',' in videos:
  409. videos = str(videos).split(',')
  410. else:
  411. videos = [str(videos)]
  412. video_id = random.choice(videos)
  413. video_url = PQ.get_audio_url(video_id)
  414. time.sleep(10)
  415. Common.logger("video").info(f"{mark}的{platform}渠道获取视频链接成功")
  416. download_video = Oss.download_url(video_url, video_path_url, str(video_id))
  417. if download_video:
  418. video_files = cls.zn_concatenate_videos(download_video, audio_duration, audio_video, platform,
  419. s_path, mark, v_oss_path)
  420. if os.path.isfile(v_oss_path):
  421. Common.logger("video").info(f"{mark}的{platform}渠道新视频生成成功")
  422. else:
  423. Common.logger("video").info(f"{mark}的{platform}渠道新视频生成失败")
  424. continue
  425. else:
  426. # chnnel_count = int(len(list_data)/2)
  427. channels = ["douyin", "kuaishou"]
  428. channel = random.choice(channels)
  429. user_id = sqlHelp.get_user_id(channel, mark)
  430. url_list, user = sqlHelp.get_url_list(user_id, mark, "50")
  431. videos = [list(item) for item in url_list]
  432. Common.logger("video").info(f"{mark}的{platform}渠道下载视频")
  433. videos = Oss.get_oss_url(videos, video_path_url)
  434. time.sleep(10)
  435. Common.logger("video").info(f"{mark}的{platform}渠道下载视频成功")
  436. video_files = cls.zw_concatenate_videos(videos, audio_duration, audio_video, platform, s_path, v_path,
  437. mark, v_oss_path)
  438. if video_files == "":
  439. pj_count += 1
  440. Common.logger("video").info(f"{mark}的{platform}渠道使用拼接视频为空")
  441. continue
  442. if os.path.isfile(v_oss_path):
  443. Common.logger("video").info(f"{mark}的{platform}渠道新视频生成成功")
  444. else:
  445. Common.logger("video").info(f"{mark}的{platform}渠道新视频生成失败")
  446. continue
  447. pj_count = 0
  448. error_count = 0
  449. # 随机生成视频oss_id
  450. oss_id = cls.random_id()
  451. Common.logger("video").info(f"{mark}的{platform}渠道上传到 OSS 生成视频id为:{oss_id}")
  452. oss_object_key = Oss.stitching_sync_upload_oss(v_oss_path, oss_id)
  453. status = oss_object_key.get("status")
  454. if status == 200:
  455. # 获取 oss 视频地址
  456. oss_object_key = oss_object_key.get("oss_object_key")
  457. Common.logger("video").info(f"{mark}的{platform}渠道拼接视频发送成功,OSS 地址:{oss_object_key}")
  458. time.sleep(10)
  459. if platform == "常规":
  460. # 已使用视频存入数据库
  461. Common.logger("video").info(f"{mark}的{platform}渠道开始已使用视频存入数据库")
  462. sqlHelp.insert_videoAudio(video_files, uid, platform, mark)
  463. Common.logger("video").info(f"{mark}的{platform}渠道完成已使用视频存入数据库")
  464. Common.logger("video").info(f"{mark}的{platform}渠道开始视频添加到对应用户")
  465. new_video_id, title = PQ.insert_piaoquantv(oss_object_key, audio_title, pq_ids_list, cover, uid)
  466. if new_video_id:
  467. Common.logger("video").info(f"{mark}的{platform}渠道视频添加到对应用户成功")
  468. count += 1
  469. total_count += 1
  470. if mark_name == "穆新艺":
  471. sheet = '50b8a1'
  472. elif mark_name == "信欣":
  473. sheet = 'UyVK7y'
  474. elif mark_name == "范军":
  475. sheet = 'uP3zbf'
  476. elif mark_name == "鲁涛":
  477. sheet = 'iDTHt4'
  478. elif mark_name == "余海涛":
  479. sheet = 'R1jIeT'
  480. elif mark_name == "罗情":
  481. sheet = 'iuxfAt'
  482. current_time = datetime.now()
  483. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  484. if platform == "常规":
  485. third_chars = [j[2] for j in video_files]
  486. data = ",".join(third_chars)
  487. user_id = user
  488. else:
  489. user_id = video_id
  490. data = ''
  491. values = [[mark, str(uid), str(user_id), data, title, new_video_id, formatted_time]]
  492. Feishu.insert_columns("LAn9so7E0hxRYht2UMEcK5wpnMj", sheet, "ROWS", 1, 2)
  493. random_wait_time = random.uniform(0.5, 2.5)
  494. time.sleep(random_wait_time)
  495. Feishu.update_values("LAn9so7E0hxRYht2UMEcK5wpnMj", sheet, "A2:Z2", values)
  496. if srt_new:
  497. current_time = datetime.now()
  498. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  499. values = [[mark, str(uid), srt_new, formatted_time]]
  500. random_wait_time = random.uniform(0.5, 2.5)
  501. time.sleep(random_wait_time)
  502. Feishu.insert_columns("IbVVsKCpbhxhSJtwYOUc8S1jnWb", "jd9qD9", "ROWS", 1, 2)
  503. time.sleep(random_wait_time)
  504. Feishu.update_values("IbVVsKCpbhxhSJtwYOUc8S1jnWb", "jd9qD9", "A2:Z2", values)
  505. if platform == "常规":
  506. time.sleep(300)
  507. except Exception as e:
  508. error_count += 1
  509. Common.logger("video").warning(f"{mark}的视频拼接失败:{e}\n")
  510. # 清空所有mp4数据
  511. cls.clear_mp4_files(mark)
  512. continue
  513. Feishu.bot('recommend', 'AGC完成通知', f'今日{platform}任务拼接任务完成,共{total_count}条', name, mark_name)