Ubuntu下pyrubberband报错AttributeError: 'array.array'无ndim属性求解决
问题解决与替代安装方案
一、解决AttributeError: 'array.array' object has no attribute 'ndim'错误
错误原因是get_array_of_samples()返回的是Python标准库的array.array对象,而pyrubberband的函数要求传入numpy数组(ndim是numpy数组的专属属性)。解决步骤如下:
- 先安装numpy依赖:
pip install numpy
- 修改代码中调用
pyrb.time_stretch的参数,将array.array对象转为numpy数组,同时转换为浮点类型(pyrubberband要求输入float32格式的音频样本):
原代码片段:
transition_start = pyrb.time_stretch(combined_audio[:transition_duration].get_array_of_samples(), combined_audio[:transition_duration].frame_rate, end_speed - start_speed)
修改后:
import numpy as np audio_samples = np.array(combined_audio[:transition_duration].get_array_of_samples(), dtype=np.float32) transition_start = pyrb.time_stretch(audio_samples, combined_audio[:transition_duration].frame_rate, end_speed - start_speed)
二、rubberband-cli与pyrubberband的替代安装方法
rubberband-cli的其他安装方式
- 源码编译安装
先安装编译依赖:
sudo apt-get install build-essential libsamplerate0-dev libsndfile1-dev
然后克隆源码并编译:
git clone https://github.com/breakfastquay/rubberband.git cd rubberband ./configure make sudo make install
- conda安装(适用于conda环境)
conda install -c conda-forge rubberband
pyrubberband的其他安装方式
- pip直接安装(需确保系统已提前安装rubberband库)
pip install pyrubberband
- conda安装(自动处理所有依赖)
conda install -c conda-forge pyrubberband
内容的提问来源于stack exchange,提问作者Satya Dewangan
相关产品推荐
相关产品推荐

