Java将MP3文件转换成wav格式文件

本文档介绍了一个Java程序,用于将MP3文件转换为WAV格式。通过读取MP3文件,将其转换为中间的PCM格式,再转化为阿里云语音识别所需的格式,最终保存为WAV文件。此外,作者还计划分享工作中的需求解决方案和提供数据集。
摘要由CSDN通过智能技术生成

本人公众号上线啦!!!

公众号与博客名一样:没有腹肌的程序猿
公众号文章类型:工作上所遇到的需求实现方案分享。
此外也会提供一些数据集供大家使用。(这个还在规划中,毕竟打工人时间挺紧的,哈哈哈哈)
到时候也会不定期给大家抽一些小东西哦。

Java将MP3文件转换成wav格式文件

(实操过,可以运行.)

<% 
//将MP3文件转为wav文件
String filePath = "D:\\test.mp3";
String targetPath = "D:\\test.wav";
byteToWav(getBytes(filePath), targetPath);
%>
<%!
public static boolean byteToWav(byte[] sourceBytes, String targetPath){
	if (sourceBytes == null || sourceBytes.length == 0) {
		System.out.println("Illegal Argument passed to this method");
		return false;
		}
		try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {
		AudioFormat sourceFormat = sourceAIS.getFormat();
		// 设置MP3的语音格式,并设置16bit
		AudioFormat mp3tFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
		// 设置阿里语音识别的音频格式
		AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, false);
		try (
		// 先通过MP3转一次,使音频流能的格式完整
		final AudioInputStream mp3AIS = AudioSystem.getAudioInputStream(mp3tFormat, sourceAIS);
		// 转成阿里需要的流
		final AudioInputStream pcmAIS = AudioSystem.getAudioInputStream(pcmFormat, mp3AIS)) {
		// 根据路径生成wav文件
		AudioSystem.write(pcmAIS, AudioFileFormat.Type.WAVE, new File(targetPath));
		}
		return true;
		} catch (IOException e) {
			System.out.println("文件转换异常:" + e.getMessage());
			return false;
		} catch (UnsupportedAudioFileException e) {
			System.out.println("文件转换异常:" + e.getMessage());
			return false;
		}

}

%>
<%!
public static byte[] getBytes(String filepath){
	byte[] buffer = null;
	try{
		File file =  new File(filepath);
		FileInputStream fis = new FileInputStream(file);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1000];
		int n;
		while((n=fis.read(b))!=-1){
			bos.write(b,0,n);
		}
		fis.close();
		bos.close();
		buffer = bos.toByteArray();
		
	}catch(Exception e){
		e.printStackTrace();
	}
	return buffer;
}


%>
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
您好!很高兴为您解答问题。 以下是Java将音频文件换为WAV文件的示例代码: ```java import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; import java.io.File; import java.io.IOException; public class AudioConverter { public void convertToWav(File inputFile, File outputFile) { try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile); AudioFormat sourceFormat = audioInputStream.getFormat(); AudioFormat targetFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false); AudioInputStream pcmAudioInputStream = AudioSystem.getAudioInputStream(targetFormat, audioInputStream); AudioSystem.write(pcmAudioInputStream, AudioFileFormat.Type.WAVE, outputFile); pcmAudioInputStream.close(); audioInputStream.close(); } catch (UnsupportedAudioFileException | IOException e) { e.printStackTrace(); } } } ``` 此代码将任何音频文件换为WAV格式,并将其保存在指定的输出文件中。要使用该代码,您需要创建两个文件对象来分别表示输入和输出文件。您可以像这样调用上述方法: ```java File inputFile = new File("path/to/myaudio.mp3"); File outputFile = new File("path/to/myaudio.wav"); AudioConverter audioConverter = new AudioConverter(); audioConverter.convertToWav(inputFile, outputFile); ``` 希望这可以帮助您将音频文件换为WAV格式。如果您需要更多帮助,请告诉我。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值