Python 使用需要编码的moviepy向视频添加字幕
我有一个名为Python 使用需要编码的moviepy向视频添加字幕,python,moviepy,video-editing,srt,video-subtitles,Python,Moviepy,Video Editing,Srt,Video Subtitles,我有一个名为subtitles.srt的非英语srt文件,我遵循了moviepy软件包()的文档和源代码的说明: 这就产生了错误TypeError:\uuuu init\uuuu()得到了一个意外的关键字参数“encoding” 在源代码中,类SubtitlesClip确实有一个关键字参数encoding。这是不是意味着源代码的版本已经过时了?对此我能做些什么?我甚至尝试将带有编码关键字参数的moviepy.video.tools.subtitles的源代码直接复制到我的代码中,但这导致了更多错
subtitles.srt
的非英语srt文件,我遵循了moviepy软件包()的文档和源代码的说明:
这就产生了错误TypeError:\uuuu init\uuuu()得到了一个意外的关键字参数“encoding”
在源代码中,类SubtitlesClip
确实有一个关键字参数encoding
。这是不是意味着源代码的版本已经过时了?对此我能做些什么?我甚至尝试将带有编码
关键字参数的moviepy.video.tools.subtitles
的源代码直接复制到我的代码中,但这导致了更多错误,如在以下行:
from moviepy.decorators import convert_path_to_string
它无法导入装饰程序将\u路径\u转换为\u字符串
源代码似乎与我安装的不一致。不管怎么说,要修好它吗?如果没有,那么在插入字幕或视频编辑方面是否有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',
fontsize=24, color='white',
stroke_color='black', stroke_width=0.5)
self.make_textclip = make_textclip
self.start=0
self.duration = max([tb for ((ta,tb), 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,tb),txt) for ((ta,tb),txt) in self.textclips.keys()
if (ta<=t<tb)]
if not sub:
sub = [((ta,tb),txt) for ((ta,tb),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,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
从moviepy.video.tools.subtitles导入SubtitlesClip
从moviepy.video.VideoClip导入文本剪辑,视频剪辑
从moviepy.video.compositing.CompositeVideoClip导入CompositeVideoClip
进口稀土
从moviepy.tools导入cvsecs
def file_至_字幕_,带_编码(文件名):
“”“将srt文件转换为字幕。
返回的列表的格式为“`[(ta,tb),“某些文本”),…]``
并且可以输入到SCLIP。
目前仅适用于“.srt”格式。
"""
times_text=[]
当前时间=无
当前_text=“”
将open(文件名为'r',编码为'utf-8')作为f:
对于f中的行:
times=re.findall(([0-9]*:[0-9]*:[0-9]*,[0-9]*)”,第行)
如果时间:
当前_时间=[t的cvsecs(t)时间]
elif line.strip()=='':
times\u text.append((当前\u时间,当前\u text.strip('\n'))
当前\u时间,当前\u文本=无,“”
elif当前时间:
当前文本+=行
返回时间
类别SubtitlesClipUTF8(SubtitlesClip):
def_uuuinit_uuu(self,subtitles,make_textclip=None):
视频剪辑。初始化(self,具有常量大小=False)
如果存在(副标题,str):
字幕=文件\u到\u字幕\u和\u编码(字幕)
#字幕=[(tt的映射(cvsecs,tt),txt),字幕中的txt]
self.subtitles=字幕
self.textclips=dict()
如果make_textclip为无:
make_textclip=lambda txt:textclip(txt,font='Georgia-Bold',
fontsize=24,颜色为白色,
笔划颜色=黑色,笔划宽度=0.5)
self.make\u textclip=make\u textclip
self.start=0
self.duration=max([tb代表self.subtitles中的((ta,tb),txt)])
self.end=self.duration
def add_textclip_如果没有(t):
“”将生成未生成的文本剪辑
如果在t处没有要显示的字幕,则返回
假的
self.textclips.keys()中((ta,tb),txt)的sub=[((ta,tb),txt)
如果(ta文档中的最新版本(您正在查看的版本)对应于尚未发布到PyPI的开发版本2.x。您通过pip安装的版本很可能是1.0.3,这是PyPI上的最新版本,并且不允许使用编码参数
从引入该功能的地方可以看到,它只被标记为在2.x版本中发布
仅将该文件复制到源代码中很可能不起作用,因为它将取决于两个版本之间发生的更改。但是,如果您觉得有冒险精神,可以按照moviepy中的方法手动安装该软件包的开发版本
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',
fontsize=24, color='white',
stroke_color='black', stroke_width=0.5)
self.make_textclip = make_textclip
self.start=0
self.duration = max([tb for ((ta,tb), 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,tb),txt) for ((ta,tb),txt) in self.textclips.keys()
if (ta<=t<tb)]
if not sub:
sub = [((ta,tb),txt) for ((ta,tb),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,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