Ver código fonte

视频动态压缩

zhangyong 9 meses atrás
pai
commit
a5b1ec91ff
2 arquivos alterados com 23 adições e 28 exclusões
  1. 17 17
      common/ffmpeg.py
  2. 6 11
      video_rewriting/video_prep.py

+ 17 - 17
common/ffmpeg.py

@@ -155,7 +155,7 @@ class FFmpeg():
         time_offset = total_duration - 1  # 提取倒数第一秒的帧
         # 获取视频最后一秒,生成.jpg
         subprocess.run(
-            ['ffmpeg', '-ss', str(time_offset), '-i', new_video_path, '-t', str(total_duration), '-vf', 'fps=1', "-y", jpg_url])
+            ['ffmpeg', '-ss', str(time_offset), '-i', new_video_path, '-t', str(total_duration), '-vf', 'fps=1,scale=360:640', "-y", jpg_url])
         return jpg_url
 
     """
@@ -181,7 +181,7 @@ class FFmpeg():
     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}"
+        ffmpeg_cmd = f"ffmpeg -i {new_video_path} -vf 'scale=640:ih*640/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
 
@@ -208,6 +208,7 @@ class FFmpeg():
             "-i", single_video_txt,
             "-c:v", "libx264",
             "-c:a", "aac",
+            # '-vf', f"scale=640x360",
             "-vf", draw,
             "-y",
             single_video
@@ -244,8 +245,8 @@ class FFmpeg():
         height = 1080
         margin_v = int(height) // 8  # 可根据需要调整字幕和背景之间的距离
         subtitle_cmd = f"subtitles={pw_srt_path}:force_style='Fontsize=13,Fontname=wqy-zenhei,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000,Bold=1,MarginV={margin_v}'"
-        bg_position_offset = (int(height) - margin_v) / 1.75
-        background_cmd = f"drawbox=y=(ih-{int(height)}/2-{bg_position_offset}):color=yellow@1.0:width=iw:height={int(height)}/4:t=fill"
+        bg_position_offset = (int(360) - 360//8) / 1.75
+        background_cmd = f"drawbox=y=(ih-{int(360)}/2-{bg_position_offset}):color=yellow@1.0:width=iw:height={int(360)}/4:t=fill"
 
         ffmpeg_cmd = [
             'ffmpeg',
@@ -258,7 +259,7 @@ class FFmpeg():
             '-c:a', 'aac',  # 音频编码格式
             '-strict', 'experimental',  # 使用实验性编码器
             '-shortest',  # 确保输出视频的长度与音频一致
-            '-vf', f"scale=1080x1920,{background_cmd},{subtitle_cmd}",  # 视频过滤器,设置分辨率和其他过滤器
+            '-vf', f"{background_cmd},{subtitle_cmd}",  # 视频过滤器,设置分辨率和其他过滤器
             pw_url_path  # 输出的视频文件路径
         ]
         subprocess.run(ffmpeg_cmd)
@@ -270,8 +271,6 @@ class FFmpeg():
     @classmethod
     def concatenate_videos(cls, video_list, video_path_url):
         concatenate_videos_url = video_path_url + 'concatenate_videos.mp4'
-        # 获取视频的原始宽高信息
-        width, height = cls.get_w_h_size(video_list[0])
         # 拼接视频
         VIDEO_COUNTER = 0
         FF_INPUT = ""
@@ -282,7 +281,7 @@ class FFmpeg():
             # 添加输入文件
             FF_INPUT += f" -i {videos}"
             # 为每个视频文件统一长宽,并设置SAR(采样宽高比)
-            FF_SCALE += f"[{VIDEO_COUNTER}:v]scale={int(height)}x{int(width)},setsar=1[v{VIDEO_COUNTER}];"
+            FF_SCALE += f"[{VIDEO_COUNTER}:v]scale=360x640,setsar=1[v{VIDEO_COUNTER}];"
             # 为每个视频文件创建一个输入流,并添加到-filter_complex参数中
             FF_FILTER += f"[v{VIDEO_COUNTER}][{VIDEO_COUNTER}:a]"
             # 增加视频计数器
@@ -299,7 +298,7 @@ class FFmpeg():
     单个视频拼接
     """
     @classmethod
