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

相关推荐
文丑颜不良啊3 小时前
LeetCode3281~3300题目解析
java·leetcode
nice_lcj5204 小时前
【从零理解计算机网络 · 02】分层模型:别背 OSI 七层,看数据怎么被“打包“和“拆包“
java·后端·tcp/ip·计算机网络·面试·osi·分层模型
SimonKing5 小时前
给Deepseek Harness装上插件市场,从此插件无忧
java·后端·程序员
艾莉丝努力练剑5 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
aiotto资源分享6 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
RuoyiOffice6 小时前
SpringBoot3+Vue3 采购到付款:ERP、WMS与财务如何按同一来源协同
java·spring boot·架构·vue
文丑颜不良啊7 小时前
LeetCode3261~3280题目解析
java·leetcode
代码不停7 小时前
Spring 原理
java·spring
企业数字化笔记7 小时前
报表上线怎么对账?口径回归、权限抽检与差异定位
java·spring boot·mysql
乌暮7 小时前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis