python音频处理

# pcm转wav
import wave
pcm_path = r'D:\output-0.pcm'
with open(pcm_path, 'rb') as pcmfile:
    pcmdata = pcmfile.read()
with wave.open(pcm_path + '.wav', 'wb') as wavfile:
    wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE'))
    wavfile.writeframes(pcmdata)
# mp3转wav
from pydub import AudioSegment
def mp3_to_wav(mp3_path, wav_path):
    song = AudioSegment.from_mp3(mp3_path)
    song.export(wav_path, format="wav")
if __name__ == '__main__':
    mp3_to_wav('hup.mp3',"hup.wav")
# wav转pcm
# !/usr/bin/env python
# -*- encoding: utf-8 -*-
import numpy as np
def wav2pcm(wavfile, pcmfile, data_type=np.int16):
    f = open(wavfile, "rb")
    f.seek(0)
    f.read(44)
    data = np.fromfile(f, dtype=data_type)
    data.tofile(pcmfile)
wav2pcm("audio.wav", "record_bc.pcm")
# 音频切割
from pydub import AudioSegment
mp3 = AudioSegment.from_mp3(r"D:\1\Clangy.mp3")#打开mp3文件
first_1_seconds =mp3[:700]#1000=1秒,切前700ms
first_1_seconds.export("hup.mp3", format="mp3")

最后附上其他参考文章
1.大量地将wav转mp3

你可能感兴趣的:(python音频处理)