{"text": " In an era dominated by short-form videos, numerous user-friendly editing software options exist. However, for specialized video formats like e-book presentations or comic readings, streamlining the video creation process through automation stands as a crucial enhancement for efficiency.",
"segments": [{"id": 0,
"seek": 0,
"start": 0.0,
"end": 6.7,
"text": " In an era dominated by short-form videos, numerous user-friendly editing software options exist.",
"tokens": [],
"temperature": 0.0,
"avg_logprob": -0.170647520768015,
"compression_ratio": 1.4947916666666667,
"no_speech_prob": 0.007638249080628157
},
{"id": 1,
"seek": 0,
"start": 6.7,
"end": 12.0,
"text": " However, for specialized video formats like e-book presentations or comic readings,",
"tokens": [],
"temperature": 0.0,
"avg_logprob": -0.170647520768015,
"compression_ratio": 1.4947916666666667,
"no_speech_prob": 0.007638249080628157
},
{"id": 2,
"seek": 0,
"start": 12.0,
"end": 19.0,
"text": " streamlining the video creation process through automation stands as a crucial enhancement for efficiency.",
"tokens": [],
"temperature": 0.0,
"avg_logprob": -0.170647520768015,
"compression_ratio": 1.4947916666666667,
"no_speech_prob": 0.007638249080628157
}],
"language": "en"}
可以看到,result 中包含一个 segments 的数组,start 表示开始时间,end 表示结束时间,而 text 就是字幕文本。
from moviepy.video.tools.subtitles import SubtitlesClip
subtitles = []
for segment in segments:
subtitle = (segment['start'], segment['end']), segment['text']
subtitles.append(subtitle)
subtitles_clip = SubtitlesClip(subtitles)
OK,现在我们已经成功创建了字幕 clip 文件。
合成视频
有了字幕片段 Clip,让我们将其与原视频合成,生成一个包含字幕的文件。
代码如下所示:
1
2
3
video = VideoFileClip("input_video2.mp4")
video = CompositeVideoClip([video, subtitles_clip])
video.write_videofile("output_with_subtitles.mp4", audio_codec="aac")