如何使用 Java 将音频流转换为 MP3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/343240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:01:46  来源:igfitidea点击:

How do I convert an audio stream to MP3 using Java?

javamp3captureencoderecording

提问by ken

Is it possible using Java to convert a real time audio stream from the mixer to MP3?

是否可以使用 Java 将实时音频流从混音器转换为 MP3?

  • It has to be converted chunk by chunk otherwise the memory will be exhausted.
  • I already know how to record but only to lossless formats such as wav and aiff.
  • I need conversion as the audio comes in.
  • 它必须逐块转换,否则内存将被耗尽。
  • 我已经知道如何录制,但只能录制为 wav 和 aiff 等无损格式。
  • 当音频进来时,我需要转换。

回答by VonC

May be you could use LAMEOnJ, which is a 100% Java API wrapping the standard LAME API (LAME being a MP3 encoder).

也许您可以使用LAMEOnJ,它是一个 100% Java API 包装标准 LAME API(LAME 是 MP3 编码器)。

I am not sure however it would encode "chunk by chunk"...

但是我不确定它会“逐块编码”...

回答by Art Clarke

Use Xuggler to do it. It's free, and open source (LGPL), and uses libmp3lame under the covers. And the MediaTool APIin Xuggler gives lots of examples of how to use it:

使用 Xuggler 来做到这一点。它是免费的、开源的 (LGPL),并在幕后使用 libmp3lame。Xuggler中的MediaTool API提供了许多如何使用它的示例:

回答by PhiLho

Lame is indeed the de facto standard on MP3 encoding, partly because of its high quality (encoding MP3 is hard to do right... and it is a subjective art), partly because of legal issues (MP3 is a patented technique, AFAIK, that's why you get Lame binaries from a limited number of sites, most of them in countries where these legal issues are less (or not) enforced).

Lame 确实是 MP3 编码的事实上的标准,部分原因是它的质量高(编码 MP3 很难做到正确......而且它是一种主观艺术),部分原因是法律问题(MP3 是一项专利技术,AFAIK,这就是为什么您从有限数量的站点获取 Lame 二进制文件的原因,其中大多数站点位于这些法律问题较少(或不)执行的国家/地区)。

The LAMEOnJ project found by VonC seems to use a non-free JNI implementation, but I suppose you can do your own binding with JNI or even JNA, particularly if you target a precise system.

VonC 发现的 LAMEOnJ 项目似乎使用了非自由的 JNI 实现,但我想您可以使用 JNI 甚至 JNA 进行自己的绑定,特别是如果您针对的是精确系统。

回答by Aaron Digulla

Create a new process in Java and write the stream to the stdin of to a tool like SoXto have it convert the audio data to MP3 on the fly.

在 Java 中创建一个新进程并将流写入到像SoX这样的工具的标准输入中,让它动态地将音频数据转换为 MP3。

回答by Bork Blatt

As to the first question - is it possible, the only limitation I can see would be processor speed. If the algorithm you are using and your processor are both fast enough, there is no reason why it wouldn't be possible.

至于第一个问题 - 是否有可能,我能看到的唯一限制是处理器速度。如果您使用的算法和处理器都足够快,则没有理由不可能。

I know this isn't Java source, but it may get you on the right track - try looking at the Lame MP3 Encoder.

我知道这不是 Java 源代码,但它可能会让您走上正轨 - 尝试查看Lame MP3 Encoder

As far as I know all MP3 conversion algorithms work chunk by chunk. They don't need to read the whole WAV file to begin conversion, but they convert on the fly.

据我所知,所有 MP3 转换算法都是逐块工作的。他们不需要阅读整个 WAV 文件即可开始转换,但他们会即时转换。

回答by krishnakumar

try using Ring buffer to encode the raw audio buffer in fly.

尝试使用环形缓冲区对原始音频缓冲区进行编码。