在Java中将mp3转换为WAV [英] Convert mp3 to wav in Java

查看:1023
本文介绍了在Java中将mp3转换为WAV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了mp3spi以支持使用javax.sound *库读取Java 8项目中的mp3文件.我现在的目标是将mp3写入wav文件.但是,结果不正确.这是最简单格式的代码:

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

这里概述了另一种方法(在Java中将MP3转换为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();

这会从4-5 mb的压缩mp3生成一个超过30 mb的文件,但是它不能用作有效的WAV文件.

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.

对我有用的方法涉及使用JLayer Converter类,但是,因为我想做一些其他处理,例如切掉一部分音频,修改音量和播放速度等,所以我觉得可能会更好使用本机库.

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.

推荐答案

打开mp3流后,通常必须先对其进行转换,然后再将其写入文件中.

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

赞(未品尝):

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

这篇关于在Java中将mp3转换为WAV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