使用MoviePy将MP4转MP3时遇FFmpeg找不到的问题求助
MacBook Air M1下MoviePy转MP4到MP3遇FFmpeg找不到的解决方法
问题重现
我使用MoviePy工具实现MP4转MP3,执行pip install moviepy安装成功后编写了如下代码:
# This code should convert a .mp4 file into a .mp3 file # This imports the moviepy package from moviepy.editor import * # here are the names of my files (I have subbed out actual files names) mp4_file = "file_name.mp4" mp3_file = "file_name.mp3" # Here is the the audio being stripped from the .mp4 file video_clip = VideoFileClip(mp4_file) audio_clip = video_clip.audio # this is writing the audio to a .mp3 file at the path that is specified. audio_clip.write_audiofile(mp3_file) # this closes the conversion code audio_clip.close() VideoClip.close()
运行后触发错误:
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
已尝试的操作:
- 通过代码指定FFmpeg路径(
/usr/bin/ffmpeg)无效 - 执行
pip install ffmpeg和brew install ffmpeg均显示安装成功,但在/usr/bin/ffmpeg或/usr/local/bin/ffmpeg路径下找不到程序
针对M1 Mac的解决方案
1. 确认FFmpeg的实际安装路径
M1芯片的Mac上,Homebrew默认安装目录是/opt/homebrew而非/usr/local,所以FFmpeg的路径通常是/opt/homebrew/bin/ffmpeg。你可以在终端执行以下命令获取准确路径:
which ffmpeg
2. 修正代码中的路径配置与错误
原代码存在两个问题:路径指向错误,以及关闭资源时调用了类方法而非实例方法。修正后的代码如下:
# 先配置FFmpeg路径,再导入moviepy from moviepy.config import change_settings # 替换为which ffmpeg输出的实际路径 change_settings({"FFMPEG_BINARY": "/opt/homebrew/bin/ffmpeg"}) from moviepy.editor import VideoFileClip mp4_file = "file_name.mp4" mp3_file = "file_name.mp3" video_clip = VideoFileClip(mp4_file) audio_clip = video_clip.audio audio_clip.write_audiofile(mp3_file) # 关闭实例而非类 audio_clip.close() video_clip.close()
3. 重新安装并验证FFmpeg
如果which ffmpeg无输出,说明FFmpeg未正确安装,重新执行以下命令:
brew uninstall ffmpeg brew install ffmpeg
安装完成后再次执行ffmpeg -version,若能输出版本信息则说明安装正常。
4. 全局配置环境变量(可选)
若不想每次在代码中指定路径,可配置全局环境变量:
- 打开终端,编辑zsh配置文件:
nano ~/.zshrc - 添加以下内容(替换为你的FFmpeg实际路径):
export IMAGEIO_FFMPEG_EXE="/opt/homebrew/bin/ffmpeg" - 保存并退出后,执行以下命令使配置生效:
source ~/.zshrc
内容的提问来源于stack exchange,提问作者Enspist
相关产品推荐
相关产品推荐

