日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

将mp3格式的音频转换为采样率8k的wav

發(fā)布時(shí)間:2023/12/10 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将mp3格式的音频转换为采样率8k的wav 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

需求

最近系統(tǒng)上需要增加一個(gè)功能,就是測(cè)試我們系統(tǒng)的ASR識(shí)別引擎,這就需要上傳一段音頻,然后我們返回識(shí)別后的文字,但是我們的識(shí)別引擎需要采樣率16k,格式為wav的音頻文件,但是我們又不能限定用戶上傳的錄音格式,所以需要我們?cè)诤笈_(tái)轉(zhuǎn)換一下格式,然后再去識(shí)別。

1、MP3轉(zhuǎn)換wav

做這個(gè)功能時(shí)候, 發(fā)現(xiàn)網(wǎng)上的資料真的很少,所以,只能安全上網(wǎng)了,在外面找到了方法。

1.1 引入jar:

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

1.2 工具類代碼:

public boolean toWav(String inputFilePath, String outputFilePath) {Converter aConverter = new Converter();try {aConverter.convert(inputFilePath, outputFilePath);} catch (JavaLayerException e) {e.printStackTrace();return false;}return true;}

1.3 測(cè)試類:

public static void main(String args[]) {String filePath = "C:\\data\\hellowordread.pcm";String targetPath = "C:\\data\\111333.wav";toWav(filePath,targetPath);}

還是非常簡(jiǎn)單哦。

2、將wav轉(zhuǎn)換為8k采樣率

public void toStandardWav( String inputFilePath, String outputFilePath){try {byte[] bytes = Files.readAllBytes(new File(inputFilePath).toPath());WaveFileReader reader = new WaveFileReader();AudioInputStream audioIn = reader.getAudioInputStream(new ByteArrayInputStream(bytes));AudioFormat srcFormat = audioIn.getFormat();int targetSampleRate = 8000;AudioFormat dstFormat = new AudioFormat(srcFormat.getEncoding(),targetSampleRate,srcFormat.getSampleSizeInBits(),srcFormat.getChannels(),srcFormat.getFrameSize(),srcFormat.getFrameRate(),srcFormat.isBigEndian());System.out.println(audioIn.getFrameLength());AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn);File file = new File(outputFilePath);WaveFileWriter writer = new WaveFileWriter();writer.write(convertedIn, AudioFileFormat.Type.WAVE, file);} catch (Exception e) {e.printStackTrace();}}

總結(jié)

經(jīng)過(guò)上面代碼,我們就可以支持常用的音頻格式進(jìn)行ASR識(shí)別引擎的測(cè)試!

總結(jié)

以上是生活随笔為你收集整理的将mp3格式的音频转换为采样率8k的wav的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。