用到的百度提供的api
需要把wav音频文件转成16k的频率,必须转,不转百度api解析不出来。显示音频文件不清晰错误。想要转化还必须要有ffmpeg程序,这个自己百度去下载。然后拿转好的文件扔到百度的api中。很简单。
pom
<!-- 百度语音识别 --> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.3.2</version> </dependency>
工具类Cover8xTo16x
package com.xiaoxin.yixinai._frame.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.util.ArrayList; import java.util.List; /** * Created by liuzhonghua on 2018/8/14. */ public class Cover8xTo16x { static final Logger logger = LoggerFactory.getLogger(Cover8xTo16x.class); /** * 音频文件频率8k转16k。必须要转,因为不转百度识别不出来,错误信息是音质太差 * @param sourceFile * @return */ public static File cover8xTo16x(File sourceFile){ String targetPath = null; try { File ffmpegPath = new File("E:\\project\\ffmpeg\\bin\\ffmpeg"); //存放ffmpeg程序的目录 targetPath = sourceFile.getAbsolutePath().replaceAll(".wav" , "_16x.wav"); // ffmpeg.exe -i source.wav -ar 16000 target.wav List<String> wavToPcm = new ArrayList<String>(); wavToPcm.add(ffmpegPath.getAbsolutePath()); wavToPcm.add("-i"); wavToPcm.add(sourceFile.getAbsolutePath()); wavToPcm.add("-ar"); wavToPcm.add("16000"); wavToPcm.add(targetPath); ProcessBuilder builder = new ProcessBuilder(); builder.command(wavToPcm); builder.redirectErrorStream(true); Process process = builder.start(); process.waitFor(); } catch (Exception e) { logger.error("录音文件8k转化16k失败"+e.getMessage()); e.printStackTrace(); return null; } if (StringUtils.isNotEmpty(targetPath)) { return new File(targetPath); } logger.error("传入的文件路径有误"); return null; } }
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY); client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); JSONObject res = client.asr("D:\\websites\\upload\\vox\\vns\\389_37\\4.6.1.ai1_16x.wav", "wav", 16000, null); System.out.println(res);