基于音频数据创建点击机器人的Librosa报错及解决方案咨询
问题描述
我是编程新手,目标是做一个机器人——当检测到音频文件中某频率的分贝超过特定阈值时自动点击。目前仅完成librosa导入,运行代码时出现报错无法解决。
我的代码
import librosa import librosa.display audio_file = ('C:/Users/amber/Desktop/test/actualtest1.mp3') y, sr = librosa.load(audio_file)
报错信息
C:\Users\amber\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/amber/PycharmProjects/pythonProject2/test.py C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\util\decorators.py:88: UserWarning: PySoundFile failed. Trying audioread instead. return f(*args, **kwargs) Traceback (most recent call last): File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\core\audio.py", line 164, in load y, sr_native = __soundfile_load(path, offset, duration, dtype) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\core\audio.py", line 195, in __soundfile_load context = sf.SoundFile(path) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\soundfile.py", line 629, in __init__ self._file = self._open(file, mode_int, closefd) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\soundfile.py", line 1183, in _open _error_check(_snd.sf_error(file_ptr), File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\soundfile.py", line 1357, in _error_check raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace')) RuntimeError: Error opening 'C:/Users/amber/Desktop/test/actualtest1.mp3': File contains data in an unknown format. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\amber\PycharmProjects\pythonProject2\test.py", line 4, in <module> y, sr = librosa.load(audio_file) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\util\decorators.py", line 88, in inner_f return f(*args, **kwargs) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\core\audio.py", line 170, in load y, sr_native = __audioread_load(path, offset, duration, dtype) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\librosa\core\audio.py", line 226, in __audioread_load reader = audioread.audio_open(path) File "C:\Users\amber\PycharmProjects\pythonProject2\venv\lib\site-packages\audioread\__init__.py", line 132, in audio_open raise NoBackendError() audioread.exceptions.NoBackendError Process finished with exit code 1
解决方案
一、修复librosa加载MP3的报错
这个报错核心是缺少MP3解码后端:librosa依赖的PySoundFile不支持MP3格式,而audioread也找不到可用的解码工具。按以下步骤解决:
安装并配置ffmpeg
下载ffmpeg预编译包,解压后将bin目录路径添加到系统PATH环境变量。验证方式:打开命令提示符输入ffmpeg -version,能正常输出版本信息即为配置成功。安装必要依赖包
在虚拟环境中执行:pip install audioread ffmpeg-python备用方案:转换音频格式
如果上述操作后仍报错,可先将MP3转成WAV格式(用ffmpeg或格式转换工具),WAV为无压缩格式,librosa兼容性更好。转换命令:ffmpeg -i actualtest1.mp3 actualtest1.wav之后修改代码中的音频路径为WAV文件即可。
二、实现目标的替代方案
如果不想在librosa上耗时间,可尝试这些更直接的方法:
方案1:实时监听麦克风+自动点击(适合实时场景)
用pyaudio实时获取麦克风输入,通过FFT检测目标频率,超过阈值时用pyautogui模拟点击:
import pyaudio import numpy as np import pyautogui # 配置参数 CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 TARGET_FREQ = 1000 # 要检测的目标频率(Hz) DB_THRESHOLD = 60 # 触发点击的分贝阈值 FREQ_STRENGTH_THRESHOLD = 0.1 # 目标频率的强度阈值 p = pyaudio.PyAudio() # 打开音频流 stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) def calculate_db(raw_data): # 计算当前音频的分贝值 audio_data = np.frombuffer(raw_data, dtype=np.int16) / 32768.0 rms = np.sqrt(np.mean(np.square(audio_data))) return 20 * np.log10(rms) if rms > 0 else 0 def detect_target_frequency(raw_data, sample_rate, target_freq): # 通过FFT检测目标频率的强度 audio_data = np.frombuffer(raw_data, dtype=np.int16) / 32768.0 fft_result = np.fft.fft(audio_data) freq_bins = np.fft.fftfreq(len(fft_result), 1/sample_rate) # 找到最接近目标频率的索引 target_idx = np.argmin(np.abs(freq_bins - target_freq)) return np.abs(fft_result[target_idx]) try: print("开始监听...") while True: data = stream.read(CHUNK) current_db = calculate_db(data) freq_strength = detect_target_frequency(data, RATE, TARGET_FREQ) # 满足条件则触发点击 if current_db > DB_THRESHOLD and freq_strength > FREQ_STRENGTH_THRESHOLD: pyautogui.click() print("触发自动点击!") except KeyboardInterrupt: print("停止监听") # 清理资源 stream.stop_stream() stream.close() p.terminate()
安装依赖:pip install pyaudio numpy pyautogui,如果pyaudio安装失败,可下载对应Python版本的whl文件手动安装。
方案2:处理本地音频文件
先用ffmpeg提取音频频谱数据,再用Python分析判断:
- 提取频谱到文本文件:
ffmpeg -i actualtest1.mp3 -filter_complex "showspectrum=mode=combined:format=flat" -f null - 2> spectrum.txt - 编写Python脚本读取
spectrum.txt,定位目标频率对应的分贝值,超过阈值时调用pyautogui.click()。
内容的提问来源于stack exchange,提问作者synapstic
相关产品推荐
相关产品推荐

