video_processor.py 37 KB

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