Moviepy包应用failed to read the first frame of video问题解决
Moviepy是Python下的第三方视讯处理包。通过呼叫ffmpeg,可以利用程式解决许多视讯处理的问题。 我们经常利用moviepy来获取视讯帧影象,以便后续为影象识别处理等应用服务。培训中,我们经常遇到的一个问题就是以下的报错:
OSError: MoviePy error: failed to read the first frame of video file test.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.
这个错误资讯会误导大家解决问题的方向。 程式发生这类问题,既与视讯的第一帧没有任何关系,也与 a deprecated version of FFMPEG无任何关系。如果按照错误提示的方向去解决问题,往往无功而返。 实际上,这个错误大多数情况下产生于程式想撷取的视讯帧不存在而导致的。如程式中想获取35秒时刻的视讯帧,但视讯总长仅20秒,显然是获取不到的,moviepy会报错的,但容易被错误资讯误导。因此在程式应用中,首先获取需要处理的视讯时长就很重要,在视讯时帧内处理视讯。获取视讯时长参考程式码如下:
video = VideoFileClip(“test.mp4”) # 开启视讯
time_length = int(video.duration) # 读取时长s
