pydub、ffmpeg 音频文件声道选择转换、采样率更改

快速查看音频通道数和每个通道能力判断具体哪个通道说话;一般能量大的那个算是说话

import wave
from pydub import AudioSegment
import numpy as np

def read_wav_file(file_path):
    with wave.open(file_path, 'rb') as wav_file:
        params = wav_file.getparams()
        num_channels = params.nchannels
        sample_width = params.sampwidth
        frame_rate = params.framerate
        num_frames = params.nframes
        print(f"Number of channels: {num_channels}")
        print(f"Sample width: {sample_width}")
        print(f"Frame rate: {frame_rate}")
        print(f"Number of frames: {num_frames}")
        
        frames = wav_file.readframes(num_frames)
        audio_data = np.frombuffer(frames, dtype=np.int16)
        if num_channels > 1:
            audio_data = audio_data.reshape(-1, num_channels)
        return audio_data, frame_rate, num_channels

def analyze_channels(audio_data, frame_rate, num_channels):
    for channel in range(num_channels):
        channel_data = audio_data[:, channel] if num_channels > 1 else audio_data
        # 计算通道的能量
        energy = np.sum(np.abs(channel_data))
        print(f"Channel {channel} energy: {energy}")
        # 你可以在这里添加更多的分析逻辑,比如使用语音活动检测(VAD)来判断说话声

if __name__ == "__main__":
    file_path = r"E:\allchat\output.wav"
    audio_data, frame_rate, num_channels = read_wav_file(file_path)
    analyze_channels(audio_data, frame_rate, num_channels)

这里 channel0 的声音算说话的
在这里插入图片描述

1、转换mono单声道,选择人声的那个通道

mp3格式

from pydub import AudioSegment

def extract_and_save_channel(input_file, output_file, channel_index):
    # 读取 MP3 文件
    audio = AudioSegment.from_mp3(input_file)
    
    # 提取特定通道
    if audio.channels > 1:
        channel_data = audio.split_to_mono()[channel_index]
    else:
        channel_data = audio
    
    # 保存提取的通道为新的 MP3 文件
    channel_data.export(output_file, format="mp3")

if __name__ == "__main__":
    input_file = "your_audio_file.mp3"
    output_file = "channel_0.mp3"
    channel_index = 0  # 选择 Channel 0
    
    extract_and_save_channel(input_file, output_file, channel_index)



wav格式

from pydub import AudioSegment

# 加载WAV文件
wav_file_path = r"E:\allchat\output_16000.wav"
audio_segment = AudioSegment.from_wav(wav_file_path)

# 提取Channel 0
if audio_segment.channels > 1:
    channel_0 = audio_segment.split_to_mono()[0]
else:
    channel_0 = audio_segment

# 导出为单声道WAV文件
mono_wav_file_path = r"E:\allchat\output_16000_channel_0.wav"
channel_0.export(mono_wav_file_path, format="wav")

在这里插入图片描述

2、采样率更改为16000

from pydub import AudioSegment

def resample_wav_with_pydub(input_file, output_file, new_rate):
    # 读取原始WAV文件
    audio = AudioSegment.from_wav(input_file)
    
    # 设置新的采样率
    audio = audio.set_frame_rate(new_rate)
    
    # 导出重采样后的WAV文件
    audio.export(output_file, format='wav')

# 使用示例
resample_wav_with_pydub('input.wav', 'output_16000.wav', 16000)

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 20:24:06       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 20:24:06       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 20:24:06       57 阅读
  4. Python语言-面向对象

    2024-07-09 20:24:06       68 阅读

热门阅读

  1. HTTP Client

    2024-07-09 20:24:06       32 阅读
  2. `mysql`常用的的CIL命令行工具

    2024-07-09 20:24:06       45 阅读
  3. 在 Windows PowerShell 中模拟 Unix/Linux 的 touch 命令

    2024-07-09 20:24:06       23 阅读
  4. LVS+keepalived群集

    2024-07-09 20:24:06       28 阅读
  5. qt udp通讯应用举例

    2024-07-09 20:24:06       29 阅读
  6. Vuetify3:v-data-table增加下拉筛选

    2024-07-09 20:24:06       17 阅读
  7. 通过升级tomcat完美解决服务器的tomcat漏洞

    2024-07-09 20:24:06       24 阅读
  8. unity 使用UnityWebRequest从服务器下载

    2024-07-09 20:24:06       20 阅读
  9. el-table 树状表格查询符合条件的数据

    2024-07-09 20:24:06       24 阅读
  10. for in和for of对比

    2024-07-09 20:24:06       23 阅读