import opuslib
import opuslib.api.encoder
import opuslib.api.decoder
import pyaudio
import wave
import struct
import numpy as np
import time #导入库
filename = "test2.wav"
wave_read = wave.open(filename, "rb")
wav_channels = wave_read.getnchannels()
wav_samplewidth = wave_read.getsampwidth()
wav_samplerate = wave_read.getframerate()
wav_frames = wave_read.getnframes()
output_filename = "output-"+filename
wave_write = wave.open(output_filename, "wb")
print("Writing wav into file '{:s}'".format(output_filename))
wave_write.setnchannels(1)
wave_write.setframerate(16000)
wave_write.setsampwidth(2)
enc = opuslib.Encoder(fs = wav_samplerate,channels = 1 ,application = "audio")#创建编码器
enc._set_bandwidth(1105)
enc._set_lsb_depth(16)
enc._set_complexity(10)
enc._set_bitrate(wav_samplerate*2)
enc._set_vbr(1)
enc._set_force_channels(1)
enc._set_packet_loss_perc(0)
dec = opuslib.Decoder(fs = wav_samplerate,channels = 1 )#创建解码器
wav_frame_cnt = 0
# Calculate the desired frame size (in samples per channel)
desired_frame_duration = 20/1000 # milliseconds
desired_frame_size = int(desired_frame_duration * 16000)
while True:
pcm = wave_read.readframes(desired_frame_size)
if len(pcm) == 0:
break
encoutput = enc.encode(pcm,320)#开始编码,数据保存在encoutput中
decoutput = dec.decode(encoutput,320)#开始解码,数据保存在decoutput中
wave_write.writeframes(decoutput)
print("end.")
基于python下opuslib的下opus编解码实例解析
最新推荐文章于 2024-05-14 09:32:19 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)