Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.1k stars 1.51k forks source link

Misleading error: OSError: MoviePy error: failed to read the first frame of video file #2021

Open networks1 opened 11 months ago

networks1 commented 11 months ago

IDK if this counts as a bug exactly, but that was the closest category. I just spend a lot of time trying to debug an error I got when running this code (simplified version, minus subs):

clip = VideoFileClip('infile.mp4')
outfile = 'out.mp4'
frames = []
time_points = [30, 60, 90, 120, 150]
for time in time_points:
    frame = clip1.get_frame(time)
    frames.append(Image.fromarray(frame))    
merged_image = merge_and_resize_frames(frames, 200)
save_image(merged_image, name + '=2000' + tag + ".jpg")
clip1.write_videofile(outfile,codec='libx264')

This would throw an error with the end of the traceback saying:

OSError: MoviePy error: failed to read the first frame of video file infile.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.

Naturally, based on this message I thought the problem was in reading the file. So I set off checking it on different players (it played) and ensuring that it was not using a deprecated version of FFMPEG (it wasn't), making sure there wasn't a missing or bad codec (there wasn't).

Then I stepped through the code using a debugger, and was surprised when it passed the first line. It threw the error on the last step of the for iterator. It happened because I was trying to get the frame at sec 150 when the clip was less than 150 sec, not because it couldn't read the first frame of the file.

I found this error quite a bit when researching it. You would probably save people a lot of time/frustration by having the error say I'm trying to extract a frame beyond the end of the clip, or at the least adding to the existing error message that this situation can also cause the error.

Steps to Reproduce the Problem

Use method get_frame(time) where time > length of the clip on which it is run

Specifications

anyadbuzi69 commented 9 months ago

Same here