音频解码及如何在Java实现

本人并不干这个,但是被迫下水了解了一下这个,稍微做了一下整理。再就是感觉现在网上以及ChatGPT在这方面给出的答案太水了,在此开辟一篇。无意放出代码,这里只介绍一些可能重要的点。

本来以为有了ChatGPT写这些就没有必要了,现在看起来还是不太可能。

  • 一是知识稍微旧了一点,新一点的没有,比如github上近一年更新的优秀项目100%是不会出现在解决方案中的。追求能用是不影响的,但是想找最好的有点难。
  • 二是方案缺少出处,扩展溯源困难。
  • 三是多方案比较困难,不易从中选取出最好的方案。
  • 四是幻觉太多,验证成本太高。

1. 解码

这里将解码定义为将有损/无损的(压缩)格式,比如 mp3/aac 等等转换到 pcm/wav 这种原始数据的格式的操作,供下游使用。

原始数据可以理解为 采样率 × 位深度 × 通道数 × 音频时长 的数据,不加其它处理。

2. 基本参数

来自 javax.sound.sampled.AudioFormat,先要理解记住这些才能够有良好掌控

  • encoding -- the audio encoding technique

  • sampleRate -- the number of samples per second

  • sampleSizeInBits -- the number of bits in each sample

  • channels -- the number of channels (1 for mono, 2 for stereo, and so on)

  • frameSize -- the number of bytes in each frame

  • frameRate -- the number of frames per second

  • bigEndian -- indicates whether the data for a single sample is stored in big-endian byte order

3. FFmpeg

A complete, cross-platform solution to record, convert and stream audio and video.

可能是最强的功能非常齐全,离线格式转换命令行就用它了,但是要注意ffmpeg在做转换时有不少参数直接默认了,使用代码转换可能有各种对不上(所以需要对参数的理解)。

常见音频编码都是确定性编码,不要自欺欺人觉得大小不一致还是正常的,得每一个byte都一样才正常。

4. javax.sound.sampled

  • 支持PCM/WAV之类的原始音频格式
  • service provider: 在实现之后能够自动发现和支持更多格式

5. hendriks73/ffsampledsp

有了1-4的铺垫之后就只需要一个优秀项目来完成剩余的工作了,目前看起来FFSampledSP是最佳选择(之一,服务器的)

FFSampledSP is an implementation of the javax.sound.sampled service provider interfaces based on FFmpeg, a complete, cross-platform solution to record, convert and stream audio and video. FFSampledSP is part of the SampledSP collection of javax.sound.sampled libraries.

  • 相当于引入了ffmepg的能力,在已有的库中实现得是相当好的了。

  • 解码流的时候需要调试参数,可以先校验是否能转换

    java 复制代码
    AudioInputStream fileStream = AudioSystem.getAudioInputStream(file);
    AudioFormat sourceFormat = fileStream.getFormat();
    log.info("in audio format: {}", aacFormat);
    AudioFormat targetFormat = new AudioFormat();
    AudioSystem.isConversionSupported(sourceFormat, targetFormat); // 需要为true
  • 引入依赖即可,并无太多需要注意的。也就是知识要得挺多,使用起来反而没什么。

相关推荐
xcl09255 小时前
北京24小时自助健身房系统软件开发实战:从架构到部署全流程指南
java·spring boot·架构
一 乐7 小时前
动漫书销售商城|基于springboot + vue动漫书销售商城(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
卓怡学长7 小时前
w214基于jsp知道特产网
java·intellij-idea
步行cgn8 小时前
Spring 注解使用详解
java·spring
一条小小yu8 小时前
Spring IoC的理解
java·后端·spring
落魄实习生8 小时前
Agent Scope Java 2.x 系列【7】工具使用
java·开发语言·ai
旺仔学长 哈哈8 小时前
springboot钓鱼爱好者交流平台APP设计与实现
java·spring boot·mysql·充电桩管理系统
自强的小白10 小时前
核心功能(Service接口)
java·mybatis
乌暮11 小时前
深入理解 Java 泛型:把「万能盒子」用对、用稳
java·开发语言·后端·学习