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

如何在Python中批量将.opus格式音频转换为.wav格式?

批量将Opus音频转换为Wav格式(Python/Jupyter Notebook实现)

前置依赖

首先需要安装必要的工具和Python库:

  1. 安装FFmpeg:
    • Linux环境:在Jupyter中执行 !apt-get update && !apt-get install -y ffmpeg
    • Windows环境:下载FFmpeg并添加到系统PATH,或用conda安装 !conda install -c conda-forge ffmpeg
  2. 安装Python库:
!pip install pydub ffmpeg-python

方案1:使用pydub实现批量转换

pydub 封装了FFmpeg,API简洁易用,适合快速开发:

import os
from pydub import AudioSegment

def batch_convert_opus_to_wav(input_root, output_root):
    # 遍历输入目录下所有子目录和文件
    for dirpath, _, filenames in os.walk(input_root):
        # 构建与原目录结构一致的输出目录
        relative_path = os.path.relpath(dirpath, input_root)
        output_dir = os.path.join(output_root, relative_path)
        os.makedirs(output_dir, exist_ok=True)
        
        # 处理每个.opus文件
        for filename in filenames:
            if filename.lower().endswith('.opus'):
                input_file = os.path.join(dirpath, filename)
                output_file = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}.wav")
                
                try:
                    # 读取opus文件并导出为wav
                    audio = AudioSegment.from_file(input_file, format='opus')
                    audio.export(output_file, format='wav')
                    print(f"✅ {input_file} -> {output_file}")
                except Exception as e:
                    print(f"❌ 转换失败 {input_file}: {str(e)}")

# 替换为你的实际目录路径
input_dir = "/your/input/opus/directory"
output_dir = "/your/output/wav/directory"

# 执行转换
batch_convert_opus_to_wav(input_dir, output_dir)

方案2:使用ffmpeg-python直接调用FFmpeg

如果需要精细控制转换参数(比如调整采样率、比特率),可以用ffmpeg-python直接操作FFmpeg:

import os
import ffmpeg

def batch_convert_with_ffmpeg(input_root, output_root, sample_rate=16000):
    for dirpath, _, filenames in os.walk(input_root):
        relative_path = os.path.relpath(dirpath, input_root)
        output_dir = os.path.join(output_root, relative_path)
        os.makedirs(output_dir, exist_ok=True)
        
        for filename in filenames:
            if filename.lower().endswith('.opus'):
                input_file = os.path.join(dirpath, filename)
                output_file = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}.wav")
                
                try:
                    # 自定义转换参数,这里设置采样率为16kHz(适配TensorFlow音频处理)
                    ffmpeg.input(input_file)\
                          .output(output_file, ar=sample_rate)\
                          .run(quiet=True, overwrite_output=True)
                    print(f"✅ {input_file} -> {output_file}")
                except Exception as e:
                    print(f"❌ 转换失败 {input_file}: {str(e)}")

# 替换为你的实际目录路径,可按需调整采样率
input_dir = "/your/input/opus/directory"
output_dir = "/your/output/wav/directory"

# 执行转换
batch_convert_with_ffmpeg(input_dir, output_dir, sample_rate=16000)

注意事项

  • 如果系统找不到FFmpeg,可手动指定路径:
    • 对于pydub:AudioSegment.converter = "/path/to/ffmpeg/executable"
    • 对于ffmpeg-python:ffmpeg.EXECUTABLE = "/path/to/ffmpeg/executable"
  • 转换前建议先测试单个文件,确保环境配置正确
  • 若文件数量极大,可添加多线程加速(比如用concurrent.futures)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:35:55