Is there any pure java way to convert .wav to .mp3?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 10:04:37
Nathan Kidd

Read your wave file @ http://java.sun.com/javase/technologies/desktop/media/jmf/ and encode to mp3 @ http://openinnowhere.sourceforge.net/lameonj/

As pointed out, lameonj is not a pure java solution. For that the options don't seem so many, but see the other SO question: MP3 Encoding in Java

If speed is not important for you, take any c implementation of MP3 (e. g. lame) and try to compile it with NestedVM to Java bytecode. It will be slow (like an emulator in an emulator), but it should work.

And it should be way less work than trying to port a MP3 library to pure Java.

I use Jump3r to convert wav to mp3 on my project because the html5 player of IE11 can't play wav files.

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 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)

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)

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);
}

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.

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.

Just check out the following source code. http://jsidplay2.cvs.sourceforge.net/jsidplay2/jump3r It is still work in progress, but a working example of the encoder part of a pure java based lame library.

See this link on 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/

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!