You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MoviePy读取自生成视频首帧失败的技术求助

问题:MoviePy读取自生成视频时无法提取帧

我尝试用MoviePy提取并保存.mp4视频的帧,简化代码如下:

from moviepy.editor import *
clip = VideoFileClip('conditiontest1.mpg')
clip.save_frame('frame1.png', 20)

运行后报错:

Traceback (most recent call last):
File"C:\Users\boss\Documents\DVDFab\Video Enhancer AI\join.py", line 23, in
clip = VideoFileClip(d + f + '.mp4')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in init
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 73, in init
self.lastread = self.read_frame()
^^^^^^^^^^^^^^^^^
File "C:\Python311\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 conditiontest1.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.

视频能在VLC正常播放,我已经安装了最新版FFmpeg,也试过用完整路径读取的方法,但都无效。而且这个问题只在处理MoviePy自己生成的视频时出现,生成视频的代码如下:

from moviepy.editor import *
clip = VideoFileClip('conditiontest.mpg4')
clip1 = clip.subclip(10,60)
clip1.write_videofile('conditiontest1.mpg4',codec='libx264')

解决思路

  • 统一视频后缀:生成和读取视频时都用标准的.mp4后缀,避免MoviePy对非标准后缀(比如.mpg4)识别异常。
  • 指定音频编码:生成视频时添加audio_codec='aac'参数,确保音频流兼容,示例代码:
    clip1.write_videofile('conditiontest1.mp4', codec='libx264', audio_codec='aac')
    
  • 强制指定FFmpeg路径:在代码开头设置FFmpeg的绝对路径,确保调用的是你安装的最新版本,示例:
    from moviepy.config import change_settings
    change_settings({"FFMPEG_BINARY": r"C:\你的FFmpeg路径\ffmpeg.exe"})
    from moviepy.editor import *
    
  • 添加编码预设:生成视频时加上preset='medium'参数,避免快速编码导致视频结构不兼容,示例:
    clip1.write_videofile('conditiontest1.mp4', codec='libx264', preset='medium')
    
  • 用FFmpeg转码修复:如果上述方法都无效,先用FFmpeg重新转码生成的视频,再用MoviePy读取,命令:
    ffmpeg -i conditiontest1.mp4 -c:v libx264 -c:a aac -y fixed_conditiontest1.mp4
    

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:40:24