一 opus
 
pyogg是一个非常不错的库,用这个做音频的编码和解码非常方便。
 
二 源码解析
 
import wave
from pyogg import OpusEncoder
from pyogg import OpusDecoder if __name__ == "__main__":
# Setup encoding
# ============== # Read a wav file to obtain PCM data
filename = "test2.wav"
wave_read = wave.open(filename, "rb")
print("Reading wav from file '{:s}'".format(filename)) # Extract the wav's specification
channels = wave_read.getnchannels()
print("Number of channels:", channels)
samples_per_second = wave_read.getframerate()
print("Sampling frequency:", samples_per_second)
bytes_per_sample = wave_read.getsampwidth() # Create an Opus encoder
opus_encoder = OpusEncoder()
opus_encoder.set_application("audio")
opus_encoder.set_sampling_frequency(samples_per_second)
opus_encoder.set_channels(channels) # Calculate the desired frame size (in samples per channel)
desired_frame_duration = 20/1000 # milliseconds
desired_frame_size = int(desired_frame_duration * samples_per_second) # Setup decoding
# ============== # Create an Opus decoder
opus_decoder = OpusDecoder()
opus_decoder.set_channels(channels)
opus_decoder.set_sampling_frequency(samples_per_second) # Open an output wav for the decoded PCM
output_filename = "output-"+filename
wave_write = wave.open(output_filename, "wb")
print("Writing wav into file '{:s}'".format(output_filename)) # Save the wav's specification
wave_write.setnchannels(channels)
wave_write.setframerate(samples_per_second)
wave_write.setsampwidth(bytes_per_sample) # Execute encode-decode
# ===================== # Loop through the wav file's PCM data and encode it as Opus
bytes_encoded = 0
while True:
# Get data from the wav file
pcm = wave_read.readframes(desired_frame_size) # Check if we've finished reading the wav file
if len(pcm) == 0:
break # Calculate the effective frame size from the number of bytes
# read
effective_frame_size = (
len(pcm) # bytes
// bytes_per_sample
// channels
) # Check if we've received enough data
if effective_frame_size < desired_frame_size:
# We haven't read a full frame from the wav file, so this
# is most likely a final partial frame before the end of
# the file. We'll pad the end of this frame with silence.
pcm += (
b"\x00"
* ((desired_frame_size - effective_frame_size)
* bytes_per_sample
* channels)
) # Encode the PCM data
encoded_packet = opus_encoder.encode(pcm)
bytes_encoded += len(encoded_packet) # At this stage we now have a buffer containing an
# Opus-encoded packet. This could be sent over UDP, for
# example, and then decoded with OpusDecoder. However it
# cannot really be saved to a file without wrapping it in the
# likes of an Ogg stream; for this see OggOpusWriter. # For this example, we will now immediately decode this
# encoded packet using OpusDecoder.
decoded_pcm = opus_decoder.decode(encoded_packet) # Save the decoded PCM as a new wav file wave_read.close()
wave_write.close()
print("Total bytes of encoded packets:", bytes_encoded)
print("Finished.")
三 运行结果
   这个只需要运行一下,结果就出来了。这里就不做赘述了。

