Fork me on GitHub

一 opuslib
这个是纯粹的opus封装,要比ogg的那个更底层,ogg的那个封装的太严了。很多业务不方便开展。
二 实例解析:
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.")

三 数据分析
A 这个参数很详细,可以个性化配置。
B 最新的兼容性比较好了,安装比较方便。
posted on 2023-06-09 15:45  虚生  阅读(444)  评论(0编辑  收藏  举报