Roman
Roman

Reputation: 131268

Why does moviepy complain about bitrate while generating audiofile?

I have just try to use moviepy library for the first time. Generation of movies from numpy arrays was really simple, intuitive and worked out of box. This is what I tried:

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)

Then I wanted to use moviepy to generate sound. In theory it should work in a very similar way. Here is what I tried:

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')

However, I got an error message:

[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.

Does anybody know what this error means and how this problem can be resolved?

Upvotes: 2

Views: 5240

Answers (2)

Tom Burrows
Tom Burrows

Reputation: 2365

Obviously Zulko's solution is correct, but I the answer to the actual question, 'Why does moviepy complain about the bitrate?' is that the line The audio/video export failed, possily because the bitrate you specified was two high or too low for the video codec. is a generic line printed after most failed export attempts. It is rarely the actual solution.

If you think it might be, call clip.write_videofile("output.mp4", bitrate=500) and set bitrate to whatever you want.

Upvotes: 0

Zulko
Zulko

Reputation: 3780

You cannot write a soundfile with a mp4 extension. Instead, use ".mp3", ".wav", ".ogg", etc.

Upvotes: 4

Related Questions