使用 moviepy 为电影添加字幕

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

我试着按照这个例子如何给电影剪辑添加字幕:

from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.video.io.VideoFileClip import VideoFileClip
subs = [((0, 3), 'sub1'),
        ((3, 7), 'sub2'),
        ((9, 11), 'sub3'),
        ((11, 16), 'sub4')]
subtitles = SubtitlesClip(subs)
clip = VideoFileClip(video_fname)
video = movedit.CompositeVideoClip([clip, subtitles])
video.to_videofile(output_video_name)

但是输出的电影竟然没有字幕。难道我做错了什么? 顺便说一句,subtitles.py 不适用于 python 3,这行代码

subtitles = [(map(cvsecs, tt),txt) for tt, txt in subtitles]

需要改成

subtitles = [(list(map(cvsecs, tt),txt)) for tt, txt in subtitles]

编辑

最终我用困难的方式创建了字幕:

from moviepy import editor
import os.path as op

def annotate(clip, txt, txt_color='red', fontsize=50, font='Xolonium-Bold'):
    """ 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))
subs = [((0, 4), 'subs1'),
        ((4, 9), 'subs2'),
        ((9, 12), 'subs3'),
        ((12, 16), 'subs4')]
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))

不完美,还有改进的空间,比如支持没有字幕的时间段,但是解决了我的问题

python video-processing subtitle moviepy
3个回答
8
投票

这也适用于

python3

from moviepy.editor import *
from moviepy.video.tools.subtitles import SubtitlesClip

generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
subs = [((0, 4), 'subs1'),
        ((4, 9), 'subs2'),
        ((9, 12), 'subs3'),
        ((12, 16), 'subs4')]

subtitles = SubtitlesClip(subs, generator)

video = VideoFileClip("input.mp4")
result = CompositeVideoClip([video, subtitles.set_pos(('center','bottom'))])

result.write_videofile("output.mp4", fps=video.fps, temp_audiofile="temp-audio.m4a", remove_temp=True, codec="libx264", audio_codec="aac")

0
投票

以前的解决方案不适用于 Mac 芯片 M1 和 Python 3.9.5.

对于给定的环境,SubtitlesClip API 只接受 SRT 文件。这是脚本工作:

from moviepy.editor import *
from moviepy.video.tools.subtitles import SubtitlesClip

generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
subs = SubtitlesClip('subtitles.srt', generator)
subtitles = SubtitlesClip(subs, generator)

video = VideoFileClip("input.mp4")
result = CompositeVideoClip([video, subtitles.set_pos(('center','bottom'))])

result.write_videofile("output.mp4")


subtitles.srt: 
1
00:00:00,000 --> 00:00:12,00
A customer review is
an evaluation of a
product or service
made by someone who
has purchased and
used, or had
experience with, a
product or service.

2
00:00:14,000 --> 00:00:27,00
Customer reviews
are a form of
customer feedback
on electronic
commerce and online
shopping sites.

3
00:00:14,000 --> 00:00:27,00

重要的是添加空的第三个条目,否则第二行将不会包含在视频中。


0
投票

我确实得到一个错误

self.duration = max([tb for ((ta,tb), txt) in self.subtitles]) 
                                 ^^^^^^^
TypeError: cannot unpack non-iterable NoneType object

Apple M2 Max, macOS:13.3.1 *.srt 文件有效且可以读取。 有什么建议吗?

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