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

发布日期:2024-05-16 19:10:23     手机:https://m.xinb2b.cn/yule/news107282.html    违规举报
核心提示:如何将mp3转换成wav(将mp3格式的音频转换为采样率8k的wav)最近系统上需要增加一个功能,就是测试我们系统的ASR识别引擎,这就需要上传一段音频,然后我们返回识别后的文字,但是我们的识别引擎需要采样率16k,格式为wav的音频文件,

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

如何将mp3转换成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识别引擎的测试!


 
 
本文地址:https://www.xinb2b.cn/yule/news107282.html,转载请注明出处。

推荐图文
推荐娱乐运动
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  违规举报  |  蜀ICP备18010318号-4  |  百度地图  | 
Processed in 0.065 second(s), 91 queries, Memory 0.47 M