使用Python读取音频数据
frames = wav_file.readframes(n_frames) # Read n_frames new frames.
assert len(frames) == sample_width * n_frames

使用Python和MPEG转换音频文件

from subprocess import check_call
ok = check_call(['ffmpeg','-i','input.mp3','output.wav'])
if ok:
with open('output.wav', 'rb') as f:
wav_file = f.read()
note:
使用Python复制到新的WAV文件
with wave.open("path_to_new_wav_file.wav", "wb") as wav_file: # Open WAV file in write-only
mode.
Python 音频模块Audio
使用Python写音频数据
params = (n_channels, sample_width, framerate, n_frames, comp_type, comp_name)
wav_file.setparams(params)
wav_file.writeframes(frames)

使用Python播放WAV文件

Windows环境

import winsound
winsound.PlaySound("path_to_wav_file.wav", winsound.SND_FILENAME)

支持单声道/立体声
不支持压缩/解压缩

import wave
with wave.open("path_to_wav_file.wav", "rb") as wav_file: # Open WAV file in read-only mode.
使用Python获取音频基本信息
n_channels = wav_file.getnchannels() # Number of channels. (1=Mono, 2=Stereo).
sample_width = wav_file.getsampwidth() # Sample width in bytes.
framerate = wav_file.getframerate() # Frame rate.
n_frames = wav_file.getnframes() # Number of frames.
comp_type = wav_file.getcomptype() # Compression type (only supports "NONE").
comp_name = wav_file.getcompname() # Compression name.
日期:2020-06-02 22:16:02 来源:oir作者:oir