zhangyong 5 mēneši atpakaļ
vecāks
revīzija
f8772ec2d0
3 mainītis faili ar 49 papildinājumiem un 47 dzēšanām
  1. 19 31
      carry_video/carry_video.py
  2. 2 4
      common/download_video.py
  3. 28 12
      common/ffmpeg.py

+ 19 - 31
carry_video/carry_video.py

@@ -131,7 +131,7 @@ class CarryViode:
                               "【 搬运&改造效率工具失败通知 】")
             return
         if not url:
-            in_carry_video_data(REDIS_NAME, data)
+            in_carry_video_data(REDIS_NAME, json.dumps(data, ensure_ascii=False, indent=4))
             logger.info(f"[+] {url}没有获取到视频链接,等待重新处理")
             AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
                                  "没有获取到视频链接,等待重新处理", "1002", str(data))
@@ -144,22 +144,19 @@ class CarryViode:
                               "https://open.feishu.cn/open-apis/bot/v2/hook/65bc5463-dee9-46d0-bc2d-ec6c49a8f3cd",
                               "【 搬运&改造效率工具失败通知 】")
             return
-        logger.info(f"[+] {url}开始处理标题")
-        if data["title_category"] == "AI标题" or data["trailer_share"] == "AI标题":
-            title = GPT4o.get_ai_title(original_title if data["title_category"] == "原标题" else data["title_category"])
-        else:
-            title = original_title if data["title_category"] == "原标题" else data["title_category"]
+
         logger.info(f"[+] {url}开始下载视频")
         video_path = DownLoad.download_video(url, file_path)
         # video_path = Oss.download_video_oss(url, file_path)
-        if not video_path:
+        if not os.path.exists(video_path) or os.path.getsize(video_path) == 0:
+            in_carry_video_data(REDIS_NAME, json.dumps(data, ensure_ascii=False, indent=4))
             logger.error(f"[+] {url}下载失败")
             AliyunLogger.logging(data["name"],"效率工具", data["tag_transport_channel"], data["video_url"],
-                                 "视频下载失败", "3002", str(data))
+                                 "视频下载失败等待重新处理", "3002", str(data))
             text = (
                 f"**负责人**: {data['name']}\n"
                 f"**内容**: {data}\n"
-                f"**失败信息**: 视频下载失败\n"
+                f"**失败信息**: 视频下载失败等待重新处理,视频链接{url}\n"
             )
             Feishu.finish_bot(text,
                               "https://open.feishu.cn/open-apis/bot/v2/hook/65bc5463-dee9-46d0-bc2d-ec6c49a8f3cd",
@@ -188,6 +185,7 @@ class CarryViode:
 
 
         elif data["transform_rule"] == "仅改造":
+            video_path = FFmpeg.video_640(video_path, file_path)
             if data["video_clipping"]:  # 判断是否需要裁剪
                 video_path = FFmpeg.video_crop(video_path, file_path)
             if data["video_clipping_time"]:  # 判断是否需要指定视频时长
@@ -223,7 +221,7 @@ class CarryViode:
                 logger.info(f"[+] {REDIS_NAME}的{data}数据片尾获取成功")
                 pw_srt = TTS.getSrt(pw_url)
                 pw_mp3_path = TTS.download_mp3(pw_url, file_path)
-                if not pw_mp3_path or not pw_mp3_path:
+                if not pw_mp3_path:
                     logger.error(f"[+] {REDIS_NAME}的{data}数据片尾音频下载失败")
                     AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
                                          "改造失败,片尾音频下载失败", "3001", str(data))
@@ -238,7 +236,7 @@ class CarryViode:
                     return
                 logger.info(f"[+] {REDIS_NAME}的{data}数据片尾音频下载成功")
                 jpg_path = FFmpeg.video_png(video_path, file_path)  # 生成视频最后一帧jpg
-                if not jpg_path:
+                if not os.path.exists(jpg_path) or os.path.getsize(jpg_path) == 0:
                     logger.error(f"[+] {REDIS_NAME}的{data}数据片尾获取最后一帧失败")
                     AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
                                          "改造失败,获取最后一帧失败", "3001", str(data))
