python使用moviepy给视频加字幕时报错,请问下如何处理呢?

给视频添加字幕时出现错误

from moviepy.editor import *

#set_duration 持续时间;set_start开始显示字幕时间

txtClip = TextClip('哈喽 中国!',color='white', font='C:/Windows/Fonts/simhei.ttf',
                   kerning = 5, fontsize=20,bg_color='gray').set_duration(5).set_pos('bottom').set_start(1)

txtClip2 = TextClip('哈喽2 中国!',color='white', font='C:/Windows/Fonts/simhei.ttf',
                   kerning = 5, fontsize=20,bg_color='gray').set_duration(5).set_pos('bottom').set_start(6)

image1 = ImageClip(r'C:\Users\Administrator\Desktop\logo.png').set_duration(5).set_pos('bottom').set_start(11)

vidio_path = 'C:/Users/Administrator/Desktop/抗日战争最强卧底 军统特工76号间谍暗战 比电影色戒更精彩.mp4'

vidio = VideoFileClip(vidio_path).subclip(0,5).set_duration(5).set_start(1)

print(type(vidio))

cvc = CompositeVideoClip([vidio,txtClip,txtClip2,image1],size=(1920,1080))

cvc.write_videofile('c:/Users/Administrator/Desktop/test.mp4',fps=24)

错误信息如下

raceback (most recent call last):
  File "c:/Users/Administrator/Desktop/视频添加字幕python.py", line 21, in <module>
    cvc.write_videofile('c:/Users/Administrator/Desktop/test.mp4',fps=24)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-51>", line 2, in write_videofile
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-50>", line 2, in write_videofile
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 137, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-49>", line 2, in write_videofile
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 317, in write_videofile
    logger=logger)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-73>", line 2, in write_audiofile
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 209, in write_audiofile
    logger=logger)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-70>", line 2, in ffmpeg_audiowrite
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py", line 169, in ffmpeg_audiowrite
    logger=logger):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 85, in iter_chunks
    fps=fps, buffersize=chunksize)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-72>", line 2, in to_soundarray
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 126, in to_soundarray
    snd_array = self.get_frame(tt)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-10>", line 2, in get_frame
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 296, in make_frame
    for c, part in zip(self.clips, played_parts)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 297, in <listcomp>
    if (part is not False)]
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-10>", line 2, in get_frame
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\Clip.py", line 138, in <lambda>
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\Clip.py", line 190, in <lambda>
    return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-10>", line 2, in get_frame
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 78, in <lambda>
    self.make_frame = lambda t: self.reader.get_frame(t)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\io\readers.py", line 184, in get_frame
    self.buffer_around(fr_max)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\io\readers.py", line 238, in buffer_around
    array = self.read_chunk(chunksize)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\moviepy\audio\io\readers.py", line 112, in read_chunk
    s = self.proc.stdout.read(L)
AttributeError: 'NoneType' object has no attribute 'stdout'

最后经过测试,发现视频的set_duration设置为4就可以,超过4就报错,,神奇的存在.

from moviepy.editor import *

#set_duration 持续时间;set_start开始显示字幕时间

txtClip = TextClip('halllo,哈喽 中国!',color='white', font='C:/Windows/Fonts/simhei.ttf',
                   kerning = 5, fontsize=60,bg_color='gray').set_duration(4).set_start(1).set_pos('bottom')

image1 = ImageClip(r'C:\Users\Administrator\Desktop\logo.png').set_duration(4).set_pos('bottom').set_start(1)

vidio_path = 'C:/Users/Administrator/Desktop/抗日战争最强卧底 军统特工76号间谍暗战 比电影色戒更精彩.mp4'

vidio = VideoFileClip(vidio_path).subclip(0,10).set_duration(4).set_start(2)
cvc = CompositeVideoClip([vidio,txtClip],size=(1920,1080))

cvc.write_videofile('c:/Users/Administrator/Desktop/test1.mp4')

最后运行成功,返回

Moviepy - Building video c:/Users/Administrator/Desktop/test1.mp4.
MoviePy - Writing audio in test1TEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video c:/Users/Administrator/Desktop/test1.mp4

Moviepy - Done !
Moviepy - video ready c:/Users/Administrator/Desktop/test1.mp4

3个回答

遇到类似问题。英文网页上说是bug,回退moviepy版本1.0.1到1.0.0就好了。的确如此。

java应该从何学起?java应该从何学起?

我也遇到在视频上加文字及图片,超过4.5秒的音频就报错,不知到和这里的4秒是否一个问题?网上说moviepy将版本可以,但一定维持新版本,该如何做呢?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问