pythoncodecmoviepy

ValueError: MoviePy couldn't find the codec associated with the filename. Provide the 'codec' parameter in write_videofile


Why is it throwing this error on some files, and for other files it doesn't ?

The files are all .mp4 videos.

'ValueError: MoviePy couldn't find the codec associated with the filename. Provide the 'codec' parameter in write_videofile.'

The code, with comments:

    # The videos on which we will work on (intro & a video)
    intro = VideoFileClip('./videos/intro.mp4')
    original = VideoFileClip('./videos/original_video.mp4')


    # We cut 2s and crop the original video
    original.cutout(0, 2)
    original = vfx.crop(original, x1=10)


    # Adding a watermark on top of the original video
    watermark = (ImageClip("./images/watermark.png")
                    .set_duration(original.duration)
                    .set_position(("center","bottom"))
                    .resize(width=intro.w))
    watermaked = CompositeVideoClip([original, watermark], size=original.size)


    # Our final video: Intro + Waterkarmed, and output it.
    final_render = concatenate_videoclips([intro, watermaked], method='compose')
    final_render.write_videofile('./videos/output_video.mp4', fps=30, threads=1)

The error stack:

  final_render.write_videofile('./videos/output_video.mp4', fps=30, threads=1)

  File "<decorator-gen-51>", line 2, in write_videofile
  File "/usr/lib/python2.7/site-packages/moviepy/decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "<decorator-gen-50>", line 2, in write_videofile
  File "/usr/lib/python2.7/site-packages/moviepy/decorators.py", line 137, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "<decorator-gen-49>", line 2, in write_videofile
  File "/usr/lib/python2.7/site-packages/moviepy/decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "/usr/lib/python2.7/site-packages/moviepy/video/VideoClip.py", line 288, in write_videofile
    raise ValueError("MoviePy couldn't find the codec associated "
ValueError: MoviePy couldn't find the codec associated with the filename. Provide the 'codec' parameter in write_videofile.

Solution

  • You need to put codec in manually using the codec argument.

    The code should look like this:

    final_render.write_videofile('./videos/output_video.mp4', fps=30, threads=1, codec="libx264")
    

    Codec changes according to video file extension:

    Extension Codec
    mp4 libx264
    ogv libtheora
    webm libvpx
    ogg libvorbis
    mp3 pcm_s16le
    wav libvorbis
    m4a libfdk_aac