Browse Source

兼容横屏视频改造

zhangyong 9 months ago
parent
commit
1cbe88c39a
2 changed files with 52 additions and 1 deletions
  1. 41 1
      common/ffmpeg.py
  2. 11 0
      video_rewriting/video_prep.py

+ 41 - 1
common/ffmpeg.py

@@ -116,7 +116,7 @@ class FFmpeg():
         return crop_url
 
     """
-    视频裁剪
+    视频截断
     """
     @classmethod
     def video_ggduration(cls, new_video_path, video_path_url, pw_random_id, gg_duration_total):
@@ -176,6 +176,46 @@ class FFmpeg():
         time.sleep(1)
         return pw_mp3_path
 
+    """横屏视频改为竖屏"""
+    @classmethod
+    def update_video_h_w(cls, new_video_path, video_path_url, pw_random_id):
+        video_h_w_path = video_path_url + str(pw_random_id) +'video_h_w_video.mp4'
+
+        ffmpeg_cmd = f"ffmpeg -i {new_video_path} -vf 'scale=1080:ih*1080/iw,pad=iw:iw*16/9:(ow-iw)/2:(oh-ih)/2' {video_h_w_path}"
+        subprocess.run(ffmpeg_cmd, shell=True)
+        return video_h_w_path
+
+    """横屏视频顶部增加字幕"""
+    @classmethod
+    def add_video_zm(cls, new_video_path, video_path_url, pw_random_id, new_text):
+        single_video_srt = video_path_url + str(pw_random_id) +'video_zm.srt'
+        single_video_txt = video_path_url + str(pw_random_id) +'video_zm.txt'
+        single_video = video_path_url + str(pw_random_id) +'video_zm.mp4'
+        duration = cls.get_video_duration(new_video_path)
+        start_time = cls.seconds_to_srt_time(0)
+        end_time = cls.seconds_to_srt_time(duration)
+        # zm = '致敬伟大的教员,为整个民族\n感谢老人家历史向一代伟人'
+        with open(single_video_txt, 'w') as f:
+            f.write(f"file '{new_video_path}'\n")
+        with open(single_video_srt, 'w') as f:
+            f.write(f"1\n{start_time} --> {end_time}\n{new_text}\n\n")
+        subtitle_cmd = f"subtitles={single_video_srt}:force_style='Fontsize=12,Fontname=wqy-zenhei,Outline=2,PrimaryColour=&H00FFFF,SecondaryColour=&H000000,Bold=1,MarginV=225'"
+        draw = f"{subtitle_cmd}"
+        ffmpeg_cmd = [
+            "ffmpeg",
+            "-f", "concat",
+            "-safe", "0",
+            "-i", single_video_txt,
+            "-c:v", "libx264",
+            "-c:a", "aac",
+            "-vf", draw,
+            "-y",
+            single_video
+        ]
+        subprocess.run(ffmpeg_cmd)
+        return single_video
+
+
     """
      生成片尾视频
     """

+ 11 - 0
video_rewriting/video_prep.py

@@ -1,6 +1,7 @@
 import configparser
 import os
 import random
+import re
 import sys
 import threading
 import time
@@ -174,6 +175,16 @@ class getVideo:
                             new_video_path = FFmpeg.video_crop(new_video_path, video_path_url, pw_random_id)
                         if gg_duration_total and gg_duration_total != 'None':  # 判断是否需要指定视频时长
                             new_video_path = FFmpeg.video_ggduration(new_video_path, video_path_url, pw_random_id, gg_duration_total)
+                        width, height = FFmpeg.get_w_h_size(new_video_path)
+                        if width < 1920:
+                            new_video_path = FFmpeg.update_video_h_w(new_video_path, video_path_url, pw_random_id)
+                            time.sleep(1)
+                            if os.path.isfile(new_video_path):
+                                new_title_re = re.sub(r'[^\w\s\u4e00-\u9fff,。!?]', '', new_title)
+                                if len(new_title_re) > 12:
+                                    new_text = '\n'.join(
+                                        [new_title_re[i:i + 12] for i in range(0, len(new_title_re), 12)])
+                                new_video_path = FFmpeg.add_video_zm(new_video_path, video_path_url, pw_random_id, new_text)
                         if video_ending and video_ending != 'None':
                             if video_ending == "AI片尾引导":
                                 pw_srt_text = GPT4o.get_ai_pw(old_title)