日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将mp3格式的音频转换为采样率8k的wav 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求

最近系統上需要增加一個功能,就是測試我們系統的ASR識別引擎,這就需要上傳一段音頻,然后我們返回識別后的文字,但是我們的識別引擎需要采樣率16k,格式為wav的音頻文件,但是我們又不能限定用戶上傳的錄音格式,所以需要我們在后臺轉換一下格式,然后再去識別。

1、MP3轉換wav

做這個功能時候, 發現網上的資料真的很少,所以,只能安全上網了,在外面找到了方法。

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 測試類:

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

還是非常簡單哦。

2、將wav轉換為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();}}

總結

經過上面代碼,我們就可以支持常用的音頻格式進行ASR識別引擎的測試!

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。