好房网

网站首页百科全书 >正文

如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)

2022-06-11 03:05:21 百科全书来源:
导读目前大家应该是对如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)比较感兴趣的,所以今天好房网小编CC就来为大家整理了一些关于...
目前大家应该是对如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)比较感兴趣的,所以今天好房网小编CC就来为大家整理了一些关于如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)方面的相关知识来分享给大家,希望大家会喜欢哦。

如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)

最近系统上需要增加一个功能,就是测试我们系统的ASR识别引擎,这就需要上传一段音频,然后我们返回识别后的文字,但是我们的识别引擎需要采样率16k,格式为wav的音频文件,但是我们又不能限定用户上传的录音格式,所以需要我们在后台转换一下格式,然后再去识别。

MP3转换wav

做这个功能时候, 发现网上的资料真的很少,所以,只能安全上网了,在外面找到了方法。

1 引入jar:

<dependency><groupId>javazoom</groupId><artifactId>jlayer</artifactId><version>0.1</version></dependency>

2 工具类代码:

publicbooleantoWav(StringinputFilePath,StringoutputFilePath){ConverteraConverter=newConverter();try{aConverter.convert(inputFilePath,outputFilePath);}catch(JavaLayerExceptione){e.printStackTrace();returnfalse;}returntrue;}

3 测试类:

publicstaticvoidmain(Stringargs[]){StringfilePath="C:\\data\\hellowordread.pcm";StringtargetPath="C:\\data\\11133wav";toWav(filePath,targetPath);}

还是非常简单哦。

将wav转换为8k采样率
publicvoidtoStandardWav(StringinputFilePath,StringoutputFilePath){try{byte[]bytes=Files.readAllBytes(newFile(inputFilePath).toPath());WaveFileReaderreader=newWaveFileReader();AudioInputStreamaudioIn=reader.getAudioInputStream(newByteArrayInputStream(bytes));AudioFormatsrcFormat=audioIn.getFormat();inttargetSampleRate=8000;AudioFormatdstFormat=newAudioFormat(srcFormat.getEncoding(),targetSampleRate,srcFormat.getSampleSizeInBits(),srcFormat.getChannels(),srcFormat.getFrameSize(),srcFormat.getFrameRate(),srcFormat.isBigEndian());System.out.println(audioIn.getFrameLength());AudioInputStreamconvertedIn=AudioSystem.getAudioInputStream(dstFormat,audioIn);Filefile=newFile(outputFilePath);WaveFileWriterwriter=newWaveFileWriter();writer.write(convertedIn,AudioFileFormat.Type.WAVE,file);}catch(Exceptione){e.printStackTrace();}}

总结

经过上面代码,我们就可以支持常用的音频格式进行ASR识别引擎的测试!


版权说明:本文由用户上传,如有侵权请联系删除!


标签:

热点推荐
热评文章
随机文章