将mp3格式的音频转换为采样率8k的wav 如何将mp3转换成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) { &n资源网bsp; e.printStackTrace(); return false; } 资源网 return true; }1.3 测试类:
【将mp3格式的音频转换为采样率8k的wav 如何将mp3转换成wav】 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 inputF资源网ilePath, 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(); } }
推荐阅读
- 郑智|足协官方认定郑智为广州助教:无职业级证书 名义主帅将是傅博
- 三星|沿用S22极简设计:Galaxy S23 Ultra变化将是三星史上最小
- 蒸鲜枣保存
- Firefox浏览器|火狐浏览器新增实用工具:将支持图片文字识别
- 家谱怎么编写,有什么格式 家谱编写
- iOS 16 支持这个适配器将标准 CarPlay 变成无线
- 苹果|苹果顶级经销商确认iPhone 14 mini将发布:或依然5199元起!这一手出乎意料
- Google搜索将降低非原创内容的排序
- raw格式怎么转换jpg?这几款raw格式转换软件你值得拥有
- 芯片|特斯拉车主将芯片植入身体:右手成了车钥匙 左手房门钥匙