安装文件
- libmp3lame MP3编码器不需要的可以不安装
- nasm-2.13
- 地址:https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/
- x264 chrome 可以识别的MP4格式
- git clone http://git.videolan.org/git/x264.git
- ffmpeg
-
安装流程
-
安装lame
- chmod -R 755 .
- ./configure --enable-shared --enable-static
- make
- make install
-
安装nasm
- ./configure
- make
- make install
-
安装x264
- chmod -R 755 .
- ./configure --enable-shared --enable-static
- make
- make install
-
安装ffmpeg
- ./configure --enable-shared --enable-libx264 --enable-libmp3lame --enable-gpl
- 注意x264是gpl协议的 不可以用于商用
-
指定依赖库的环境变量
- export LD_LIBRARY_PATH=/usr/local/lib/
java代码
- 获取视频的基本信息
// 根据视频路径获取基本信息
public int getTime(String sourcePath) {
int time = 0;
Runtime run = null;
try {
run = Runtime.getRuntime();
long start=System.currentTimeMillis();
// 抽帧哈 ffmpeg -i a.mp4
String snapshotCmd = "ffmpeg -i " + sourcePath;
Process snapshotProcess = run.exec(snapshotCmd);
//从输入流中读取视频信息 ffmpeg的信息在错误输出流里面
BufferedReader br = new BufferedReader(new InputStreamReader(snapshotProcess.getErrorStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
stringBuilder.append(line);
}
br.close();
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(stringBuilder.toString());
if (m.find()) {
time = getTimelen(m.group(1));
log.info("视频时长:" + time + "s , 开始时间:" + m.group(2) + ", 比特率:" + m.group(3) + "kb/s");
}
String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
pattern = Pattern.compile(regexVideo);
m = pattern.matcher(stringBuilder.toString());
if (m.find()) {
log.info("编码格式:" + m.group(1) + ", 视频格式:" + m.group(2) + ", 分辨率:" + m.group(3) + "kb/s");
}
return time;
} catch (Exception e) {
log.info("获取视频信息失败");
return 0;
}
}
- 转码
public void transcodeMp4(String sourcePath , String targetPath) {
Runtime run = null;
try {
run = Runtime.getRuntime();
long start=System.currentTimeMillis();
String cmd = "ffmpge -y -i " + sourcePath + " " + targetPath;
log.info(cmd);
Process p = run.exec(cmd);
//释放进程
printFfmpegStream(p.getInputStream());
printFfmpegStream(p.getErrorStream());
p.waitFor();
long end=System.currentTimeMillis();
long costTime = end - start;
log.info(sourcePath + " convert success, costs:" + costTime + "ms-------------------------------------------------");
ResultModel<Boolean> booleanResultModel = mediaCmsServiceClient.localTranscodeVideo(request);
log.info("回调成功 转码成功 \n{} \n{}", JSON.toJSONString(request), JSON.toJSONString(booleanResultModel));
} catch (Exception e) {
log.info("回调成功 转码失败 \n{}", JSON.toJSONString(request));
}finally{
run.freeMemory();
}
}
- 截图
public void snapshotMp4(String sourcePath, String targetFolderPath,Integer id) {
Runtime run = null;
try {
run = Runtime.getRuntime();
long start=System.currentTimeMillis();
int time = getTime(sourcePath);
float v = 10f / time;
// 抽帧哈 ffmpeg -i 870101971ad54167a8ff9cf05edfa9b5.mp4 image/image-%05d.jpeg
String snapshotCmd = "ffmpge -y -i " + sourcePath + " -r " + new DecimalFormat("0.####").format(Math.max(v,0.00001)) + " " + targetFolderPath + "image-%05d.jpg";
log.info(snapshotCmd);
Process snapshotProcess = run.exec(snapshotCmd);
//释放进程
printFfmpegStream(snapshotProcess.getInputStream());
snapshotProcess.getOutputStream().close();
snapshotProcess.waitFor();
long end=System.currentTimeMillis();
long costTime = end - start;
log.info(sourcePath + " snapshot success, costs:" + costTime + "ms-------------------------------------------------");
} catch (Exception e) {
log.error("抽帧失败", e);
}finally{
//run调用lame解码器最后释放内存
run.freeMemory();
}
}