我正在使用python 3.6中的moviepy制作带字幕的视频。
from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.video.io.VideoFileClip import VideoFileClip
generator = (lambda txt: TextClip(txt, font='Georgia-Regular', fontsize=24, color='white'))
subtitles = SubtitlesClip("sub.md", generator)
myvideo = VideoFileClip("video.mp4")
final = CompositeVideoClip([myvideo, subtitles])
final.write_videofile("final.mp4", fps=myvideo.fps)
我遇到错误:
self.duration = max([tb for ((ta,tb), txt) in self.subtitles])
ValueError: max() arg is an empty sequence
答案 0 :(得分:0)
看下面的示例代码,看看是否有可以从中得出更正的提示。我还删除了指向moviepy的GitHub错误存储库的链接。
def add_text_to_movie(movie_fol, movie_name, out_movie_name, subs, fontsize=50, txt_color='red', font='Xolonium-Bold'):
from moviepy import editor
def annotate(clip, txt, txt_color=txt_color, fontsize=fontsize, font=font):
""" Writes a text at the bottom of the clip. """
txtclip = editor.TextClip(txt, fontsize=fontsize, font=font, color=txt_color)
cvc = editor.CompositeVideoClip([clip, txtclip.set_pos(('center', 'bottom'))])
return cvc.set_duration(clip.duration)
video = editor.VideoFileClip(op.join(movie_fol, movie_name))
annotated_clips = [annotate(video.subclip(from_t, to_t), txt) for (from_t, to_t), txt in subs]
final_clip = editor.concatenate_videoclips(annotated_clips)
final_clip.write_videofile(op.join(movie_fol, out_movie_name))