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

Ubuntu WSL2环境下Torchaudio无法兼容Wav文件问题

问题

仓库在原生Ubuntu系统运行正常,但在配置完全相同的Ubuntu WSL2环境下,使用Torchaudio 2.2.1时出现错误:Torchaudio无法自动检测WAV音频文件类型,错误栈如下:

Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/concurrent/futures/process.py", line 246, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/home/ubuntu/brainmagick_abdhu/bm/dataset.py", line 381, in _preload
    recording.events()
  File "/home/ubuntu/brainmagick_abdhu/bm/studies/api.py", line 264, in events
    self._events = self._load_events()
  File "/home/ubuntu/brainmagick_abdhu/bm/studies/brennan2019.py", line 307, in _load_events
    events = _read_meta(file)
  File "/home/ubuntu/brainmagick_abdhu/bm/studies/brennan2019.py", line 167, in _read_meta
    events = events.event.create_blocks(groupby='sentence')
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/pandas/core/accessor.py", line 224, in __get__
    accessor_obj = self._accessor(obj)
  File "/home/ubuntu/brainmagick_abdhu/bm/events.py", line 549, in __init__
    self._frame = self.validate()
  File "/home/ubuntu/brainmagick_abdhu/bm/events.py", line 603, in validate
    return pd.DataFrame(self._frame.apply(self._validate_event, axis=1).tolist())
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/pandas/core/frame.py", line 10034, in apply
    return op.apply().__finalize__(self, method="apply")
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/pandas/core/apply.py", line 837, in apply
    return self.apply_standard()
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/pandas/core/apply.py", line 963, in apply_standard
    results, res_index = self.apply_series_generator()
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/pandas/core/apply.py", line 979, in apply_series_generator
    results[i] = self.func(v, *self.args, **self.kwargs)
  File "/home/ubuntu/brainmagick_abdhu/bm/events.py", line 586, in _validate_event
    event_obj = event_class.from_dict(event)
  File "/home/ubuntu/brainmagick_abdhu/bm/events.py", line 53, in from_dict
    return cls(**{k: v for k, v in row.items() if k in [f.name for f in fields(cls)]})
  File "<string>", line 9, in __init__
  File "/home/ubuntu/brainmagick_abdhu/bm/events.py", line 132, in __post_init__
    info = torchaudio.info(self.filepath)
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/torchaudio/_backend/utils.py", line 97, in info
    backend = dispatcher(uri, format, backend)
  File "/home/ubuntu/anaconda3/envs/p310env/lib/python3.10/site-packages/torchaudio/_backend/utils.py", line 53, in dispatcher
    raise RuntimeError(f"Couldn't find appropriate backend to handle uri {uri} and format {format}.")
RuntimeError: Couldn't find appropriate backend to handle uri /home/ubuntu/brainmagick_abdhu/data/brennan2019/download/audio/DownTheRabbitHoleFinal_SoundFile1.wav and format None.

原因分析

WSL2环境下,Torchaudio的后端自动检测逻辑可能失效,或者缺少底层音频处理依赖库(如libsndfile),导致无法识别WAV文件格式。

解决方案

1. 手动指定文件格式或后端

  • 直接修改代码中torchaudio.info的调用,手动指定WAV格式:
    info = torchaudio.info(self.filepath, format="wav")
    
  • 或者在项目启动前强制指定Torchaudio后端,避免自动检测失败:
    终端执行:
    export TORCHAUDIO_BACKEND="sox_io"
    
    或在Python代码开头添加:
    import torchaudio
    torchaudio.set_audio_backend("sox_io")
    

2. 安装缺失的系统依赖

WSL2默认可能缺少Torchaudio依赖的底层库,执行以下命令安装:

sudo apt update && sudo apt install -y libsndfile1 ffmpeg

之后重新安装Torchaudio确保依赖绑定正确:

pip uninstall -y torchaudio
pip install torchaudio==2.2.1 --force-reinstall

3. 验证后端状态

运行以下代码确认Torchaudio可用后端:

import torchaudio
print(torchaudio.list_audio_backends())

输出应包含sox_io或soundfile,若没有则说明依赖安装不完整,需重新检查步骤2。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 09:44:52