使用MoviePy的time_mirror反转视频报错求助(MacOS环境)
使用MoviePy的vfx.time_mirror反转视频时读取帧失败的问题
问题详情
在MacOS系统中,尝试通过MoviePy的vfx.time_mirror方法反转视频,代码如下:
from moviepy.editor import * import moviepy.video.fx.all as vfx # Load the video video = VideoFileClip("/videos/uncut/videogames/other/001.mp4") popreversed_video = video.fx(vfx.time_mirror) # Save the reversed video reversed_video.write_videofile("videos/cut/videogames/other/001_rev.mp4") print("done")
执行后反复出现读取帧失败错误:
MoviePy error: failed to read the first frame of video file /videos/uncut/videogames/other/001.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. File "/Shorts Project/codefiles/video_play_and_rewinde_test_001.py", line 11, in <module> popreversed_video = video.fx(vfx.time_mirror) OSError: MoviePy error: failed to read the first frame of video file /videos/uncut/videogames/other/001.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.
已执行过以下操作,但问题未解决:
pip install moviepypip uninstall moviepypip install ffmpegpip install ffmpeg-pythonbrew install ffmpegpip uninstall ffmpegpip install --upgrade moviepy
所有工具均为最新版本。
解决步骤
校验视频路径与权限
- 代码中视频路径为绝对路径
/videos/uncut/videogames/other/001.mp4,在终端执行ls /videos/uncut/videogames/other/001.mp4,确认文件存在且当前用户有读取权限。 - 如果是相对路径,需去掉开头斜杠,改为
videos/uncut/videogames/other/001.mp4,确保路径相对于脚本运行目录有效。
- 代码中视频路径为绝对路径
验证视频文件完整性
- 用QuickTime等播放器打开原视频,确认能正常播放。若文件损坏,重新获取或修复视频。
- 在终端执行
ffmpeg -i /videos/uncut/videogames/other/001.mp4,若FFmpeg能输出完整的视频参数,说明文件正常;若报错,需处理文件编码或损坏问题。
指定MoviePy使用的FFmpeg二进制文件
- MacOS通过brew安装的FFmpeg路径通常为
/usr/local/bin/ffmpeg,先在终端执行which ffmpeg确认路径。 - 在代码开头添加以下代码,强制MoviePy使用brew安装的FFmpeg:
import os os.environ["FFMPEG_BINARY"] = "/usr/local/bin/ffmpeg" # 替换为which ffmpeg的输出结果 - 执行
python -c "from moviepy.config import get_setting; print(get_setting('FFMPEG_BINARY'))",确认输出为brew安装的FFmpeg路径。
- MacOS通过brew安装的FFmpeg路径通常为
修正代码变量错误
- 代码中定义了
popreversed_video,但保存时调用的是reversed_video,会触发NameError,先修正变量名:reversed_video = video.fx(vfx.time_mirror) reversed_video.write_videofile("videos/cut/videogames/other/001_rev.mp4")
- 代码中定义了
尝试降级MoviePy版本
- 最新版本可能存在兼容性问题,执行
pip install moviepy==2.0.0安装指定版本后再测试代码。
- 最新版本可能存在兼容性问题,执行
内容的提问来源于stack exchange,提问作者stelidakis
相关产品推荐
相关产品推荐