@@ -253,7 +251,7 @@ class CarryViode:
                     return
                 logger.info(f"[+] {REDIS_NAME}的{data}数据片尾获取最后一帧成功")
                 pw_path = FFmpeg.pw_video(jpg_path, file_path, pw_mp3_path, pw_srt)  # 生成片尾视频
-                if not pw_path:
+                if not os.path.exists(pw_path) or os.path.getsize(pw_path) == 0:
                     logger.error(f"[+] {REDIS_NAME}的{data}数据片尾拼接失败")
                     AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
                                          "改造失败,片尾拼接失败", "3001", str(data))
@@ -266,26 +264,10 @@ class CarryViode:
                                       "https://open.feishu.cn/open-apis/bot/v2/hook/65bc5463-dee9-46d0-bc2d-ec6c49a8f3cd",
                                       "【 搬运&改造效率工具失败通知 】")
                     return
-                pw_video_list = [video_path, pw_path]
+                video_paths = [video_path, pw_path]
                 logger.info(f"[+] {REDIS_NAME}的{data}数据合并开始拼接")
-                video_path = FFmpeg.concatenate_videos(pw_video_list, file_path)  # 视频与片尾拼接到一起
-                if not video_path:
-                    logger.error(f"[+] {REDIS_NAME}的{data}数据合并拼接失败")
-                    AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
-                                         "改造失败,合并拼接失败", "3001", str(data))
-                    text = (
-                        f"**负责人**: {data['name']}\n"
-                        f"**内容**: {data}\n"
-                        f"**失败信息**: 视频合并拼接失败\n"
-                    )
-                    Feishu.finish_bot(text,
-                                      "https://open.feishu.cn/open-apis/bot/v2/hook/65bc5463-dee9-46d0-bc2d-ec6c49a8f3cd",
-                                      "【 搬运&改造效率工具失败通知 】")
-                    return
-                logger.info(f"[+] {REDIS_NAME}的{data}数据合并拼接成功")
-            if data["video_share"]:
-                video_path = FFmpeg.single_video(video_path, file_path, data["video_share"])
-                if not video_path:
+                video_path = FFmpeg.single_video(video_paths, file_path, data["video_share"])
+                if not os.path.exists(video_path) or os.path.getsize(video_path) == 0:
                     logger.error(f"[+] {REDIS_NAME}的{data}数据添加片中字幕失败")
                     AliyunLogger.logging(data["name"], "效率工具",data["tag_transport_channel"], data["video_url"],
                                          "改造失败,添加片中字幕失败", "3001", str(data))
@@ -325,6 +307,12 @@ class CarryViode:
             n_id_list = [n_ids]
         pq_list = []
         for n_id in n_id_list:
+            logger.info(f"[+] {url}开始处理标题")
+            if data["title_category"] == "AI标题" or data["trailer_share"] == "AI标题":
+                title = GPT4o.get_ai_title(
+                    original_title if data["title_category"] == "原标题" else data["title_category"])
+            else:
+                title = original_title if data["title_category"] == "原标题" else data["title_category"]
             code = PQ.insert_piaoquantv(oss_object_key, title, n_id)
             if not code:
                 logger.error(f"[+] {REDIS_NAME}的{data}写入票圈后台失败")

+ 2 - 4
common/download_video.py

@@ -16,7 +16,6 @@ class DownLoad:
             ffmpeg_cmd_oss = [
                 "ffmpeg",
                 "-y",  # 覆盖输出文件
-                "-protocol_whitelist", "file,http,https,tcp,tls",
                 "-i", video_url,  # 输入文件
                 "-c", "copy",  # 复制视频流
                 "-bsf:a", "aac_adtstoasc",  # 转换 AAC 音频格式
@@ -36,8 +35,7 @@ class DownLoad:
                         with open(f"{video}", "wb") as file:
                             # 将响应内容写入文件
                             file.write(response.content)
-                        time.sleep(5)
                         return video
-                return None
+                return video
             except Exception:
-                return None
+                return video

+ 28 - 12
common/ffmpeg.py

@@ -46,6 +46,8 @@ class FFmpeg():
             capture_output=True, text=True)
         return float(result.stdout)
 
