基于python的opus编解码实力解析
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编解码实力解析的更多相关文章
- python中的编解码小结
在用python27写文件或者上传文件时遇到这样一个问题:.在网上搜了下说加入以下三行代码可以解决: import sys reload(sys) sys.setdefaultencoding('ut ...
- python进行base64编解码
[转] 直接上代码 import base64 fin = open(r"D:\2.zip", "rb") fout = open(r"D:\2.x. ...
- android 音频编解码1
1. Android 官方的 MediaCodec API 该 API 是在 Andorid 4.1 (API 16) 版本引入的 MediaCodec 使用的基本流程是: 1234567891011 ...
- Python 下JSON的两种编解码方式实例解析
概念 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写.在日常的工作中,应用范围极其广泛.这里就介绍python下它的两种编解码方法: ...
- 【并行计算与CUDA开发】基于NVIDIA显卡的硬编解码的一点心得 (完结)
原文:基于NVIDIA显卡的硬编解码的一点心得 (完结) 1.硬解码软编码方法:大体流程,先用ffmpeg来读取视频文件的包,接着开启两个线程,一个用于硬解码,一个用于软编码,然后将读取的包传给解码器 ...
- 【听如子说】-python模块系列-AIS编解码Pyais
Pyais Module Introduce pyais一个简单实用的ais编解码模块 工作中需要和ais打交道,在摸鱼的过程中发现了一个牛逼的模块,对ais编解码感兴趣的可以拿项目学习一下,或者运用 ...
- python rsa 加密解密 (编解码,base64编解码)
最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...
- 音视频编解码问题:javaCV如何快速进行音频预处理和解复用编解码(基于javaCV-FFMPEG)
前言: 前面我用了很多章实现了javaCV的基本操作,包括:音视频捕捉(摄像头视频捕捉和话筒音频捕捉),推流(本地音视频或者摄像头话筒混合推流到服务器),转流(rtsp->rtmp),收流(录制 ...
- python base64 编解码,转换成Opencv,PIL.Image图片格式
二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...
- 【H.264/AVC视频编解码技术具体解释】十三、熵编码算法(4):H.264使用CAVLC解析宏块的残差数据
<H.264/AVC视频编解码技术具体解释>视频教程已经在"CSDN学院"上线,视频中详述了H.264的背景.标准协议和实现,并通过一个实战project的形式对H.2 ...
随机推荐
- 《Spring 手撸专栏》| 开篇介绍,我要带新人撸 Spring 啦!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 不正经!写写面经,去撸Spring源码啦? 是的,在写了4篇关于Spring核心源码 ...
- Python脚本之将一个文件夹划分多个文件夹和批量创建文件夹
import os import shutil # 要移出的文件路径 path = r"C:\old_dir" # 新创建多个文件夹的路径 new_path = r"C: ...
- Oracle 19c快速安装部署
最近学习开源,发现不止MySQL,很多开源的产品都有个特点:安装简单. 而回过头来看传统的Oracle确实是太重了,在这个用户产品快速更新迭代的时代下,如果数据库这类基础产品的安装部署都需花费很长时间 ...
- 【SpringBootStarter】自定义全局加解密组件
[SpringBootStarter] 目的 了解SpringBoot Starter相关概念以及开发流程 实现自定义SpringBoot Starter(全局加解密) 了解测试流程 优化 最终引用的 ...
- win10 通过 ssh 连接云服务器失败 are too open. bad permissions.
最近突然想起了自己的学生机服务器,买来了吃灰很久了,拿出来捣鼓捣鼓 以前服务器装的 windows server,这次把它重装成了 CentOS 8.0,然后按官网步连接步骤骤一步一步尝试. 腾讯云官 ...
- 机器学习基础01DAY
数据的特征抽取 现实世界中多数特征都不是连续变量,比如分类.文字.图像等,为了对非连续变量做特征表述,需要对这些特征做数学化表述,因此就用到了特征提取. sklearn.feature_extract ...
- 未配置Datasource时, 启动 SpringBoot 程序报错的问题
SpringBoot will show error if there is no datasource configuration in application.yml/application.pr ...
- IntersectionObserver对象
IntersectionObserver对象 IntersectionObserver对象,从属于Intersection Observer API,提供了一种异步观察目标元素与其祖先元素或顶级文档视 ...
- Spring Boot+Eureka+Spring Cloud微服务快速上手项目实战
说明 我看了一些教程要么写的太入门.要么就是写的太抽象.真正好的文章应该是快速使人受益的而不是浪费时间.本文通过一个包括组织.部门.员工等服务交互的案例让刚接触spring cloud微服务的朋友快速 ...
- JS实现提示文本框可输入剩余字数
最近在设计写博客功能时,涉及到留言框输入字数限制,需要给用户剩余数字提示. 参考文章:https://www.cnblogs.com/crazytrip/p/4968230.html 实现效果: 源码 ...