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

目录
相关文章
|
机器学习/深度学习 存储 JSON
ModelScope问题之加载训到一半保存的checkpoint接着训练如何解决
ModelScope训练是指在ModelScope平台上对机器学习模型进行训练的活动;本合集将介绍ModelScope训练流程、模型优化技巧和训练过程中的常见问题解决方法。
276 0
|
测试技术 Linux
Linux(8)Debain系统测试EC25-EUX模块usbnet0(qmi qcm)问题点
Linux(8)Debain系统测试EC25-EUX模块usbnet0(qmi qcm)问题点
428 0
ffmpeg中--enable-gpl什么意思
ffmpeg中--enable-gpl什么意思
750 0
ffmpeg中--enable-gpl什么意思
|
网络协议 Linux 网络安全
|
JavaScript API
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts
|
存储 编解码 算法
在线音频转换工具 - 免费
云库工具是一款强大的音频格式转换器,支持AAC、AC3、MP3、FLAC等多种格式,具备快速高效、简便易用、高质量输出和批量转换的技术优势。适用于多设备兼容、存储优化和专业音频处理场景。无论新手或专业人士,都能轻松满足音频格式转换需求。尝试云库工具,体验高效便捷的转换服务。
866 0
在线音频转换工具 - 免费
|
编解码 Linux vr&ar
如何使用ffmpeg将.m4a 格式转换为 pcma格式
ffmpeg是一款开源的万能媒体格式转换工具。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的
|
消息中间件 并行计算 Python
Python3,如何实现CPU的并行计算,那还不简单,5种方式,这篇就搞定。
Python3,如何实现CPU的并行计算,那还不简单,5种方式,这篇就搞定。
566 0
|
编解码 网络协议 安全
GB28181智能安全帽方案探究及技术实现
GB28181智能安全帽方案探究及技术实现
443 0
|
存储 前端开发 Go
golang正则表达式regexp
golang正则表达式regexp
404 0