Convert mp3 to wav in Java

﹥>﹥吖頭↗ 提交于 2019-12-22 11:29:53

问题


I installed the mp3spi to support reading mp3 files in my Java 8 project usng the javax.sound* libraries. My goal now is to write mp3 to a wav file. However, the result is incorrect. Here's the code in its simplest format:

public static void mp3ToWav(InputStream mp3Data) throws UnsupportedAudioFileException, IOException {
    AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(mp3Data);
    AudioFormat format = mp3Stream.getFormat();
    AudioFormat convertFormat = new AudioSystem.write(mp3Stream, Type.WAVE, new File("C:\\temp\\out.wav"));
}

There's one other way as outlined here (mp3 to wav conversion in java):

File mp3 = new File("C:\\music\\greatest-songs-of-all-time\\RebeccaBlack-Friday.mp3");
if(!mp3.exists()) {
    throw new FileNotFoundException("couldn't find mp3");
}
FileInputStream fis = new FileInputStream(mp3);
BufferedInputStream bis = new BufferedInputStream(fis);

AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(bis);
AudioFormat sourceFormat = mp3Stream.getFormat();
AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
        sourceFormat.getSampleRate(), 16, 
        sourceFormat.getChannels(), 
        sourceFormat.getChannels() * 2,
        sourceFormat.getSampleRate(),
        false);

try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(mp3Stream)) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(convertFormat, convert1AIS);
    System.out.println("Length is: " + mp3Stream.getFrameLength() + " div by " + mp3Stream.getFormat().getFrameRate());
    byte [] buffer = new byte[8192];
    int iteration = 0;
    while(true){
        int readCount = convert2AIS.read(buffer, 0, buffer.length);
        if(readCount == -1){
            break;
        }
        baos.write(buffer, 0, readCount);
        iteration++;
    }

    System.out.println("completed with iteration: " + iteration);

    FileOutputStream fw = new FileOutputStream("C:\\temp\\out-2.wav");
    fw.write(baos.toByteArray());
    fw.close();
}

bis.close();
fis.close();

This generates a file that's over 30 mb from a compressed mp3 of 4-5 mb, however it doesn't work as a valid WAV file.

The method that does work for me involves using JLayer Converter class, however, because I want to do some other processing like cutting out portions of audio, modifying the volume and playback speed, etc., I feel like I might be better off working with the native library.


回答1:


After you opened the mp3 stream, you typically have to convert it, before writing it to a file.

Like this (untested):

public static void mp3ToWav(File mp3Data) throws UnsupportedAudioFileException, IOException {
    // open stream
    AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(mp3Data);
    AudioFormat sourceFormat = mp3Stream.getFormat();
    // create audio format object for the desired stream/audio format
    // this is *not* the same as the file format (wav)
    AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
        sourceFormat.getSampleRate(), 16, 
        sourceFormat.getChannels(), 
        sourceFormat.getChannels() * 2,
        sourceFormat.getSampleRate(),
        false);
    // create stream that delivers the desired format
    AudioInputStream converted = AudioSystem.getAudioInputStream(convertFormat, mp3Stream);
    // write stream into a file with file format wav
    AudioSystem.write(converted, Type.WAVE, new File("C:\\temp\\out.wav"));
}


来源:https://stackoverflow.com/questions/41784397/convert-mp3-to-wav-in-java

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