基于python的opus编解码实力解析的更多相关文章

  1. python中的编解码小结

    在用python27写文件或者上传文件时遇到这样一个问题:.在网上搜了下说加入以下三行代码可以解决: import sys reload(sys) sys.setdefaultencoding('ut ...

  2. python进行base64编解码

    [转] 直接上代码 import base64 fin = open(r"D:\2.zip", "rb") fout = open(r"D:\2.x. ...

  3. android 音频编解码1

    1. Android 官方的 MediaCodec API 该 API 是在 Andorid 4.1 (API 16) 版本引入的 MediaCodec 使用的基本流程是: 1234567891011 ...

  4. Python 下JSON的两种编解码方式实例解析

    概念   JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写.在日常的工作中,应用范围极其广泛.这里就介绍python下它的两种编解码方法: ...

  5. 【并行计算与CUDA开发】基于NVIDIA显卡的硬编解码的一点心得 (完结)

    原文:基于NVIDIA显卡的硬编解码的一点心得 (完结) 1.硬解码软编码方法:大体流程,先用ffmpeg来读取视频文件的包,接着开启两个线程,一个用于硬解码,一个用于软编码,然后将读取的包传给解码器 ...

  6. 【听如子说】-python模块系列-AIS编解码Pyais

    Pyais Module Introduce pyais一个简单实用的ais编解码模块 工作中需要和ais打交道,在摸鱼的过程中发现了一个牛逼的模块,对ais编解码感兴趣的可以拿项目学习一下,或者运用 ...

  7. python rsa 加密解密 (编解码,base64编解码)

    最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...

  8. 音视频编解码问题:javaCV如何快速进行音频预处理和解复用编解码(基于javaCV-FFMPEG)

    前言: 前面我用了很多章实现了javaCV的基本操作,包括:音视频捕捉(摄像头视频捕捉和话筒音频捕捉),推流(本地音视频或者摄像头话筒混合推流到服务器),转流(rtsp->rtmp),收流(录制 ...

  9. python base64 编解码,转换成Opencv,PIL.Image图片格式

    二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...

  10. 【H.264/AVC视频编解码技术具体解释】十三、熵编码算法(4):H.264使用CAVLC解析宏块的残差数据

    <H.264/AVC视频编解码技术具体解释>视频教程已经在"CSDN学院"上线,视频中详述了H.264的背景.标准协议和实现,并通过一个实战project的形式对H.2 ...

随机推荐

  1. 驱动开发:应用DeviceIoContro开发模板

    内核中执行代码后需要将结果动态显示给应用层的用户,DeviceIoControl 是直接发送控制代码到指定的设备驱动程序,使相应的移动设备以执行相应的操作的函数,如下代码是一个经典的驱动开发模板框架, ...

  2. [zookeeper] 集群搭建及启动后查询服务器状态异常解决

    一.集群搭建 1.每台服务器上部署zookeeper 1.将zookeeper压缩包解压到指定位置,在zookeeper解压后目录下创建数据目录zkData 2.在zkData下创建myid文件,内容 ...

  3. Intel 4工艺太难了!酷睿Ultra终于突破5GHz

    无论是14nm还是10nm,Intel这些年的新工艺都有一个通性:刚诞生的时候性能平平,高频率都上不去,只能用于笔记本移动端(分别对应5代酷睿.10代酷睿),后期才不断成熟,比如到了13代酷睿就达到史 ...

  4. Docker从认识到实践再到底层原理(一)|技术架构

    前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总 然后就是博主最近最花时间的一 ...

  5. Linux 进程卡住了怎么办?

    在我们使用 Linux 系统时,如果网络或者磁盘等 I/O 出问题,会发现进程卡住了,即使用 kill -9 也无法杀掉进程,很多常用的调试工具,比如 strace, pstack 等也都失灵了,是怎 ...

  6. Oracle 19c RAC 自动应用RU补丁简明版

    环境:Oracle RAC(GI 19.3 + DB 19.3) 本文应用补丁信息, 19.16 RU: p34130714_190000_Linux-x86-64.zip 本文主要演示使用opatc ...

  7. Delphi原子操作函数介绍

    一.Delphi的原子操作函数 在System.SyncObjs单元中,有一个TInterlocked的密封类,其十多个类函数(class function)其实都是调用的System单元的原子操作函 ...

  8. SP16113 SUBTLEBA - Trucks Transportation 题解

    题目传送门 前言 本题样例有问题,如果想要样例可以去 vjudge 上. 本题提交后可能会出现 UKE ,建议前往 link 提交,而且本篇题解中所提供的代码也为 link 代码. 前置知识 Krus ...

  9. NC23051 华华和月月种树

    题目链接 题目 题目描述 华华看书了解到,一起玩养成类的游戏有助于两人培养感情.所以他决定和月月一起种一棵树.因为华华现在也是信息学高手了,所以他们种的树是信息学意义下的. 华华和月月一起维护了一棵动 ...

  10. 【OpenGL ES】立方体贴图(6张图)

    1 前言 ​ 本文通过一个立方体贴图的例子,讲解三维纹理贴图的应用,案例中使用 6 张不同的图片给立方体贴图,图片如下: ​ 本文涉及到的知识点主要包含:三维绘图.MVP 矩阵变换.纹理贴图,读者如果 ...