Parcourir la source

增加拼接类

zhangyong il y a 1 an
Parent
commit
6444f05664
3 fichiers modifiés avec 165 ajouts et 11 suppressions
  1. 24 0
      common/sql_help.py
  2. 21 10
      main.py
  3. 120 1
      video_stitching/video_stitching.py

+ 24 - 0
common/sql_help.py

@@ -71,6 +71,30 @@ class sql():
         count = str(count).replace('(', '').replace(')', '').replace(',', '')
         return int(count)
 
+    #  获取抖音拼接数量
+    @classmethod
+    def get_pjdouyin_account_id(cls):
+        current_time = datetime.now()
+        formatted_time = current_time.strftime("%Y-%m-%d")
+        count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM video_audio WHERE time = '{formatted_time}' AND video_type = 6 GROUP BY audio, account_id) AS subquery;"""
+        count = MysqlHelper.get_values(count, "prod")
+        if count == None:
+            count = 0
+        count = str(count).replace('(', '').replace(')', '').replace(',', '')
+        return int(count)
+
+    #  获取快手拼接数量
+    @classmethod
+    def get_ksdouyin_account_id(cls):
+        current_time = datetime.now()
+        formatted_time = current_time.strftime("%Y-%m-%d")
+        count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM video_audio WHERE time = '{formatted_time}' AND video_type = 5 GROUP BY audio, account_id) AS subquery;"""
+        count = MysqlHelper.get_values(count, "prod")
+        if count == None:
+            count = 0
+        count = str(count).replace('(', '').replace(')', '').replace(',', '')
+        return int(count)
+
 
     @classmethod
     def update_inconformity_id_list(cls, id):

+ 21 - 10
main.py

@@ -5,10 +5,8 @@ import schedule
 import time
 sys.path.append(os.getcwd())
 from common import Common, Feishu
-from video_capture.kuaishou.kuaishou_author.kuaishou_author import kuaishouAuthor
 from common.sql_help import sql
 from video_stitching.video_stitching import VideoStitching
-from video_capture.douyin.douyin_author.douyin_author import douyinAuthor
 
 
 
@@ -18,16 +16,27 @@ def job_video_stitching():
     global flag
     dy_yinmei_count = sql.get_dy_yinmei_account_id()
     koubo_count = sql.get_koubo_account_id()
-    jieri_count = sql.get_jieri_account_id()
-    chunjie_zizhi_count = sql.get_chunjie_zizhi_account_id()
+    # jieri_count = sql.get_jieri_account_id()
+    # chunjie_zizhi_count = sql.get_chunjie_zizhi_account_id()
     ks_yinmei_count = sql.get_ks_yinmei_account_id()
+    pjdouyin_account_id = sql.get_pjdouyin_account_id()
+    ksdouyin_account_id = sql.get_ksdouyin_account_id()
 
     if int(ks_yinmei_count) < 10:
         Common.logger("video").info("开始执行-快手")
         video_type = "音画美文"
         channel_type = "kuaishou"
         VideoStitching.video_stitching(video_type, ks_yinmei_count, channel_type)
-
+    elif int(pjdouyin_account_id) < 5:
+        Common.logger("video").info("开始执行-抖音拼接")
+        video_type = "抖音-拼接类"
+        channel_type = "dypinjie"
+        VideoStitching.video_stitching_pinjie(video_type, pjdouyin_account_id, channel_type)
+    elif int(ksdouyin_account_id) < 5:
+        Common.logger("video").info("开始执行-快手拼接")
+        video_type = "快手-拼接类"
+        channel_type = "kspinjie"
+        VideoStitching.video_stitching_pinjie(video_type, pjdouyin_account_id, channel_type)
     elif int(dy_yinmei_count) < 10:
         Common.logger("video").info("开始执行-抖音")
         video_type = "音画美文"
@@ -50,9 +59,9 @@ def job_video_stitching():
         VideoStitching.video_stitching(video_type, koubo_count, channel_type)
     count = int(dy_yinmei_count + koubo_count + ks_yinmei_count)
     Common.logger("video").info(f"视频生成条数为:{count}")
-    if count == 30:
+    if count == 40:
         if flag:
-            Feishu.bot('recommend', '拼接视频', '自制视频拼接完成啦,共计30条~')
+            Feishu.bot('recommend', '拼接视频', '自制视频拼接完成啦,共计40条~')
             flag = False
     if count == 0:
         flag = True
@@ -63,9 +72,11 @@ def job_feishu_bot():
     koubo_count = sql.get_koubo_account_id()
     ks_yinmei_count = sql.get_ks_yinmei_account_id()
     # chunjie_zizhi_count = sql.get_chunjie_zizhi_account_id()
-    count = int(dy_yinmei_count + koubo_count + ks_yinmei_count)
-    if count < 30:
-        Feishu.bot('recommend', '拼接视频', f'视频生成异常,不符合预期,请检查\n目前生成数量如下:\n抖音视频拼接:{dy_yinmei_count}条 \n快手视频拼接:{ks_yinmei_count}条\n口播视频拼接:{koubo_count}条')
+    pjdouyin_account_id = sql.get_pjdouyin_account_id()
+    ksdouyin_account_id = sql.get_ksdouyin_account_id()
+    count = int(dy_yinmei_count + koubo_count + ks_yinmei_count + pjdouyin_account_id + ksdouyin_account_id)
+    if count < 40:
+        Feishu.bot('recommend', '拼接视频', f'视频生成异常,不符合预期,请检查\n目前生成数量如下:\n抖音-拼接类:{pjdouyin_account_id}条\n快手-拼接类:{ksdouyin_account_id}条\n抖音视频拼接:{dy_yinmei_count}条 \n快手视频拼接:{ks_yinmei_count}条\n口播视频拼接:{koubo_count}条')
 
 
 # 每15分钟执行一次

+ 120 - 1
video_stitching/video_stitching.py

@@ -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__':