+
+
     """
     视频裁剪
     """
@@ -90,6 +92,7 @@ class FFmpeg():
         except ValueError as e:
             return 1920, 1080
 
+
     """
     视频裁剪
     """
@@ -155,7 +158,7 @@ class FFmpeg():
         # 获取视频时长
         total_duration = cls.get_video_duration(video_path)
         if total_duration == 0:
-            return None
+            return jpg_url
         time_offset = total_duration - 1  # 提取倒数第一秒的帧
         # 获取视频最后一秒,生成.jpg
         subprocess.run(
@@ -189,6 +192,14 @@ class FFmpeg():
         subprocess.run(ffmpeg_cmd, shell=True)
         return video_h_w_path
 
+    """视频转为640像素"""
+    @classmethod
+    def video_640(cls, video_path, file_path):
+        video_url = file_path + 'pixelvideo.mp4'
+        ffmpeg_cmd = f"ffmpeg -i {video_path} -vf 'scale=640:trunc(ih*640/iw/2)*2' {video_url}"
+        subprocess.run(ffmpeg_cmd, shell=True)
+        return video_url
+
     """横屏视频顶部增加字幕"""
     @classmethod
     def add_video_zm(cls, new_video_path, video_path_url, pw_random_id, new_text):
@@ -252,7 +263,7 @@ class FFmpeg():
         pw_url_path = file_path + 'pw_video.mp4'
         pw_duration = cls.get_mp3_duration(pw_mp3_path)
         if pw_duration == 0:
-            return None
+            return pw_url_path
         time.sleep(2)
         # 添加字幕 wqy-zenhei  Hiragino Sans GB
         height = 1080
@@ -305,11 +316,12 @@ class FFmpeg():
         ffmpeg_cmd.extend(["-filter_complex", f"{FF_SCALE}{FF_FILTER}concat=n={VIDEO_COUNTER}:v=1:a=1[v][a]",
                            "-map", "[v]", "-map", "[a]", "-y", concatenate_videos_url])
         try:
-            result = subprocess.run(ffmpeg_cmd, capture_output=True, text=True)
-            if result.returncode != 0:
-                return None
-            else:
-                return concatenate_videos_url
+            print(ffmpeg_cmd)
+            # result = subprocess.run(ffmpeg_cmd)
+            # if result.returncode != 0:
+            #     return None
+            # else:
+            #     return concatenate_videos_url
         except Exception as e:
             logger.error(f"[+] 视频合并失败,失败信息{e}")
             return None
@@ -318,18 +330,22 @@ class FFmpeg():
     单个视频拼接
     """
     @classmethod
-    def single_video(cls, video_path, file_path, zm):
+    def single_video(cls, video_paths, file_path, zm):
         single_video_url = file_path + 'single_video.mp4'
         single_video_srt = file_path + 'single_video.srt'
         # 获取时长
-        duration = cls.get_video_duration(video_path)
-        if duration == 0:
+        durations = 0
+        for video in video_paths:
+            duration = cls.get_video_duration(video)
+            durations += duration  # 累加时长
+        if durations == 0:
             return single_video_url
         start_time = cls.seconds_to_srt_time(2)
-        end_time = cls.seconds_to_srt_time(duration)
+        end_time = cls.seconds_to_srt_time(durations)
         single_video_txt = file_path + 'single_video.txt'
         with open(single_video_txt, 'w') as f:
-            f.write(f"file '{video_path}'\n")
+            for video in video_paths:
+                f.write(f"file '{video}'\n")
         if zm:
             with open(single_video_srt, 'w') as f:
                 f.write(f"1\n{start_time} --> {end_time}\n<font color=\"red\">\u2764\uFE0F</font>{zm}\n\n")