-    def single_video(cls, new_video_path, video_share, video_path_url, zm):
+    def single_video(cls, new_video_path, video_path_url, zm):
         single_video_url = video_path_url + 'single_video.mp4'
         single_video_srt = video_path_url + 'single_video.srt'
         # 获取时长
@@ -311,18 +310,19 @@ class FFmpeg():
             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\u2764\uFE0F{zm}\n\n")
-        width, height = cls.get_w_h_size(new_video_path)
+        # width, height = cls.get_w_h_size(new_video_path)
+        height = 1080
         box_height = int(int(height) / 4)  # 框的高度为视频高度的四分之一
 
         background_cmd = f"drawbox=y=ih-{70 + box_height}-{int(box_height / 20)}:color=yellow@1.0:width=iw:height={box_height}:t=fill"
 
-        if video_share == '有':
-            # 添加字幕 wqy-zenhei  Hiragino Sans GB
-            subtitle_cmd = f"subtitles={single_video_srt}:force_style='Fontsize=14,Fontname=wqy-zenhei,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000,Bold=1,MarginV=20'"
-            draw = f"{background_cmd},{subtitle_cmd}"
-        else:
-            subtitle_cmd = f"subtitles={single_video_srt}:force_style='Fontsize=14,Fontname=wqy-zenhei,Outline=2,PrimaryColour=&H00FFFF,SecondaryColour=&H000000,Bold=1,MarginV=20'"
-            draw = f"{subtitle_cmd}"
+        # if video_share == '有':
+        #     # 添加字幕 wqy-zenhei  Hiragino Sans GB
+        #     subtitle_cmd = f"subtitles={single_video_srt}:force_style='Fontsize=14,Fontname=wqy-zenhei,Outline=0,PrimaryColour=&H000000,SecondaryColour=&H000000,Bold=1,MarginV=20'"
+        #     draw = f"{background_cmd},{subtitle_cmd}"
+        # else:
+        subtitle_cmd = f"subtitles={single_video_srt}:force_style='Fontsize=14,Fontname=wqy-zenhei,Outline=2,PrimaryColour=&H00FFFF,SecondaryColour=&H000000,Bold=1,MarginV=20'"
+        draw = f"{subtitle_cmd}"
         # 多线程数
         num_threads = 5
         # 构建 FFmpeg 命令,生成视频

+ 6 - 11
video_rewriting/video_prep.py

@@ -88,19 +88,14 @@ class getVideo:
             piaoquan_id = str(task["piaoquan_id"])
             number = task["number"]  # 指定条数
             title = task["title"]
-            video_share = task["video_share"]
+            video_share_name = task["video_share"]
             video_ending = task["video_ending"]
             crop_total = task["crop_total"]
             gg_duration_total = task["gg_duration_total"]
             video_path_url = cls.create_folders(mark, str(task_mark))  # 创建目录
-
-            if video_share and video_share != 'None':
-                video_share_list = video_share.split('/')
-                video_share_mark = video_share_list[0]
-                video_share_name = video_share_list[1]
-                zm = Material.get_pzsrt_data("summary", pz_sheet, video_share_name)  # 获取srt
-                if zm == '':
-                    Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下片中标示填写错误,请关注!!!!', name)
+            zm = Material.get_pzsrt_data("summary", pz_sheet, video_share_name)  # 获取srt
+            if zm == '':
+                Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下片中标示填写错误,请关注!!!!', name)
             if ',' in channel_urls:
                 channel_url = channel_urls.split(',')
             else:
@@ -247,12 +242,12 @@ class getVideo:
                             Common.logger("log").info(f"{task_mark}下的视频{url},视频与片尾拼接成功")
                             time.sleep(1)
                             if video_share and video_share != 'None':
-                                new_video_path = FFmpeg.single_video(video_path, video_share_mark, video_path_url, zm)
+                                new_video_path = FFmpeg.single_video(video_path, video_path_url, zm)
                             else:
                                 new_video_path = video_path
                         else:
                             if video_share and video_share != 'None':
-                                new_video_path = FFmpeg.single_video(new_video_path, video_share_mark, video_path_url, zm)
+                                new_video_path = FFmpeg.single_video(new_video_path, video_path_url, zm)
                         time.sleep(1)
                         oss_id = cls.random_id()
                         Common.logger("log").info(f"{task_mark}下的视频{url},开始发送oss")