问题描述
我有一个名为 subtitles.srt
的非英文 srt 文件,我遵循了 moviepy 包 (https://moviepy.readthedocs.io/en/latest/_modules/moviepy/video/tools/subtitles.html) 的文档和源代码的说明:
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')
sub = SubtitlesClip("subtitles.srt",generator,encoding='utf-8')
这给出了错误 TypeError: __init__() got an unexpected keyword argument 'encoding'
。
在源代码中,类 SubtitlesClip
确实有一个关键字参数 encoding
。这是否意味着源代码的版本已过时或什么?我能做些什么呢?我什至试图将带有 moviepy.video.tools.subtitles
关键字参数的 encoding
的源代码直接复制到我的代码中,但它导致了更多错误,例如:
from moviepy.decorators import convert_path_to_string
未能导入装饰器 convert_path_to_string
。
源代码似乎与我安装的不一致。无论如何要解决它?如果没有,一般来说,是否有任何好的 Python 库替代品用于插入字幕或视频编辑?
编辑:我目前的解决方案是创建一个 SubtitlesClip
的子类并覆盖父类的构造函数:
from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.video.VideoClip import TextClip,VideoClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
import re
from moviepy.tools import cvsecs
def file_to_subtitles_with_encoding(filename):
""" Converts a srt file into subtitles.
The returned list is of the form ``[((ta,tb),'some text'),...]``
and can be fed to SubtitlesClip.
Only works for '.srt' format for the moment.
"""
times_texts = []
current_times = None
current_text = ""
with open(filename,'r',encoding='utf-8') as f:
for line in f:
times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)",line)
if times:
current_times = [cvsecs(t) for t in times]
elif line.strip() == '':
times_texts.append((current_times,current_text.strip('\n')))
current_times,current_text = None,""
elif current_times:
current_text += line
return times_texts
class SubtitlesClipUTF8(SubtitlesClip):
def __init__(self,subtitles,make_textclip=None):
VideoClip.__init__(self,has_constant_size=False)
if isinstance(subtitles,str):
subtitles = file_to_subtitles_with_encoding(subtitles)
#subtitles = [(map(cvsecs,tt),txt) for tt,txt in subtitles]
self.subtitles = subtitles
self.textclips = dict()
if make_textclip is None:
make_textclip = lambda txt: TextClip(txt,font='Georgia-Bold',color='white',stroke_color='black',stroke_width=0.5)
self.make_textclip = make_textclip
self.start=0
self.duration = max([tb for ((ta,txt) in self.subtitles])
self.end=self.duration
def add_textclip_if_none(t):
""" Will generate a textclip if it hasn't been generated asked
to generate it yet. If there is no subtitle to show at t,return
false. """
sub =[((ta,txt) for ((ta,txt) in self.textclips.keys()
if (ta<=t<tb)]
if not sub:
sub = [((ta,txt) in self.subtitles if
(ta<=t<tb)]
if not sub:
return False
sub = sub[0]
if sub not in self.textclips.keys():
self.textclips[sub] = self.make_textclip(sub[1])
return sub
def make_frame(t):
sub = add_textclip_if_none(t)
return (self.textclips[sub].get_frame(t) if sub
else np.array([[[0,0]]]))
def make_mask_frame(t):
sub = add_textclip_if_none(t)
return (self.textclips[sub].mask.get_frame(t) if sub
else np.array([[0]]))
self.make_frame = make_frame
hasmask = bool(self.make_textclip('T').mask)
self.mask = VideoClip(make_mask_frame,ismask=True) if hasmask else None
我实际上只更改了两行,但我必须创建一个新类并重新定义整个事情,所以我怀疑是否真的有必要。还有比这更好的解决方案吗?