用到的百度提供的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);