将mp3格式的音频转换为采样率8k的wav
生活随笔
收集整理的這篇文章主要介紹了
将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 工具類(lèi)代碼:
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è)試類(lèi):
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)題。
- 上一篇: halcon区域腐蚀膨胀算子_Halco
- 下一篇: 关于目录操作walk