当前位置: 首页 > news >正文

Python-Opus——安装编解码库opus

安装环境

  • 安装环境:
sudo apt-get update
sudo apt-get install build-essential
# pyaudio
sudo apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev
# python opuslib
sudo apt-get install libopus-dev
  • 安装python包:
pip install opuslib pyaudio wave numpy

运行

import opuslib
import opuslib.api.encoder
import opuslib.api.decoder
import pyaudio
import wave
import struct
import numpy as np
import time #导入库filename = "/root/ghz/opus_workspace/example_recording.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 = filename[:-4]+"_output"+filename[-4:]
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:breakencoutput = enc.encode(pcm,320)#开始编码,数据保存在encoutput中decoutput = dec.decode(encoutput,320)#开始解码,数据保存在decoutput中wave_write.writeframes(decoutput)
print("end.")

http://www.mrgr.cn/news/27395.html

相关文章:

  • 【Python刷题】Atcoder Beginner Contest 371
  • 华为OD机试 - 推荐多样性(Python/JS/C/C++ 2024 E卷 100分)
  • 指针与函数传递
  • Cisco Wireless WLC 5520 HA config and show commands
  • C++——⼆叉搜索树
  • 【吊打面试官系列-Redis面试题】使用过 Redis 分布式锁么,它是什么回事?
  • SQLite的入门级项目学习记录(三)
  • DOM编程
  • 线性规划------ + 案例 + Python源码求解(见文中)
  • 【JavaScript】数据结构之树
  • [atcoder abc 371d]1D Country
  • bat批量修改文件名
  • MES系统:智能工厂与数字化改造的关键引擎
  • 【devops】devops-git之github使用
  • Spring Boot与gRPC的完美融合:构建高效用户服务与订单服务通信
  • matlab fid = fopen(file_nav,‘rt‘);语句解释
  • 在Windows 10上安装Python 3并设置本地编程环境的方法
  • 【RabbitMQ 项目】服务端数据管理模块之交换机管理
  • Docker技术深度解析与实践应用
  • 如何设置word页码从指定页开始