java——MP3转wav

转载地址

java 下mp3 转 wav

相对原文有改动。
注:MP3转wav并不会提升源文件的音质。

Maven

<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>mp3spi</artifactId>
	<version>1.9.5.4</version>
</dependency>

示例

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Test2 {
	
	public static AudioFormat targetFormat = null;
	
	
	
    /**
     * mp3转pcm(8k 16bit)
     * @param mp3filepath
     * @param pcmfilepath
     * @return
     */
    public static boolean Mp3ToPcm(String mp3filepath, String pcmfilepath){
        try {
            //获取文件的音频流,pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //将音频转化为  pcm的格式保存下来
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取MP3音频流
     * @param mp3filepath
     * @return
     */
    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        
        try {
            AudioInputStream in = null;
            //读取音频文件的类
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            //设定输出格式为pcm格式的音频文件
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels()*16/8, baseFormat.getFrameRate(), false);
            //输出到音频
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }
    /**
     * pcm(8k 16bit)转wav(16k 16bit)
     * @param pcmfilepath
     * @param wavfilepath
     * @throws IOException
     */
    public static void pcmToWav(String pcmfilepath,String wavfilepath) throws IOException {
        FileInputStream fis = new FileInputStream(pcmfilepath);
        byte channels = (byte) targetFormat.getChannels();
        int sampleRate = (int) targetFormat.getSampleRate();
        int byteRate = targetFormat.getFrameSize()*sampleRate*channels/8;
        int datalen = (int)fis.getChannel().size();
        ByteBuffer bb = ByteBuffer.allocate(44);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.put(new byte[] {'R','I','F','F'});//RIFF标记
        bb.putInt(datalen+44-8);//原始数据长度(不包含RIFF和本字段共8个字节)
        bb.put(new byte[] {'W','A','V','E'});//WAVE标记
        bb.put(new byte[] {'f','m','t',' '});//fmt标记
        bb.putInt(16);//“fmt”字段的长度,存储该子块的字节数(不含前面的Subchunk1ID和Subchunk1Size这8个字节)
        bb.putShort((short)1);//存储音频文件的编码格式,PCM其存储值为1
        bb.putShort((short) targetFormat.getChannels());//通道数,单通道(Mono)值为1,双通道(Stereo)值为2
        //采样率
        bb.putInt(sampleRate);
        //音频数据传送速率,采样率*通道数*采样深度/8。(每秒存储的bit数,其值=SampleRate * NumChannels * BitsPerSample/8)
        bb.putInt(byteRate);
        //块对齐/帧大小,NumChannels * BitsPerSample/8
        bb.putShort((short)(targetFormat.getChannels()*16/8));
        //pcm数据位数,一般为8,16,32等
        bb.putShort((short)16);
        bb.put(new byte[] {'d','a','t','a'});//data标记
        bb.putInt(datalen);//data数据长度
        byte[] header = bb.array();
        //wav头
//        for(int i=0;i<header.length;i++) {
//            System.out.printf("%02x ",header[i]);
//        }
        ByteBuffer wavbuff = ByteBuffer.allocate(44+datalen);
        wavbuff.put(header);
        byte[] temp = new byte[datalen];
        fis.read(temp);
        wavbuff.put(temp);
        byte[] wavbytes = wavbuff.array();
        FileOutputStream fos = new FileOutputStream(wavfilepath);
        fos.write(wavbytes);
        fos.flush();
        fos.close();
        fis.close();
        System.out.println("end");
    }

    public static void main(String[] args) throws IOException {
        String mp3filepath = "C:\\Users\\Administrator\\Music\\等什么君 - 归寻.mp3";//mp3文件路径
        String pcmfilepath = "C:\\Users\\Administrator\\Music\\等什么君 - 归寻.pcm";//转化后的pcm路径
        String wavfilepath = "C:\\Users\\Administrator\\Music\\等什么君 - 归寻.wav";//转化后的wav路径
        Mp3ToPcm(mp3filepath,pcmfilepath);
        pcmToWav(pcmfilepath,wavfilepath);
    }
}
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将 MP3 换为 WAV,可以使用 Java 中的 JLayer 库和 Tritonus 库。 首先,您需要从以下网址下载 JLayer 库和 Tritonus 库: - JLayer: http://www.javazoom.net/javalayer/javalayer.html - Tritonus: http://www.tritonus.org/plugins.html 将这些库文件添加到您的 Java 项目中,并使用以下代码将 MP3 文件换为 WAV 文件: ```java import java.io.*; import javax.sound.sampled.*; import javazoom.jl.decoder.*; public class MP3toWAVConverter { public static void convert(String filename) throws IOException, UnsupportedAudioFileException, Exception { String outputFilename = filename.replace(".mp3", ".wav"); InputStream in = new FileInputStream(filename); OutputStream out = new FileOutputStream(outputFilename); Decoder decoder = new Decoder(); AudioFormat format = new AudioFormat(44100, 16, 2, true, false); javax.sound.sampled.AudioInputStream ais = new javax.sound.sampled.AudioInputStream(new BufferedInputStream(new DecoderInputStream(in)), format, DecoderInputStream.getLength()); AudioSystem.write(ais, AudioFileFormat.Type.WAVE, out); out.close(); in.close(); } } ``` 在上面的代码中,我们使用 JLayer 库的 Decoder 类将 MP3 文件解码为音频流,然后使用 Java 内置的 AudioSystem 类将音频流写入 WAV 文件。 要使用上面的代码,只需调用 `MP3toWAVConverter.convert(filename)` 方法并传入要换的 MP3 文件名。换后的 WAV 文件将与 MP3 文件位于同一目录中,并且文件名将相同,只是扩展名不同。 注意,这种方法可能会导致换后的 WAV 文件大小较大,因为 WAV 格式的音频文件通常比 MP3 格式的文件更大。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值