是否有任何纯 Java 方式将 .wav 转换为 .mp3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4997923/
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
Is there any pure java way to convert .wav to .mp3?
提问by Ivan Suhinin
I've struggled a lot with Java but could not combine a working example of Java .wav to .mp3 converter. This converter will be used in a Java applet so it should depend only on libraries written in pure Java with no underlying C code calls.
我在 Java 方面苦苦挣扎,但无法将 Java .wav 到 .mp3 转换器的工作示例结合起来。此转换器将用于 Java 小程序,因此它应仅依赖于用纯 Java 编写的库,没有底层 C 代码调用。
Can anyone provide a fully working example?
谁能提供一个完整的工作示例?
Thank you
谢谢
回答by Nathan Kidd
Read your wave file @ http://java.sun.com/javase/technologies/desktop/media/jmf/and encode to mp3 @ http://openinnowhere.sourceforge.net/lameonj/
阅读您的波形文件 @ http://java.sun.com/javase/technologies/desktop/media/jmf/并编码为 mp3 @ http://openinnowhere.sourceforge.net/lameonj/
As pointed out, lameonj is not a purejava solution. For that the options don't seem so many, but see the other SO question: MP3 Encoding in Java
正如所指出的,lameonj 不是纯Java 解决方案。为此,选项似乎并不多,但请参阅另一个 SO 问题:Java 中的 MP3 编码
回答by kitekat75
I use Jump3r to convert wav to mp3 on my project because the html5 player of IE11 can't play wav files.
我在我的项目中使用 Jump3r 将 wav 转换为 mp3,因为 IE11 的 html5 播放器无法播放 wav 文件。
Jump3r is the simpliest solution found to run inside a tomcat servlet. I wasn't able to integrate others solutions like jave certainly due to the security manager... Jump3r is a pure java program.
Jump3r 是在 tomcat servlet 中运行的最简单的解决方案。由于安全管理器,我当然无法集成其他解决方案,例如 jave... Jump3r 是一个纯 Java 程序。
Jump3r is available on the maven repository (https://mvnrepository.com/artifact/de.sciss/jump3r/1.0.4) and the sources are available on github (https://github.com/Sciss/jump3r)
Jump3r 在 maven 存储库 ( https://mvnrepository.com/artifact/de.sciss/jump3r/1.0.4) 上可用,源在 github ( https://github.com/Sciss/jump3r)上可用
To convert a file, you should call the main method (in the following code, I use an inlined version of the main method to catch/throw an IOException if necessary)
要转换文件,您应该调用 main 方法(在下面的代码中,如果需要,我使用 main 方法的内联版本来捕获/抛出 IOException)
private void convertWavFileToMp3File(File source, File target) throws IOException {
String[] mp3Args = { "--preset","standard",
"-q","0",
"-m","s",
source.getAbsolutePath(),
target.getAbsolutePath()
};
(new Main()).run(mp3Args);
}
回答by mihi
If speed is not important for you, take any c implementation of MP3 (e. g. lame) and try to compile it with NestedVMto Java bytecode. It will be slow (like an emulator in an emulator), but it should work.
如果速度对您来说不重要,请使用 MP3 的任何 c 实现(例如 lame)并尝试使用NestedVM将其编译为 Java 字节码。它会很慢(就像模拟器中的模拟器一样),但它应该可以工作。
And it should be way less work than trying to port a MP3 library to pure Java.
与尝试将 MP3 库移植到纯 Java 相比,它的工作量应该要少得多。
回答by Stephen C
Another potentially relevant project is jffmpeg. This apparently aimed to an JMF support for a wide range of formats using both native and Java codecs. Judging from the 'formats' page, they made significant progress on the pure Java side. Unfortunately, the project has gone quiet.
另一个可能相关的项目是jffmpeg。这显然旨在为使用本机和 Java 编解码器的各种格式提供 JMF 支持。从“格式”页面来看,他们在纯 Java 方面取得了重大进展。不幸的是,该项目已经安静下来。
This doesn't directly help the OP in the short term. But if he or others are keen to have pure Java codecs in the long term, consider getting involved.
这在短期内不会直接帮助 OP。但是,如果他或其他人希望长期拥有纯 Java 编解码器,请考虑参与其中。
回答by kenchis
Just check out the following source code. http://jsidplay2.cvs.sourceforge.net/jsidplay2/jump3rIt is still work in progress, but a working example of the encoder part of a pure java based lame library.
只需查看以下源代码。 http://jsidplay2.cvs.sourceforge.net/jsidplay2/jump3r它仍在进行中,但它是基于纯 Java 的 lame 库的编码器部分的工作示例。
回答by RogerClark
See this link on SourceForge http://sourceforge.net/projects/jump3r/files/
请参阅 SourceForge 上的此链接 http://sourceforge.net/projects/jump3r/files/
Its JAR's only (no source code), but it does work on both PC and Android, but no necessarily as described in the authors posting http://pure-java-mp3-encoder.blogspot.com.au/
它是唯一的 JAR(没有源代码),但它可以在 PC 和 Android 上运行,但不一定如作者在http://pure-java-mp3-encoder.blogspot.com.au/ 上所描述的那样
I got it to work by just using the jump3r-1.0.3.jar file, as a library, and instantiated mp3.Main then used called mp3.run() e.g. part of my Android code
我只是通过使用 jump3r-1.0.3.jar 文件作为库来让它工作,然后实例化 mp3.Main 然后使用称为 mp3.run() 例如我的 Android 代码的一部分
String[] mp3Args = {"--preset","standard",
"-q","0",
"-m","s",
Environment.getExternalStorageDirectory().getPath()+"/myfile.wav",
Environment.getExternalStorageDirectory().getPath()+"/myfile.mp3"};
Main m = new mp3.Main();
try
{
m.run(mp3Args);
}
catch(Exception e)
{
System.out.println("ERROR processing MP3 " + e);// Some bug in Android seems to cause error BufferedOutputSteam is Closed. But it still seems to work OK.
}
I suspect it would be possible to directly call the lame encoder passing buffers of data etc, but as the exact API for this Java version is not documented, it would require some research
我怀疑可以直接调用蹩脚的编码器传递数据缓冲区等,但由于没有记录此 Java 版本的确切 API,因此需要进行一些研究