将mp3格式的音频转换为采样率8k的wav
生活随笔
收集整理的這篇文章主要介紹了
将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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: halcon区域腐蚀膨胀算子_Halco
- 下一篇: 关于目录操作walk