diff --git a/entity/ffmpeg.py b/entity/ffmpeg.py index 38ced9a..660fc95 100644 --- a/entity/ffmpeg.py +++ b/entity/ffmpeg.py @@ -353,6 +353,42 @@ class FfmpegTask(object): effect_index += 1 filter_args.append(f"{video_output_str}trim=end={show_seconds}[v_eff{effect_index}]") video_output_str = f"[v_eff{effect_index}]" + elif effect.startswith("grid4:"): + param = effect.split(":", 2)[1] + if param == '': + param = "1" + delay_seconds = float(param) + effect_index += 1 + + # 获取分辨率,如果没有设置则使用默认值 + if self.resolution: + width, height = self.resolution.split('x') + else: + width, height = "1920", "1080" # 默认分辨率 + + # 计算四宫格中每个象限的大小 + grid_width = int(width) // 2 + grid_height = int(height) // 2 + + # 分割视频流为4份 + filter_args.append(f"{video_output_str}split=4[grid_v1_{effect_index}][grid_v2_{effect_index}][grid_v3_{effect_index}][grid_v4_{effect_index}]") + + # 创建黑色背景 + filter_args.append(f"color=black:size={width}x{height}:duration=1[bg_{effect_index}]") + + # 缩放每个视频流到绝对尺寸 + filter_args.append(f"[grid_v1_{effect_index}]scale={grid_width}:{grid_height}[v1_scaled_{effect_index}]") + filter_args.append(f"[grid_v2_{effect_index}]scale={grid_width}:{grid_height},tpad=start_duration={delay_seconds}[v2_scaled_{effect_index}]") + filter_args.append(f"[grid_v3_{effect_index}]scale={grid_width}:{grid_height},tpad=start_duration={delay_seconds*2}[v3_scaled_{effect_index}]") + filter_args.append(f"[grid_v4_{effect_index}]scale={grid_width}:{grid_height},tpad=start_duration={delay_seconds*3}[v4_scaled_{effect_index}]") + + # 使用overlay将四个视频流叠加到四个位置 + filter_args.append(f"[bg_{effect_index}][v1_scaled_{effect_index}]overlay=0:0:shortest=1[grid_step1_{effect_index}]") + filter_args.append(f"[grid_step1_{effect_index}][v2_scaled_{effect_index}]overlay=w/2:0:shortest=1[grid_step2_{effect_index}]") + filter_args.append(f"[grid_step2_{effect_index}][v3_scaled_{effect_index}]overlay=0:h/2:shortest=1[grid_step3_{effect_index}]") + filter_args.append(f"[grid_step3_{effect_index}][v4_scaled_{effect_index}]overlay=w/2:h/2:shortest=1[v_eff{effect_index}]") + + video_output_str = f"[v_eff{effect_index}]" ... if self.resolution: filter_args.append(f"{video_output_str}scale={self.resolution.replace('x', ':')}[v]")