百度api语音识别

首先安装ffmpeg
1.处理MP3转 pcm
import java.io.File;

public class FfmpegTest {

public static void main(String[] args) {
String sPath = "C:\Users\Administrator\Desktop\1.mp3";
String tPath = "c:\Users\Administrator\Desktop\test.pcm";
try {
new FfmpegTest().changeAmrToMp3(sPath, tPath);
} catch (Exception e) {
e.printStackTrace();
}
}

public void changeAmrToMp3(String sourcePath, String targetPath) throws Exception {
String webroot = "D:\Program Files\ffmpeg\bin";
Runtime run = null;
try {
run = Runtime.getRuntime();
long start = System.currentTimeMillis();
System.out.println(new File(webroot).getAbsolutePath());
//执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
//wav转pcm
//Process p=run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
//mp3转pcm
Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -y -i " + sourcePath + " -acodec pcm_s16le -f s16le -ac 1 -ar 16000 " + targetPath);
//释放进程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
long end = System.currentTimeMillis();
System.out.println(sourcePath + " convert success, costs:" + (end - start) + "ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
//run调用lame解码器最后释放内存
run.freeMemory();
}
}
}

2.导入

maven:

  <dependency>
        <groupId>com.baidu.aip</groupId>
        <artifactId>java-sdk</artifactId>
        <version>4.3.2</version>
    </dependency>

3.书写百度语音识别代码
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.bind.DatatypeConverter;

import com.baidu.aip.speech.AipSpeech;
import org.json.JSONObject;

public class Sample {

private static final String serverURL = "http://vop.baidu.com/server_api";
private static String token = "";
private static final String testFileName = "c:\Users\Administrator\Desktop\test.pcm";
//put your own params here
//private static final String apiKey = "***";//这里的apiKey就是前面申请在应用卡片中的apiKey
//private static final String secretKey = "***";//这里的secretKey就是前面申请在应用卡片中的secretKey
//private static final String cuid = "***";//cuid是设备的唯一标示,因为我用的是PC,所以这里用的是网卡Mac地址
public static final String cuid = "1541********";
public static final String apiKey = "lReiTd4mY***********";
public static final String secretKey = "RM9Qf1mg2mTP8m8fGd**************";
public static void main(String[] args) throws Exception {
getToken();
/* method1();
method2();*/
method3();
}

private static void getToken() throws Exception {
String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
"&client_id=" + apiKey + "&client_secret=" + secretKey;
HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection();
token = new JSONObject(printResponse(conn)).getString("access_token");
}

/*private static void method1() throws Exception {
File pcmFile = new File(testFileName);
HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection();

// construct params
JSONObject params = new JSONObject();
params.put("format", "pcm");
params.put("rate", 8000);
params.put("channel", "1");
params.put("token", token);
params.put("cuid", cuid);
params.put("len", pcmFile.length());
params.put("speech", DatatypeConverter.printBase64Binary(loadFile(pcmFile)));

// add request header
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

conn.setDoInput(true);
conn.setDoOutput(true);

// send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(params.toString());
wr.flush();
wr.close();

printResponse(conn);
}

private static void method2() throws Exception {
File pcmFile = new File(testFileName);
HttpURLConnection conn = (HttpURLConnection) new URL(serverURL
+ "?cuid=" + cuid + "&token=" + token).openConnection();

// add request header
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "audio/pcm; rate=8000");

conn.setDoInput(true);
conn.setDoOutput(true);

// send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(loadFile(pcmFile));
wr.flush();
wr.close();

printResponse(conn);
}*/
private static void method3(){
// 初始化一个AipSpeech cuid apiKey secretKey
AipSpeech client = new AipSpeech(cuid, apiKey, secretKey);

// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);

// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
//client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理

// 可选:设置log4j日志输出格式,若不设置,则使用默认配置
// 也可以直接通过jvm启动参数设置此环境变量
//System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

// 调用接口
JSONObject res = client.asr(testFileName, "pcm", 16000, null);
System.out.println(res.toString(2));

}


private static String printResponse(HttpURLConnection conn) throws Exception {
if (conn.getResponseCode() != 200) {
// request error
return "";
}
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append(' ');
}
rd.close();
System.out.println(new JSONObject(response.toString()).toString(4));
return response.toString();
}

private static byte[] loadFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

long length = file.length();
byte[] bytes = new byte[(int) length];

int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}

if (offset < bytes.length) {
is.close();
throw new IOException("Could not completely read file " + file.getName());
}

is.close();
return bytes;
}
}
原文地址:https://www.cnblogs.com/zhengshao/p/10272131.html