生成透明背景和淡入淡出效果的视频

问题描述 投票:0回答:1

我正在使用这个(python)命令生成一个只有字符串和透明背景的视频:

cmd = [
    "ffmpeg", "-y",
    "-loglevel", "panic",
    "-f", "lavfi",
    "-i", f"[email protected]:s=1920x1080:rate=25:duration={duration},format=rgba", 
    "-vf", f"drawtext=text='{string}':fontfile='{font}':fontsize={font_size}:fontcolor={font_color}:x=(w-tw)-{bottom_right_offset.split(':')[0]}:y=(h-th)-{bottom_right_offset.split(':')[1]}:borderw=2:bordercolor=black", 
    "-vcodec", "prores_ks",
    "-profile:v", "4",
    "-pix_fmt", "yuva444p10le",
    output_file
]
subprocess.run(cmd)

效果很好,但我还想为我的文本添加淡入/淡出效果(这样,当我将此视频添加到 Adobe Premiere 中的另一个视频之上时,文本会慢慢出现和消失)。我还不知道该怎么做。我可以获得透明背景或淡入/淡出效果,但似乎我无法让两者一起工作。有什么想法吗?

ffmpeg
1个回答
0
投票

要使用 FFmpeg 在视频中实现透明背景和淡入/淡出效果,您可以修改命令以包含文本的淡入淡出滤镜。但是,由于您正在使用透明背景并想要应用淡入淡出效果,因此这有点棘手,因为您希望淡入淡出效果应用于文本的 Alpha 通道而不是整个帧。

一种方法是使用 Alpha 通道创建文本,然后对该 Alpha 通道应用淡入和淡出效果。不幸的是,drawtext 不直接支持文本淡入淡出。相反,您可以使用解决方法,通过在透明背景上生成带有文本的单独视频(或叠加层),然后在将其与透明背景视频组合之前对此叠加层应用淡入淡出效果。

以下是如何修改现有命令以在文本上包含淡入和淡出效果。此解决方案涉及在绘图文本过滤器中使用enable=' Between(t,{start_time},{end_time})' 选项来控制文本随时间的不透明度,本质上是模拟淡入淡出效果。请注意,{start_time} 和 {end_time} 应分别替换为您希望淡入开始和淡出结束的时间(以秒为单位)。

   cmd = [
    "ffmpeg", "-y",
    "-loglevel", "panic",
    "-f", "lavfi",
    "-i", f"[email protected]:s=1920x1080:rate=25:duration={duration},format=rgba", 
    "-vf", f"drawtext=text='{string}':fontfile='{font}':fontsize={font_size}:fontcolor={font_color}@1: x=(w-tw)/2:y=(h-th)/2:enable='between(t,{start_time},{fade_in_duration})+between(t,{duration}-{fade_out_duration},{duration})',format=rgba,fade=t=in:st={start_time}:d={fade_in_duration}:alpha=1,fade=t=out:st={duration}-{fade_out_duration}:d={fade_out_duration}:alpha=1", 
    "-vcodec", "prores_ks",
    "-profile:v", "4",
    "-pix_fmt", "yuva444p10le",
    output_file
]
subprocess.run(cmd)

在此修改后的命令中,将 {start_time}、{fade_in_duration} 和 {fade_out_duration} 替换为您想要的淡入开始时间(以秒为单位)、淡入持续时间和淡出持续时间,分别。 fontcolor={font_color}@1 中的 @1 确保文本在应该可见时完全不透明,并且淡入淡出滤镜会随着时间的推移调整不透明度。此命令假设淡入在视频中的 {start_time} 秒处开始,持续 {fade_in_duration} 秒,然后淡出在视频结束前 {fade_out_duration} 秒开始,一直持续到结束。

请记住,这种方法直接操纵文本叠加的不透明度。根据您视频的淡入淡出效果要求调整 {start_time}、{fade_in_duration} 和 {fade_out_duration}。

© www.soinside.com 2019 - 2024. All rights reserved.