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

访客3年前黑客资讯310

如何将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>
      &nbsp百思特网; </dependency>

1.2 工具类代码:

public boolean toWav(String inputFilePath, String outputFilePath) {
 
        Converter aConverter = new Converter();        try {
            aConverter.convert(inputFilePath, outputFilePath);
        } catch (JavaLayerException e) {
            e.printStackTrace();         &n百思特网bsp;  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());
            WaveFileR百思特网eader 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识别引擎的测试!

相关文章

中方回应华为在瑞典遭超级电池美打压:中国是美国胁迫

  中新网北京1月4日电 (记者 梁晓辉)就瑞典媒体曝光美国施压瑞典禁止华为参与该国5G建设一事,中国外交部发言人华春莹4日在例行记者会上说,这个报道再次证明,在世界范围里到处威逼利诱、搞胁迫外交的恰...

了不起的文字年度100句(收藏版)

五岁的时候,妈妈汇报我快乐是人生的要害。 上学今后他们问我长大后的志愿和空想是什么? 我写下“快乐”。他们说我没搞清楚题目, 我汇报他们,是他们没搞清楚人生。 ——列侬 人类之所以有进步, 主要原因...

黑客作案的32工具(常见黑客技术)

黑客作案的32工具(常见黑客技术)

本文目录一览: 1、一般黑客常用的工具有哪些,要分类型 2、黑客常用工具有哪些 3、黑客作案时要用到哪些工具? 4、黑客常用工具 一般黑客常用的工具有哪些,要分类型 渗透、免杀、破解、渗...

被赌博网站黑了找黑客有用吗-哪个黑客可以帮我盗回QQ

被赌博网站黑了找黑客有用吗-哪个黑客可以帮我盗回QQ

被赌博网站黑了找黑客有用吗相关问题 黑客初学者应该看什么书相关问题 黑客什么的还 电脑怎么进入编程模式(电脑如何进入编程模式)...

怎么去掉磁盘写保护?方法就这么简单

怎么去掉磁盘写保护?方法就这么简单

相信很多人在使用U盘的过程中,经常出现U盘写保护的情况,导致无法向U盘拷贝文件,同时还无法修改U盘文件。而U盘写保护如果是通过U盘的按钮控制倒还比较容易解除,通常只需要拨动按钮到“开锁”的状态,即可成...

黑客为什么抓不到(为什么抓不到苍蝇)-黑客入门软件游戏

黑客为什么抓不到(为什么抓不到苍蝇)-黑客入门软件游戏

黑客为什么抓不到(为什么抓不到苍蝇)(tiechemo.com)一直致力于黑客(HACK)技术、黑客QQ群、信息安全、web安全、渗透运维、黑客工具、找黑客、黑客联系方式、24小时在线网络黑客、黑客业...