将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识别引擎的测试!

目录
相关文章
|
小程序
微信小程序如何实现进入小程序自动连WiFi功能
微信小程序如何实现进入小程序自动连WiFi功能
588 0
|
Java Maven Docker
java 音频转为wav格式标准音频 | Java工具类
java 音频转为wav格式标准音频 | Java工具类
java 音频转为wav格式标准音频 | Java工具类
|
10月前
|
人工智能 API 开发者
通过宏实现Word接入DeepSeek
本文介绍如何在Microsoft Word中通过宏接入DeepSeek,实现自动化文本处理。首先确保具备Word 2016及以上版本、DeepSeek API密钥和VBA基础。接着,从豆包平台获取API密钥及模型ID,并在Word中启用开发者选项和宏功能。最后,编写VBA宏代码调用DeepSeek API,完成文本分析与处理。
697 0
vue3.2中setup语法糖父组件如何调用子组件中的方法
vue3.2中setup语法糖父组件如何调用子组件中的方法
vue3.2中setup语法糖父组件如何调用子组件中的方法
|
存储 定位技术 API
C语言实现贪吃蛇【完整版】
C语言实现贪吃蛇【完整版】
|
开发工具 git
【最新】如何将idea上的项目推送到gitee
【最新】如何将idea上的项目推送到gitee
3600 0
|
JavaScript 小程序 Java
基于SSM框架的购物商城系统设计与实现
基于SSM框架的购物商城系统设计与实现
523 2
|
XML JSON Java
图文并茂:解析Spring Boot Controller返回图片的三种方式
图文并茂:解析Spring Boot Controller返回图片的三种方式
1581 0
|
移动开发 小程序 JavaScript
微信小程序与H5的区别?
微信小程序与H5的区别?
|
编解码 网络协议 安全
GB28181智能安全帽方案探究及技术实现
GB28181智能安全帽方案探究及技术实现
487 0

热门文章

最新文章