video_processor.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. import configparser
  2. import json
  3. import os
  4. import random
  5. import re
  6. import shutil
  7. import time
  8. from datetime import datetime
  9. from common.odps_data import OdpsDataCount
  10. from common.redis import get_data, get_first_value_with_prefix, increment_key
  11. from common.tag_video import Tag
  12. from common.tts_help import TTS
  13. from common import Material, Feishu, Common, Oss, AliyunLogger
  14. from common.ffmpeg import FFmpeg
  15. from common.gpt4o_help import GPT4o
  16. from data_channel.douyin import DY
  17. from data_channel.dy_keyword import DyKeyword
  18. from data_channel.dy_ls import DYLS
  19. from data_channel.ks_keyword import KsKeyword
  20. from data_channel.ks_ls import KSLS
  21. from data_channel.ks_pc_keyword import KsPcKeyword
  22. from data_channel.kuaishou import KS
  23. from data_channel.kuaishouchuangzuozhe import KsFeedVideo
  24. from data_channel.piaoquan import PQ
  25. from common.sql_help import sqlCollect
  26. from data_channel.shipinhao import SPH
  27. # 读取配置文件
  28. from data_channel.shipinhaodandian import SPHDD
  29. from data_channel.sph_keyword import SphKeyword
  30. from data_channel.sph_ls import SPHLS
  31. config = configparser.ConfigParser()
  32. config.read('./config.ini')
  33. class VideoProcessor:
  34. """
  35. 视频处理类,包含创建文件夹、生成随机ID、删除文件和处理视频任务等方法。
  36. """
  37. @classmethod
  38. def create_folders(cls, mark):
  39. """
  40. 根据标示和任务标示创建目录
  41. """
  42. id = cls.random_id()
  43. video_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/" + str(id) + "/"
  44. if not os.path.exists(video_path_url):
  45. os.makedirs(video_path_url)
  46. return video_path_url
  47. @classmethod
  48. def random_id(cls):
  49. """
  50. 随机生成ID
  51. """
  52. now = datetime.now()
  53. rand_num = random.randint(10000, 99999)
  54. return f"{now.strftime('%Y%m%d%H%M%S')}{rand_num}"
  55. @classmethod
  56. def remove_files(cls, mark):
  57. """
  58. 删除指定目录下的所有文件和子目录
  59. """
  60. path = config['PATHS']['VIDEO_PATH'] + mark + "/"
  61. # 删除目录下的所有内容
  62. if os.path.exists(path):
  63. # 遍历目录下的所有文件和子目录
  64. for filename in os.listdir(path):
  65. file_path = os.path.join(path, filename)
  66. try:
  67. if os.path.isfile(file_path) or os.path.islink(file_path):
  68. os.unlink(file_path) # 删除文件或符号链接
  69. elif os.path.isdir(file_path):
  70. shutil.rmtree(file_path) # 删除子目录及其所有内容
  71. except Exception as e:
  72. print(f'Failed to delete {file_path}. Reason: {e}')
  73. @classmethod
  74. def process_task(cls, task, mark, name, feishu_id, cookie_sheet):
  75. """
  76. 处理单个任务
  77. """
  78. task_mark = task["task_mark"]
  79. channel_id = str(task["channel_id"])
  80. url = str(task["channel_url"])
  81. piaoquan_id = str(task["piaoquan_id"])
  82. number = task["number"]
  83. title = task["title"]
  84. video_share = task["video_share"]
  85. video_ending = task["video_ending"]
  86. crop_total = task["crop_total"]
  87. gg_duration_total = task["gg_duration_total"]
  88. voice = task['voice']
  89. if voice:
  90. if ',' in voice:
  91. voices = voice.split(',')
  92. else:
  93. voices = [voice]
  94. voice = random.choice(voices)
  95. else:
  96. voice = "zhifeng_emo"
  97. zm = Material.get_pzsrt_data("summary", "500Oe0", video_share)
  98. Common.logger(mark).info(f"{name}的{task_mark}下{channel_id}的用户:{url}开始获取视频")
  99. new_count = None
  100. if name in ['快手品类账号', '抖音品类账号', '抖音品类账号-1', '视频号品类账号']:
  101. new_count = OdpsDataCount.main(channel_id, name, url)
  102. data_list = cls.get_data_list(
  103. channel_id, task_mark, url,
  104. new_count if new_count is not None else number,
  105. mark, feishu_id, cookie_sheet, name, task
  106. )
  107. if new_count:
  108. sqlCollect.insert_spider_supply_targetcnt(channel_id, name, url, number, new_count, str(len(data_list)))
  109. current_time = datetime.now()
  110. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  111. values = [
  112. [
  113. name,
  114. channel_id,
  115. url,
  116. str(number),
  117. str(new_count),
  118. str(len(data_list)),
  119. formatted_time
  120. ]
  121. ]
  122. Feishu.insert_columns("Z5xLsdyyxh3abntTTvUc9zw8nYd", "099da8", "ROWS", 1, 2)
  123. time.sleep(0.5)
  124. Feishu.update_values("Z5xLsdyyxh3abntTTvUc9zw8nYd", "099da8", "A2:Z2", values)
  125. if not data_list:
  126. AliyunLogger.logging(channel_id, name, url, "", "无改造视频", "4000")
  127. Common.logger(mark).info(f"{name}的{task_mark}下{channel_id}的视频ID{url} 无改造视频")
  128. text = (
  129. f"**通知类型**: 没有改造的视频\n"
  130. f"**负责人**: {name}\n"
  131. f"**渠道**: {channel_id}\n"
  132. f"**视频主页ID**: {url}\n"
  133. )
  134. Feishu.finish_bot(text, "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  135. "【 机器改造通知 】")
  136. return
  137. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 获取视频完成,共{len(data_list)}条")
  138. for video in data_list:
  139. try:
  140. cls.remove_files(mark)
  141. video_path_url = cls.create_folders(mark)
  142. new_title = cls.generate_title(video, title)
  143. v_id = video["video_id"]
  144. cover = video["cover"]
  145. video_url = video["video_url"]
  146. old_title = video['old_title']
  147. rule = video['rule']
  148. # if channel_id == "单点视频":
  149. # redis_video = get_redis_video_data(v_id)
  150. # if redis_video:
  151. # continue
  152. if not old_title:
  153. old_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  154. text = (
  155. f"**通知类型**: 标题为空,使用兜底标题生成片尾\n"
  156. f"**负责人**: {name}\n"
  157. f"**渠道**: {channel_id}\n"
  158. f"**视频主页ID**: {url}\n"
  159. f"**视频Video_id**: {v_id}\n"
  160. )
  161. Feishu.finish_bot(text,
  162. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  163. "【 机器改造通知 】")
  164. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},标题为空,使用兜底标题生成片尾")
  165. time.sleep(1)
  166. pw_random_id = cls.random_id()
  167. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 开始下载视频")
  168. new_video_path = cls.download_and_process_video(channel_id, video_url, video_path_url, v_id,
  169. crop_total, gg_duration_total, pw_random_id, new_title, mark, video)
  170. if not os.path.isfile(new_video_path) or new_video_path == None:
  171. AliyunLogger.logging(channel_id, name, url, v_id, "视频下载失败", "3002", f"video_url:{video_url}")
  172. text = (
  173. f"**通知类型**: 视频下载失败\n"
  174. f"**负责人**: {name}\n"
  175. f"**渠道**: {channel_id}\n"
  176. f"**视频主页ID**: {url}\n"
  177. f"**视频Video_id**: {v_id}\n"
  178. )
  179. Feishu.finish_bot(text,
  180. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  181. "【 机器改造通知 】")
  182. continue
  183. if new_video_path:
  184. if video_ending and video_ending != 'None':
  185. new_video_path = cls.handle_video_ending(new_video_path, video_ending, old_title, pw_random_id, video_path_url, mark, task_mark, url, name, video_share, zm, voice)
  186. if new_video_path == None:
  187. if name == "单点视频":
  188. sqlCollect.update_shp_dd_vid_4(v_id)
  189. from_user_name = video['from_user_name'] # 来源用户
  190. from_group_name = video['from_group_name'] # 来源群组
  191. source = video['source'] # 渠道
  192. text = (
  193. f"**渠道**: {source}\n"
  194. f"**来源用户**: {from_user_name}\n"
  195. f"**来源群组**: {from_group_name}\n"
  196. f"**原视频链接**: {video['video_url']}\n"
  197. f"**原视频封面**: {video['cover']}\n"
  198. f"**原视频标题**: {video['old_title']}\n"
  199. )
  200. Feishu.finish_bot(text,
  201. "https://open.feishu.cn/open-apis/bot/v2/hook/493b3d4c-5fae-4a9d-980b-1dd86636524e",
  202. "【 视频下载失败,跳过该视频 】")
  203. continue
  204. else:
  205. if video_share and video_share != 'None':
  206. new_video_path = FFmpeg.single_video(new_video_path, video_path_url, zm)
  207. # new_video_path = FFmpeg.single_video(new_video_path, video_path_url, zm)
  208. if not os.path.isfile(new_video_path):
  209. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title}"
  210. AliyunLogger.logging(channel_id, name, url, v_id, "视频改造失败", "3001", log_data)
  211. text = (
  212. f"**通知类型**: 视频改造失败\n"
  213. f"**负责人**: {name}\n"
  214. f"**渠道**: {channel_id}\n"
  215. f"**视频主页ID**: {url}\n"
  216. f"**视频Video_id**: {v_id}\n"
  217. )
  218. Feishu.finish_bot(text,
  219. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  220. "【 机器改造通知 】")
  221. continue
  222. # 上传视频和封面,并更新数据库
  223. code = cls.upload_video_and_thumbnail(new_video_path, cover, v_id, new_title, task_mark, name, piaoquan_id,
  224. video_path_url, mark, channel_id, url, old_title, title, rule)
  225. # 更新已使用的视频号状态
  226. pq_url = f'https://admin.piaoquantv.com/cms/post-detail/{code}/detail' # 站内视频链接
  227. if name == "单点视频":
  228. sphdd_status = sqlCollect.update_shp_dd_vid(v_id)
  229. if sphdd_status == 1:
  230. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 视频修改已使用,状态已修改")
  231. from_user_name = video['from_user_name'] # 来源用户
  232. from_group_name = video['from_group_name'] # 来源群组
  233. source = video['source'] # 渠道
  234. channel_id = source
  235. text = (
  236. f"**站内视频链接**: {pq_url}\n"
  237. f"**渠道**: {source}\n"
  238. f"**来源用户**: {from_user_name}\n"
  239. f"**来源群组**: {from_group_name}\n"
  240. f"**原视频链接**: {video['video_url']}\n"
  241. f"**原视频封面**: {video['cover']}\n"
  242. f"**原视频标题**: {video['old_title']}\n"
  243. )
  244. Feishu.finish_bot(text, "https://open.feishu.cn/open-apis/bot/v2/hook/493b3d4c-5fae-4a9d-980b-1dd86636524e", "【 有一条新的内容改造成功 】")
  245. if channel_id == "快手历史" or channel_id == "抖音历史" or channel_id == "视频号历史":
  246. explain = "历史爆款"
  247. else:
  248. explain = "新供给"
  249. current_time = datetime.now()
  250. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  251. if name == "品类关键词搜索":
  252. first_category = task["first_category"]
  253. secondary_category = task["secondary_category"]
  254. keyword_principal = task["keyword_name"]
  255. tag_first = f"一级品类_{first_category}"
  256. tag_keyword = f"关键词_{url}"
  257. if channel_id == "抖音搜索":
  258. tag_channel = "来源_抖音关键词"
  259. elif channel_id == "快手搜索":
  260. tag_channel = "来源_快手关键词"
  261. elif channel_id == "视频号搜索":
  262. tag_channel = "来源_视频号关键词"
  263. tag = f"{tag_first},{tag_keyword},{tag_channel}"
  264. tag_status = Tag.video_tag(code, tag)
  265. if tag_status == 0:
  266. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url}下的票圈视频{code},写入标签成功")
  267. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice},,first_category:{first_category},,secondary_category:{secondary_category},,keyword_principal:{keyword_principal},,tag:{tag}"
  268. values = [
  269. [
  270. name,
  271. task_mark,
  272. channel_id,
  273. url,
  274. str(v_id),
  275. piaoquan_id,
  276. old_title,
  277. title if title in ["原标题", "AI标题"] else "",
  278. new_title,
  279. str(code),
  280. formatted_time,
  281. str(rule),
  282. explain,
  283. voice,
  284. first_category,
  285. secondary_category,
  286. keyword_principal,
  287. pq_url
  288. ]
  289. ]
  290. elif name == "抖音品类账号-1" or name == "抖音品类账号" or name == "视频号品类账号" or name == "快手品类账号":
  291. first_category = task["first_category"]
  292. tag_first = f"一级品类_{first_category}"
  293. if channel_id == "抖音" or channel_id == "抖音历史":
  294. tag_channel = "来源_抖音品类账号"
  295. elif channel_id == "快手" or channel_id == "快手历史":
  296. tag_channel = "来源_快手品类账号"
  297. elif channel_id == "视频号" or channel_id == "快手历史":
  298. tag_channel = "来源_视频号品类账号"
  299. tag = f"{tag_first},{tag_channel}"
  300. tag_status = Tag.video_tag( code, tag )
  301. if tag_status == 0:
  302. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url}下的票圈视频{code},写入标签成功")
  303. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice},,tag:{tag}"
  304. # log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice},,first_category:{first_category},,tag:{tag}"
  305. values = [
  306. [
  307. name,
  308. task_mark,
  309. channel_id,
  310. url,
  311. str( v_id ),
  312. piaoquan_id,
  313. old_title,
  314. title if title in ["原标题", "AI标题"] else "",
  315. new_title,
  316. str( code ),
  317. formatted_time,
  318. str( rule ),
  319. explain,
  320. voice,
  321. first_category,
  322. pq_url
  323. ]
  324. ]
  325. else:
  326. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice}"
  327. values = [
  328. [
  329. name,
  330. task_mark,
  331. channel_id,
  332. url,
  333. str(v_id),
  334. piaoquan_id,
  335. old_title,
  336. title if title in ["原标题", "AI标题"] else "",
  337. new_title,
  338. str(code),
  339. formatted_time,
  340. str(rule),
  341. explain,
  342. voice
  343. ]
  344. ]
  345. AliyunLogger.logging(channel_id, name, url, v_id, "视频改造成功", "1000", log_data, str(code))
  346. text = (
  347. f"**通知类型**: 视频改造成功\n"
  348. f"**站内视频链接**: {pq_url}\n"
  349. f"**负责人**: {name}\n"
  350. f"**渠道**: {channel_id}\n"
  351. f"**视频主页ID**: {url}\n"
  352. f"**视频Video_id**: {v_id}\n"
  353. f"**使用音频音色**: {voice}\n"
  354. )
  355. Feishu.finish_bot(text,
  356. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  357. "【 机器改造通知 】")
  358. if values:
  359. if name == "王雪珂":
  360. sheet = "vfhHwj"
  361. elif name == "抖音品类账号-1":
  362. sheet = "61kvW7"
  363. elif name == "鲁涛":
  364. sheet = "FhewlS"
  365. elif name == "范军":
  366. sheet = "B6dCfS"
  367. elif name == "余海涛":
  368. sheet = "mfBrNT"
  369. elif name == "罗情":
  370. sheet = "2J3PwN"
  371. elif name == "王玉婷":
  372. sheet = "bBHFwC"
  373. elif name == "刘诗雨":
  374. sheet = "fBdxIQ"
  375. elif name == "信欣":
  376. sheet = "lPe1eT"
  377. elif name == "快手创作者版品类推荐流":
  378. sheet = "k7l7nQ"
  379. elif name == "抖音品类账号":
  380. sheet = "Bsg5UR"
  381. elif name == "视频号品类账号":
  382. sheet = "b0uLWw"
  383. elif name == "单点视频":
  384. sheet = "ptgCXW"
  385. elif name == "快手品类账号":
  386. sheet = "ibjoMx"
  387. elif name == "品类关键词搜索":
  388. sheet = "Tgpikc"
  389. Feishu.insert_columns("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "ROWS", 1, 2)
  390. time.sleep(0.5)
  391. Feishu.update_values("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "A2:Z2", values)
  392. except Exception as e:
  393. Common.logger(mark).error(f"{name}的{task_mark}任务处理失败:{e}")
  394. continue
  395. @classmethod
  396. def get_data_list(cls, channel_id, task_mark, url, number, mark, feishu_id, cookie_sheet, name, task):
  397. """
  398. 根据渠道ID获取数据列表
  399. """
  400. if channel_id == "抖音":
  401. return DY.get_dy_url(task_mark, url, number, mark, feishu_id, cookie_sheet, channel_id, name)
  402. elif channel_id == "票圈":
  403. return PQ.get_pq_url(task_mark, url, number, mark, channel_id, name)
  404. elif channel_id == "视频号":
  405. return SPH.get_sph_url(task_mark, url, number, mark, channel_id, name)
  406. elif channel_id == "快手":
  407. return KS.get_ks_url(task_mark, url, number, mark, feishu_id, cookie_sheet, channel_id, name)
  408. elif channel_id == "快手创作者版":
  409. return KsFeedVideo.get_data(channel_id, name)
  410. elif channel_id == "单点视频":
  411. return SPHDD.get_sphdd_data(url, channel_id, name)
  412. elif channel_id == "抖音历史":
  413. return DYLS.get_dy_zr_list(task_mark, url, number, mark, channel_id, name)
  414. elif channel_id == "快手历史":
  415. return KSLS.get_ksls_list(task_mark, url, number, mark, channel_id, name)
  416. elif channel_id == "视频号历史":
  417. return SPHLS.get_sphls_data(task_mark, url, number, mark, channel_id, name)
  418. elif channel_id == '抖音搜索':
  419. return DyKeyword.get_key_word(url, task_mark, mark, channel_id, name, task)
  420. elif channel_id == '快手搜索':
  421. return KsKeyword.get_key_word(url, task_mark, mark, channel_id, name, task)
  422. elif channel_id == '视频号搜索':
  423. return SphKeyword.get_key_word(url, task_mark, mark, channel_id, name)
  424. @classmethod
  425. def generate_title(cls, video, title):
  426. """
  427. 生成新标题
  428. """
  429. if video['old_title']:
  430. new_title = video['old_title'].strip().replace("\n", "") \
  431. .replace("/", "").replace("\\", "").replace("\r", "") \
  432. .replace(":", "").replace("*", "").replace("?", "") \
  433. .replace("?", "").replace('"', "").replace("<", "") \
  434. .replace(">", "").replace("|", "").replace(" ", "") \
  435. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  436. .replace("'", "").replace("#", "").replace("Merge", "")
  437. else:
  438. return '这个视频,分享给我的老友,祝愿您能幸福安康'
  439. if title == "原标题":
  440. if not new_title:
  441. new_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  442. elif title == "AI标题":
  443. if not new_title:
  444. new_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  445. else:
  446. new_title = GPT4o.get_ai_title(new_title)
  447. else:
  448. titles = title.split('/') if '/' in title else [title]
  449. new_title = random.choice(titles)
  450. return new_title
  451. @classmethod
  452. def download_and_process_video(cls, channel_id, video_url, video_path_url, v_id, crop_total, gg_duration_total,
  453. pw_random_id, new_title, mark, video):
  454. """
  455. 下载并处理视频
  456. """
  457. if channel_id == "单点视频":
  458. new_video_path = PQ.dd_sph_download_video(video_url, video_path_url, v_id, video, channel_id)
  459. if new_video_path == None:
  460. return None
  461. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  462. elif channel_id == "视频号":
  463. new_video_path = PQ.sph_download_video(video_url, video_path_url, v_id, video)
  464. if new_video_path == None:
  465. return None
  466. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  467. elif channel_id == "票圈" or channel_id == "快手创作者版" or channel_id == '视频号搜索':
  468. new_video_path = PQ.download_video(video_url, video_path_url, v_id)
  469. if new_video_path == None:
  470. return None
  471. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  472. elif channel_id == "视频号历史":
  473. new_video_path = Oss.download_sph_ls(video_url, video_path_url, v_id)
  474. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  475. else:
  476. Common.logger(mark).info(f"视频准备下载")
  477. new_video_path = Oss.download_video_oss(video_url, video_path_url, v_id)
  478. Common.logger(mark).info(f"视频下载成功: {new_video_path}")
  479. if os.path.isfile(new_video_path):
  480. if crop_total and crop_total != 'None': # 判断是否需要裁剪
  481. new_video_path = FFmpeg.video_crop(new_video_path, video_path_url, pw_random_id)
  482. if gg_duration_total and gg_duration_total != 'None': # 判断是否需要指定视频时长
  483. new_video_path = FFmpeg.video_ggduration(new_video_path, video_path_url, pw_random_id,
  484. gg_duration_total)
  485. width, height = FFmpeg.get_w_h_size(new_video_path)
  486. if width < height: # 判断是否需要修改为竖屏
  487. new_video_path = FFmpeg.update_video_h_w(new_video_path, video_path_url, pw_random_id)
  488. new_title_re = re.sub(r'[^\w\s\u4e00-\u9fff,。!?]', '', new_title)
  489. if len(new_title_re) > 12:
  490. new_title_re = '\n'.join(
  491. [new_title_re[i:i + 12] for i in range(0, len(new_title_re), 12)])
  492. new_video_path = FFmpeg.add_video_zm(new_video_path, video_path_url, pw_random_id, new_title_re)
  493. return new_video_path
  494. else:
  495. return None
  496. @classmethod
  497. def handle_video_ending(cls, new_video_path, video_ending, old_title, pw_random_id, video_path_url, mark, task_mark, url, name, video_share, zm, voice):
  498. """
  499. 处理视频片尾
  500. """
  501. if video_ending == "AI片尾引导":
  502. pw_srt_text = GPT4o.get_ai_pw(old_title)
  503. if pw_srt_text:
  504. pw_url = TTS.get_pw_zm(pw_srt_text, voice)
  505. if pw_url:
  506. pw_mp3_path = TTS.download_mp3(pw_url, video_path_url, pw_random_id)
  507. # oss_mp3_key = Oss.mp3_upload_oss(pw_mp3_path, pw_random_id)
  508. # oss_mp3_key = oss_mp3_key.get("oss_object_key")
  509. # new_pw_path = f"http://art-crawler.oss-cn-hangzhou.aliyuncs.com/{oss_mp3_key}"
  510. # print(f"mp3地址:{new_pw_path}")
  511. # pw_url_sec = FFmpeg.get_video_duration(pw_mp3_path)
  512. pw_srt = TTS.getSrt(pw_url)
  513. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾srt成功")
  514. else:
  515. # Feishu.bot('zhangyong', 'TTS获取失败提示', f'无法获取到片尾音频,及时更换token', "张勇")
  516. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾失败")
  517. return None
  518. else:
  519. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾失败")
  520. return None
  521. else:
  522. if ',' in video_ending:
  523. video_ending_list = video_ending.split(',')
  524. else:
  525. video_ending_list = [video_ending]
  526. ending = random.choice(video_ending_list)
  527. pw_list = Material.get_pwsrt_data("summary", "DgX7vC", ending) # 获取srt
  528. if pw_list:
  529. pw_id = pw_list["pw_id"]
  530. pw_srt = pw_list["pw_srt"]
  531. pw_url = PQ.get_pw_url(pw_id)
  532. pw_mp3_path = FFmpeg.get_video_mp3(pw_url, video_path_url, pw_random_id)
  533. else:
  534. Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下片尾标示错误,请关注!!!!', name)
  535. for attempt in range(3):
  536. jpg_path = FFmpeg.video_png(new_video_path, video_path_url, pw_random_id) # 生成视频最后一帧jpg
  537. if os.path.isfile(jpg_path):
  538. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},生成视频最后一帧成功")
  539. break
  540. time.sleep(1)
  541. for attempt in range(3):
  542. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取mp3成功")
  543. pw_path = FFmpeg.pw_video(jpg_path, video_path_url, pw_mp3_path, pw_srt, pw_random_id,
  544. pw_mp3_path) # 生成片尾视频
  545. if os.path.isfile(pw_path):
  546. Common.logger(mark).info(f"{task_mark}下的视频{url},生成片尾视频成功")
  547. break
  548. time.sleep(1)
  549. pw_video_list = [new_video_path, pw_path]
  550. Common.logger(mark).info(f"{task_mark}下的视频{url},视频与片尾开始拼接")
  551. video_path = FFmpeg.concatenate_videos(pw_video_list, video_path_url) # 视频与片尾拼接到一起
  552. if video_path:
  553. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},视频与片尾拼接成功")
  554. time.sleep(1)
  555. if video_share and video_share != 'None':
  556. new_video_path = FFmpeg.single_video(video_path, video_path_url, zm)
  557. else:
  558. new_video_path = video_path
  559. return new_video_path
  560. else:
  561. return None
  562. @classmethod
  563. def upload_video_and_thumbnail(cls, new_video_path: str, cover: str, v_id, new_title: str, task_mark: str, name: str, piaoquan_id,
  564. video_path_url: str, mark: str, channel_id: str, url: str, old_title: str, title, rule: str):
  565. """
  566. 上传视频和封面到OSS,并更新数据库
  567. """
  568. try:
  569. oss_id = cls.random_id()
  570. Common.logger(mark).info(f"{name}的{task_mark},开始发送oss")
  571. oss_object_key = Oss.stitching_sync_upload_oss(new_video_path, oss_id) # 视频发送OSS
  572. Common.logger(mark).info(f"{name}的{task_mark},发送oss成功{oss_object_key}")
  573. status = oss_object_key.get("status")
  574. if status == 200:
  575. oss_object_key = oss_object_key.get("oss_object_key")
  576. time.sleep(1)
  577. if channel_id == "快手历史":
  578. jpg = None
  579. else:
  580. if channel_id == "视频号历史":
  581. jpg_path = Oss.download_sph_ls(cover, video_path_url, v_id)
  582. else:
  583. jpg_path = PQ.download_video_jpg(cover, video_path_url, v_id) # 下载视频封面
  584. if os.path.isfile(jpg_path):
  585. oss_jpg_key = Oss.stitching_fm_upload_oss(jpg_path, oss_id) # 封面发送OSS
  586. status = oss_jpg_key.get("status")
  587. if status == 200:
  588. jpg = oss_jpg_key.get("oss_object_key")
  589. else:
  590. jpg = None
  591. else:
  592. jpg = None
  593. code = PQ.insert_piaoquantv(oss_object_key, new_title, jpg, piaoquan_id)
  594. Common.logger(mark).info(f"{name}的{task_mark}下的视频ID{v_id}发送成功")
  595. sqlCollect.insert_task(task_mark, v_id, mark, channel_id) # 插入数据库
  596. current_time = datetime.now()
  597. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  598. if name == "单点视频":
  599. url = str(rule)
  600. sqlCollect.insert_machine_making_data(name, task_mark, channel_id, url, v_id, piaoquan_id, new_title, code,
  601. formatted_time, old_title, oss_object_key)
  602. return code
  603. except Exception as e:
  604. Common.logger(mark).error(f"{name}的{task_mark}上传视频和封面到OSS,并更新数据库失败:{e}\n")
  605. return
  606. @classmethod
  607. def main(cls, data):
  608. """
  609. 主函数,初始化任务并使用线程池处理任务。
  610. """
  611. mark = data["mark"]
  612. name = data["name"]
  613. feishu_id = data["feishu_id"]
  614. feishu_sheet = data["feishu_sheet"]
  615. cookie_sheet = data["cookie_sheet"]
  616. try:
  617. data = get_data(mark, feishu_id, feishu_sheet)
  618. if not data:
  619. Common.logger("redis").error(f"{mark}任务开始新的一轮\n")
  620. return
  621. task = json.loads(data)
  622. if mark == 'dy-pl-gjc' and task['channel_id'] == '抖音搜索':
  623. mark_count = 'dyss-count'
  624. count = get_first_value_with_prefix(mark_count)
  625. increment_key(mark_count)
  626. if int(count) >= 300:
  627. Common.logger(mark).log(f"抖音搜索接口今日已经上限")
  628. return "抖音搜索上限"
  629. if mark == 'ks-pl-gjc':
  630. mark_count = 'ksss-count'
  631. count = get_first_value_with_prefix(mark_count)
  632. increment_key(mark_count)
  633. if int(count) >= 300:
  634. Common.logger(mark).log(f"快手搜索接口今日已经上限")
  635. return "快手搜索上限"
  636. if mark == 'sph-pl-gjc':
  637. mark_count = 'ss-sph-count'
  638. count = get_first_value_with_prefix(mark_count)
  639. increment_key(mark_count)
  640. if int(count) >= 300:
  641. Common.logger(mark).log(f"视频号搜索接口今日已经上限")
  642. return "视频号搜索上限"
  643. if mark == 'sph-plzh'and task['channel_id'] == '视频号':
  644. mark_count = 'sph-count'
  645. count = get_first_value_with_prefix(mark_count)
  646. increment_key(mark_count)
  647. if int(count) >= 400:
  648. Common.logger(mark).log(f"视频号获取用户主页今日已经上限")
  649. return "视频号获取用户主页视频上限"
  650. VideoProcessor.process_task(task, mark, name, feishu_id, cookie_sheet)
  651. return mark
  652. except Exception as e:
  653. Common.logger(mark).error(f"任务处理失败: {e}")
  654. return mark
  655. # if __name__ == "__main__":
  656. # main()