2

我只是moviepy第一次尝试使用库。从数组生成电影numpy非常简单、直观且开箱即用。这是我尝试过的:

from moviepy.editor import VideoClip
import numpy as np

def make_frame(t):

    val = int(255.0*(t/3.0))

    ls = []
    for height in range(100):
        row = []
        for width in range(300):
            row.append([val,0,0])
        ls.append(row)
    frame = np.array(ls)
    return frame

animation = VideoClip(make_frame, duration = 3)

animation.write_gif('first_try.gif', fps=24)
animation.write_videofile('first_try.mp4', fps=24)

然后我想用它moviepy来产生声音。从理论上讲,它应该以非常相似的方式工作。这是我尝试过的:

from moviepy.editor import AudioClip
import numpy as np

make_frame = lambda t : 2*[ np.sin(404 * 2 * np.pi * t) ]
clip = AudioClip(make_frame, duration=5)

clip.write_audiofile('sound.mp4')

但是,我收到一条错误消息:

[MoviePy] Writing audio in sound.mp4
|----------| 0/111   0% [elapsed: 00:00 left: ?, ? iters/sec]Traceback (most recent call last):
  File "sound.py", line 9, in <module>
    clip.write_audiofile('sound.mp4')
  File "<string>", line 2, in write_audiofile
  File "/usr/local/lib/python2.7/dist-packages/moviepy/decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/audio/AudioClip.py", line 204, in write_audiofile
    verbose=verbose, ffmpeg_params=ffmpeg_params)
  File "<string>", line 2, in ffmpeg_audiowrite
  File "/usr/local/lib/python2.7/dist-packages/moviepy/decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/audio/io/ffmpeg_audiowriter.py", line 162, in ffmpeg_audiowrite
    writer.write_frames(chunk)
  File "/usr/local/lib/python2.7/dist-packages/moviepy/audio/io/ffmpeg_audiowriter.py", line 122, in write_frames
    raise IOError(error)
IOError: [Errno 32] Broken pipe

MoviePy error: FFMPEG encountered the following error while writing file sound.mp4:

Invalid encoder type 'libx264'


The audio export failed, possily because the bitrate you specified was two high or too low for the video codec.

有谁知道这个错误是什么意思以及如何解决这个问题?

4

2 回答 2

3

您不能编写带有 mp4 扩展名的声音文件。相反,请使用“.mp3”、“.wav”、“.ogg”等。

于 2015-02-21T06:42:27.430 回答
0

显然 Zulko 的解决方案是正确的,但我回答了实际问题,“为什么 moviepy 抱怨比特率?” 是该行The audio/video export failed, possily because the bitrate you specified was two high or too low for the video codec.是在大多数失败的导出尝试后打印的通用行。它很少是实际的解决方案。

如果您认为可能是这样,请调用clip.write_videofile("output.mp4", bitrate=500)并将比特率设置为您想要的任何值。

于 2017-01-11T12:08:12.573 回答