音频解码及如何在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
  • 引入依赖即可,并无太多需要注意的。也就是知识要得挺多,使用起来反而没什么。

相关推荐
rabbit_pro7 分钟前
Java使用Mybatis-Plus封装动态数据源工具类
java·python·mybatis
期待のcode13 分钟前
Java虚拟机类加载机制
java·开发语言
短剑重铸之日20 分钟前
《SpringBoot4.0初识》第四篇:原生镜像
java·原生镜像·springboot4.0
程序员欣宸24 分钟前
LangChain4j实战之十二:结构化输出之三,json模式
java·人工智能·ai·json·langchain4j
天若有情67341 分钟前
打破思维定式!C++参数设计新范式:让结构体替代传统参数列表
java·开发语言·c++
亲爱的非洲野猪1 小时前
从ReentrantLock到AQS:深入解析Java并发锁的实现哲学
java·开发语言
wheelmouse77881 小时前
如何设置VSCode打开文件Tab页签换行
java·python
yangminlei1 小时前
Spring Boot——日志介绍和配置
java·spring boot
廋到被风吹走1 小时前
【Spring】Spring Boot Starter设计:公司级监控SDK实战指南
java·spring boot·spring
码头整点薯条1 小时前
启动报错:Invalid value type for attribute ‘factoryBeanObjectType‘ 解决方案
java