1.百度ai官网,下载sdk包,连接:http://ai.baidu.com/docs#/ASR-Online-Java-SDK/top
2.官网有demo,可以参考,下面是在下的util类,
import org.apache.log4j.Logger;
import org.json.JSONObject;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.util.Util;
public class BaiduAipUtils {
protected static Logger logger = Logger.getLogger(BaiduAipUtils.class);
//设置APPID/AK/SK
public static final String APP_ID = "";
public static final String API_KEY = "";
public static final String SECRET_KEY = "";
public static String getBaiduAipResult(String saveUrt,String type){
logger.info("百度aip------------->BaiduAipUtils:url="+saveUrt);
// 初始化一个FaceClient
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
if(type == null)
type = "wav";
if(saveUrt == null)
return null;
JSONObject res = client.asr(saveUrt, type, 16000, null);
String works = "";
if("0".equals(res.get("err_no").toString()) && "success.".equals(res.get("err_msg").toString())){
works = res.getJSONArray("result").getString(0);
}
logger.info(res.toString(2));
logger.info(works);
return works;
}
/**
* @param client
* @throws Throwable
*/
public void synthesis(AipSpeech client) throws Throwable
{
// 对本地语音文件进行识别
String path = "D:\\code\\java-sdk\\speech_sdk\\src\\test\\resources\\16k_test.pcm";
JSONObject asrRes = client.asr(path, "pcm", 16000, null);
System.out.println(asrRes);
// 对语音二进制数据进行识别
byte[] data = Util.readFileByBytes(path); //readFileByBytes仅为获取二进制数据示例
JSONObject asrRes2 = client.asr(data, "pcm", 16000, null);
System.out.println(asrRes);
// 对网络上音频进行识别
String url = "http://somehost/res/16k_test.pcm";
String callback = "http://callbackhost/aip/dump";
JSONObject res = client.asr(url, callback, "pcm", 16000, null);
System.out.println(res);
}
}
3.视频转码插件 ffmpeg
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FFMpegUtil implements IStringGetter {
private int runtime = 0;
private String ffmpegUri;//ffmpeg的全路径 如e:/ffmpeg/ffmpeg.exe 或 /root/ffmpeg/bin/ffmpeg
private String originFileUri; // 视频源文件地址
private enum FFMpegUtilStatus {
Empty, CheckingFile, GettingRuntime
};
private FFMpegUtilStatus status = FFMpegUtilStatus.Empty;
/**
* 执行命令组合
*/
private List
/**
* 构造函数
*
* @param ffmpegUri
* ffmpeg的全路径 如e:/ffmpeg/ffmpeg.exe 或 /root/ffmpeg/bin/ffmpeg
* @param originFileUri
* 所操作视频文件的全路径 如e:/upload/temp/test.wmv
*/
public FFMpegUtil(String ffmpegUri,String originFileUri) {
this.ffmpegUri = ffmpegUri;
this.originFileUri = originFileUri;
}
/**
* 获取视频时长
*
* @return
*/
public int getRuntime() {
runtime = 0;
status = FFMpegUtilStatus.GettingRuntime;
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");
cmd.add(originFileUri);
CmdExecuter.exec(cmd, this);
return runtime;
}
/**
* 检测文件是否是支持的格式 将检测视频文件本身,而不是扩展名
*
* @return
*/
public boolean isSupported() {
isSupported = true;
status = FFMpegUtilStatus.CheckingFile;
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");
cmd.add(originFileUri);
CmdExecuter.exec(cmd, this);
return isSupported;
}
private boolean isSupported;
/**
* 生成视频截图
*
* @param imageSavePath
* 截图文件保存全路径
* @param screenSize
* 截图大小 如640x480
*/
public void makeScreenCut(String imageSavePath) {
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");//设定输入流
cmd.add(originFileUri);
cmd.add("-q:v");//质量为基础的VBR
cmd.add("2");
cmd.add("-y");//覆盖输出文件
cmd.add("-f");//设定输出格式
cmd.add("image2");//格式
//cmd.add("-ss");//开始时间
//cmd.add("8");
//cmd.add("-t");//设置纪录时间
//cmd.add("0.001");
//cmd.add("-s");//设定画面的宽与高
//cmd.add(screenSize);//暂时不要截屏大小
cmd.add(imageSavePath);
CmdExecuter.exec(cmd, null);
}
/**
* 视频转换
*
* @param fileSavePath
* 文件保存全路径(包括扩展名)如 e:/abc/test.flv
* @param screenSize
* 视频分辨率 如640x480
* @param audioByte
* 音频比特率
* @param audioCollection
* 音频采样率
* @param quality
* 视频质量(0.01-255)越低质量越好
* @param fps
* 每秒帧数(15或29.97)
*/
public void videoTransfer(String fileSavePath, String screenSize, int audioByte, int audioCollection,
double quality, double fps) {
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");
cmd.add(originFileUri);
cmd.add("-y");
cmd.add("-ab");
cmd.add(Integer.toString(audioByte));
cmd.add("-ar");
cmd.add(Integer.toString(audioCollection));
cmd.add("-qscale");
cmd.add(Double.toString(quality));
cmd.add("-r");
cmd.add(Double.toString(fps));
cmd.add("-s");
cmd.add(screenSize);
cmd.add(fileSavePath);
CmdExecuter.exec(cmd, null);
}
/**
* 视频转换
*
* @param fileSavePath
* 文件保存全路径(包括扩展名)如 e:/abc/test.flv
* @param screenSize
* 视频分辨率 如640x480
* @param audioByte
* 音频比特率
* @param audioCollection
* 音频采样率
* @param quality
* 视频质量(0.01-255)越低质量越好
* @param fps
* 每秒帧数(15或29.97)
*/
public void videoTransfer(String fileSavePath) {
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");
cmd.add(originFileUri);
cmd.add("-y");
cmd.add(fileSavePath);
CmdExecuter.exec(cmd, null);
}
@Override
public void dealString(String str) {
switch (status) {
case Empty:
break;
case CheckingFile: {
Matcher m = Pattern.compile("Unknown format").matcher(str);
if (m.find())
this.isSupported = false;
break;
}
case GettingRuntime: {
Matcher m = Pattern.compile("Duration: //w+://w+://w+").matcher(str);
while (m.find()) {
String msg = m.group();
msg = msg.replace("Duration: ", "");
runtime = msg.length();
}
break;
}
}// switch
}
/**
* 转成pcm
* @param fileSavePath
*/
public void videoTransferPcm(String fileSavePath) {
cmd.clear();
cmd.add(ffmpegUri);
cmd.add("-i");
cmd.add(originFileUri);
cmd.add("-f");
cmd.add("s16le"); //16 位有符号数
cmd.add("-ar");
cmd.add("16000"); //16000 Hz 采样率
cmd.add("-ac");
cmd.add("1"); // 单声道
cmd.add(fileSavePath);
CmdExecuter.exec(cmd, null);
}
}
4.执行linux命令,util类
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
public class CmdExecuter {
/**
* 执行指令
*
* @param cmd
* 执行指令
* @param getter
* 指令返回处理接口,若为null则不处理输出
*/
static public void exec(List
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
builder.redirectErrorStream(true);
Process proc = builder.start();
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
if (getter != null)
getter.dealString(line);
}
proc.waitFor();
stdout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.
package com.sinaif.common.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
public class CmdExecuter {
/**
* 执行指令
*
* @param cmd
* 执行指令
* @param getter
* 指令返回处理接口,若为null则不处理输出
*/
static public void exec(List
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
builder.redirectErrorStream(true);
Process proc = builder.start();
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
if (getter != null)
getter.dealString(line);
}
proc.waitFor();
stdout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.ffmpeg 相关操作命令,参考官网:
https://ffmpeg.org/ffmpeg.html,也可以百度别人总结,这里就不赘述了
7.解决ffmpeg comman not found 问题
[hoover@110 bin]# ffmpeg -i /usr/local/ffmpeg/15023354983067356.mp4 -y /usr/local/ffmpeg/aa.wav
-bash: ffmpeg: command not found
写全路径,从根目录开始
[hoover@110 bin]# /usr/local/ffmpeg/bin/ffmpeg -i /usr/local/ffmpeg/15023352284504711.mp4 -f s16le -ar 8000 -ac 1 /usr/local/ffmpeg/dd.pcm