|
@@ -59,7 +59,9 @@ class VideoStitching():
|
|
|
if channel_type == "douyin":
|
|
|
video_type = 0
|
|
|
elif channel_type == "kschunjie":
|
|
|
- video_type = 4
|
|
|
+ video_type = 5
|
|
|
+ elif channel_type == "dypinjie":
|
|
|
+ video_type = 6
|
|
|
else:
|
|
|
video_type = 2
|
|
|
for j in audio_url:
|
|
@@ -181,6 +183,10 @@ class VideoStitching():
|
|
|
list = ["67231152", "67231153", "67231154", "67231155", "67231157"]
|
|
|
elif channel_type == "douyin":
|
|
|
list = ["67231113", "67231112", "67231111", "67231110", "67231109"]
|
|
|
+ elif channel_type == "dypinjie":
|
|
|
+ list = ["68276195", "68276196", "68276197", "68276198", "68276199"]
|
|
|
+ elif channel_type == "kspinjie":
|
|
|
+ list = ["68276262", "68276263", "68276264", "68276265", "68276267"]
|
|
|
else:
|
|
|
list = ["67413406", "67413407", "67413408", "67413409", "67413410"]
|
|
|
code = 1
|
|
@@ -483,7 +489,120 @@ class VideoStitching():
|
|
|
Common.logger("video").warning(f"新拼接视频发送oss失败:{e}\n")
|
|
|
return
|
|
|
|
|
|
+ # 视频拼接
|
|
|
+ @classmethod
|
|
|
+ def concatenate_pinjie_videos(cls, videos):
|
|
|
+ clips = []
|
|
|
+ total_duration = 0
|
|
|
+ included_videos = []
|
|
|
+ # 设置最大可使用的内存限制(单位:字节)
|
|
|
+ memory_limit = 6 * 1024 * 1024 * 1024 # 6GB
|
|
|
+ resource.setrlimit(resource.RLIMIT_AS, (memory_limit, memory_limit))
|
|
|
+ # 设置固定
|
|
|
+ duration_limit = 120
|
|
|
+ # 遍历每个视频并计算总时长
|
|
|
+ for i, video in enumerate(videos):
|
|
|
+ filename = video[2].split("/")[-1]
|
|
|
+ clip = VideoFileClip(f'./video_stitching/video_material/{filename}.mp4')
|
|
|
+ clips.append(clip)
|
|
|
+ total_duration += clip.duration
|
|
|
+ if total_duration >= duration_limit:
|
|
|
+ break
|
|
|
+
|
|
|
+ # 如果总时长小于等于目标时长,则不做视频拼接
|
|
|
+ if total_duration <= duration_limit:
|
|
|
+ Common.logger("video").info(f"时长小于等于目标时长,不做视频拼接")
|
|
|
+ # 关闭视频文件
|
|
|
+ for clip in clips:
|
|
|
+ clip.close()
|
|
|
+ for video in videos:
|
|
|
+ filename = video[2].split("/")[-1]
|
|
|
+ os.remove(f'./video_stitching/video_material/{filename}.mp4')
|
|
|
+ return ""
|
|
|
+ else:
|
|
|
+ Common.logger("video").info(f"总时长大于目标时长")
|
|
|
+ remaining_time = duration_limit
|
|
|
+ final_clips = []
|
|
|
+
|
|
|
+ for clip, video in zip(clips, videos):
|
|
|
+ if remaining_time - clip.duration >= 0:
|
|
|
+ final_clips.append(clip)
|
|
|
+ included_videos.append(video)
|
|
|
+ remaining_time -= clip.duration
|
|
|
+ else:
|
|
|
+ # 如果剩余时间不足以加入下一个视频,则截断当前视频并返回已包含的URL
|
|
|
+ final_clips.append(clip)
|
|
|
+ included_videos.append(video)
|
|
|
+ break
|
|
|
+ final_clip = concatenate_videoclips(final_clips)
|
|
|
+ # 统一设置视频分辨率
|
|
|
+ final_width = 320
|
|
|
+ final_height = 480
|
|
|
+ video_with_subtitles = final_clip.resize((final_width, final_height))
|
|
|
+ # 生成视频
|
|
|
+ video_with_subtitles.write_videofile(output_path, fps=35)
|
|
|
+ if os.path.isfile(output_path):
|
|
|
+ Common.logger("video").info("视频生成成功!生成路径为:", output_path)
|
|
|
+ return included_videos, video_with_subtitles, clips
|
|
|
+ else:
|
|
|
+ Common.logger("video").info("视频生成失败,请检查代码和文件路径。")
|
|
|
+ return "", video_with_subtitles, clips
|
|
|
+
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def video_stitching_pinjie(cls, video_type, count, channel_type):
|
|
|
+ title_list = Material.get_pinjie_title()
|
|
|
+ # 获取已入库的用户id
|
|
|
+ account_id = cls.get_account_id(channel_type)
|
|
|
+ account = random.choice(account_id)
|
|
|
+ account = str(account).replace('(', '').replace(')', '').replace(',', '')
|
|
|
+ Common.logger("video").info(f"获取用户ID:{account}")
|
|
|
+ # 获取 未使用的视频链接
|
|
|
+ url_list = cls.get_url_list('', account)
|
|
|
+ if url_list == None:
|
|
|
+ Common.logger("video").info(f"未使用视频链接为空:{url_list}")
|
|
|
+ return
|
|
|
+ videos = [list(item) for item in url_list]
|
|
|
+ videos = Oss.get_oss_url(video_type, videos)
|
|
|
+ # 视频截取
|
|
|
+ try:
|
|
|
+ audio_url, video_with_subtitles, clips = cls.concatenate_pinjie_videos(videos)
|
|
|
+ if len(audio_url) == 0:
|
|
|
+ Common.logger("video").info(f"视频生成失败")
|
|
|
+ # 随机生成视频id
|
|
|
+ id = cls.random_id()
|
|
|
+ Common.logger("video").info(f"生成视频id为:{id}")
|
|
|
+ # 上传 oss
|
|
|
+ oss_object_key = Oss.stitching_sync_upload_oss(output_path, id)
|
|
|
+ status = oss_object_key.get("status")
|
|
|
+ # 获取 oss 视频地址
|
|
|
+ oss_object_key = oss_object_key.get("oss_object_key")
|
|
|
+ Common.logger("video").info(f"新拼接视频,oss发送成功,oss地址:{oss_object_key}")
|
|
|
+ if status == 200:
|
|
|
+ time.sleep(10)
|
|
|
+ # 发送成功 已使用视频存入数据库
|
|
|
+ cls.insert_videoAudio(audio_url, '', channel_type)
|
|
|
+ Common.logger("video").info(f"发送成功 已使用视频存入数据库完成")
|
|
|
+ if os.path.isfile(output_path):
|
|
|
+ os.remove(output_path)
|
|
|
+ Common.logger("video").info(f"文件删除成功{output_path}")
|
|
|
+ else:
|
|
|
+ Common.logger("video").info(f"文件不存在{output_path}")
|
|
|
+ for video in videos:
|
|
|
+ filename = video[2].split("/")[-1]
|
|
|
+ os.remove(f'./video_stitching/video_material/{filename}.mp4')
|
|
|
+ piaoquantv = cls.insert_piaoquantv(oss_object_key, title_list, video_type, channel_type)
|
|
|
+ if piaoquantv:
|
|
|
+ Common.logger("video").info(f"视频添加到对应用户成功")
|
|
|
+ # 关闭视频文件
|
|
|
+ for clip in clips:
|
|
|
+ clip.close()
|
|
|
+ # 释放视频对象
|
|
|
+ video_with_subtitles.close()
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ Common.logger("video").warning(f"新拼接视频发送oss失败:{e}\n")
|
|
|
+ return
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|