pythonmoviepyoserror

Exception has occurred: OSError MoviePy error: failed to read the first frame of video file ****. That might mean that the file is corrupted


WHAT I'M DOING:

Looping a video for a certain amount of time.

However, I'm getting the following error (The file is not corrupt):

Exception has occurred: OSError
MoviePy error: failed to read the first frame of video file Pexels Videos 1292738.mp4. That might mean that the file is corrupted.

at the commented line in my code:

chdir(r'C:\Users\jack_l\Downloads\makeAVideo\stock')
myStock = next(walk(r'C:\Users\jack_l\Downloads\makeAVideo\stock'), (None, None, []))[2]
stockFile = VideoFileClip(str(myStock[0]), target_resolution=(1080, 1920), audio=False)
stockFile = stockFile.loop(duration = 300)
stockFile = stockFile.set_fps(30)
chdir(r'C:\Users\jack_l\Downloads\makeAVideo')
stockFile.write_videofile('theVideo.mp4') # this line

Does anyone know what's going wrong? Any help is greatly appreciated, thank you.

THE FILE I'M USING:

https://drive.google.com/drive/folders/1n8ReLmPj8cIUi6og_GgmlRoumCMB7zIL?usp=sharing

FULL ERROR:

Traceback (most recent call last):
  File "c:\Users\jack_l\Downloads\makeVideos.py", line 40, in <module>
    stockFile.write_videofile('theVideo.mp4')
  File "<decorator-gen-55>", line 2, in write_videofile
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "<decorator-gen-54>", line 2, in write_videofile
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 135, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "<decorator-gen-53>", line 2, in write_videofile
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\VideoClip.py", line 300, 
in write_videofile
    ffmpeg_write_video(self, filename, fps, codec,
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 220, in ffmpeg_write_video
    for t,frame in clip.iter_frames(logger=logger, with_times=True,
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 472, in iter_frames
    frame = self.get_frame(t)
  File "<decorator-gen-11>", line 2, in get_frame
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 93, in get_frame    return self.make_frame(t)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 136, in <lambda>    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 187, in <lambda>    return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
  File "<decorator-gen-11>", line 2, in get_frame
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 93, in get_frame    return self.make_frame(t)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 113, in <lambda>
    self.make_frame = lambda t: self.reader.get_frame(t)
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 184, in get_frame
    result = self.read_frame()
  File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 133, in read_frame
    raise IOError(("MoviePy error: failed to read the first frame of "
OSError: MoviePy error: failed to read the first frame of video file Pexels Videos 1292738.mp4. That might mean that the file is corrupted. That may also mean that you are using a deprecated version of FFMPEG. On Ubuntu/Debian for instance 
the version in the repos is deprecated. Please update to a recent version from the website.

Solution

  • You change folder before write_videofile - and this can make problem.

    Code can be "lazy" and it may NOT read file when you define VideoFileClip() but when you want to write new file after changing directory - and it may try to read file from new place.

    You should use /full/path/to/Pexels Videos 1292738.mp4 instead of using chdir()


    Full working code:

    import os
    from moviepy.editor import *
    
    # --- info ---
    
    import moviepy
    print('moviepy:', moviepy.__version__)
    print('ffmpeg :', moviepy.config.FFMPEG_BINARY)
    
    # --- main ---
    
    input_dir  = r'C:\Users\jack_l\Downloads\makeAVideo\stock'
    output_dir = r'C:\Users\jack_l\Downloads\makeAVideo'
    
    #print('chdir:', input_dir)
    #os.chdir(input_dir)
    
    root, dirs, files = next(os.walk(input_dir), (None, None, []))
    #print(files)
    
    if files:
        #input_path  = os.path.join(root, files[0])
        input_path  = os.path.join(input_dir, files[0])
        output_path = os.path.join(output_dir, 'theVideo.mp4')
        
        print('input :', input_path)
        print('output:', output_path)
    
        stock_file = VideoFileClip(input_path, target_resolution=(1080, 1920), audio=False)
        stock_file = stock_file.loop(duration=300)
        stock_file = stock_file.set_fps(30)
    
        #print('chdir:', output_dir)
        #os.chdir(output_dir)
    
        stock_file.write_videofile(output_path)