python 读取 mp3

import wave
import os
import numpy as np
from pydub import AudioSegment
import matplotlib.pylab as plt
%matplotlib inline

wav_file = 'now.wav'

song = AudioSegment.from_mp3('music.mp3')
song.export(wav_file , format="wav")

with wave.open(wav_file ,'rb') as f:
    params = f.getparams()
    print(params)
    nchannels, sampwidth, framerate, nframes = params[:4]
    nframes = int(nframes/80)
    strData = f.readframes(nframes)
    waveData = np.fromstring(strData,dtype=np.int32)
    waveData = waveData*1.0/(max(abs(waveData)))  # 幅值归一化
    # plot the wave
    time = np.arange(0,nframes)*(1.0 / framerate)
    plt.figure(figsize=(100,2))
    plt.plot(time,waveData)
    plt.xlabel("Time(s)")
    plt.ylabel("Amplitude")
    plt.title("Single channel wavedata")

_wave_params(nchannels=2, sampwidth=2, framerate=44100, nframes=14417280, comptype=‘NONE’, compname=‘not compressed’)
在这里插入图片描述

你可能感兴趣的:(#,编程语言)