package com.ld.igds.util;
|
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.LineNumberReader;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.concurrent.*;
|
|
/**
|
* 使用ffmpeg.exe的命令执行截图,不要添加任何JAR包。
|
* <p>
|
* 链接:https://pan.baidu.com/s/1MEvUXPMe6bDOsQYt5LMIxA 提取码:y177
|
*
|
* @author: andy.jia
|
* @description:
|
* @version:
|
* @data:2020年6月13日
|
*/
|
public class FfmpegUtil {
|
|
/**
|
* 定义ffmpeg工具的位置
|
*/
|
public static final String FFMPEG_PATH = "D:/IGDS/ffmpeg-win64-static/bin/ffmpeg.exe";
|
|
public static final String IMG_SIZE = "1280*720";
|
|
public static final Map<String, Process> contextProcess = new HashMap<>();
|
|
/**
|
* 关闭进程
|
*
|
* @param key
|
*/
|
public static void destroyProcess(String key) {
|
Process process = contextProcess.get(key);
|
contextProcess.remove(key);
|
if (null != process) {
|
process.destroy();
|
}
|
}
|
|
public static void addProcess(String key, Process process) {
|
contextProcess.put(key, process);
|
}
|
|
/**
|
* 根据视频流转换为图片
|
*
|
* @param mediaAddr
|
* @param key
|
* @return
|
*/
|
public static String cutImgByMediaAddr(String mediaAddr, String baseFile,String fileName, String key) {
|
// 图片命令
|
StringBuilder command = new StringBuilder();
|
command.append(FFMPEG_PATH);
|
command.append(" -i ");
|
command.append(mediaAddr);
|
command.append(" -vframes");
|
command.append(" 1");
|
command.append(" -y");
|
command.append(" -f");
|
command.append(" image2");
|
command.append(" -t");
|
command.append(" 1");
|
command.append(" -s ");
|
command.append(IMG_SIZE);
|
command.append(" ");
|
command.append(baseFile+fileName);
|
|
InputStreamReader isr = null;
|
LineNumberReader input = null;
|
Process process;
|
try {
|
process = Runtime.getRuntime().exec(command.toString());
|
addProcess(key, process);
|
isr = new InputStreamReader(process.getInputStream());
|
input = new LineNumberReader(isr);
|
while (input.readLine() != null) {
|
}
|
} catch (java.io.IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != input)
|
input.close();
|
if (null != isr)
|
isr.close();
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
|
destroyProcess(key);
|
}
|
return fileName;
|
}
|
|
public static String cutImgByMediaAddr(String mediaAddr, String diskFile, String key) {
|
// 图片命令
|
StringBuilder command = new StringBuilder();
|
command.append(FFMPEG_PATH);
|
command.append(" -i ");
|
command.append(mediaAddr);
|
command.append(" -vframes");
|
command.append(" 1");
|
command.append(" -y");
|
command.append(" -f");
|
command.append(" image2");
|
command.append(" -t");
|
command.append(" 1");
|
command.append(" -s ");
|
command.append(IMG_SIZE);
|
command.append(" ");
|
command.append(diskFile);
|
|
InputStreamReader isr = null;
|
LineNumberReader input = null;
|
Process process;
|
try {
|
process = Runtime.getRuntime().exec(command.toString());
|
addProcess(key, process);
|
isr = new InputStreamReader(process.getInputStream());
|
input = new LineNumberReader(isr);
|
while (input.readLine() != null) {
|
}
|
} catch (java.io.IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != input)
|
input.close();
|
if (null != isr)
|
isr.close();
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
|
destroyProcess(key);
|
}
|
return diskFile;
|
}
|
|
|
/**
|
* 调用抓拍,每个动作执行6S
|
*
|
* @param mediaAddr
|
* @param baseFile
|
* @return
|
*/
|
public static String snapImg(String mediaAddr, String baseFile, String fileName) {
|
final ExecutorService exec = Executors.newFixedThreadPool(1);
|
String key = ContextUtil.getUUID();
|
|
Callable<String> call = new Callable<String>() {
|
public String call() throws Exception {
|
return cutImgByMediaAddr(mediaAddr, baseFile,fileName, key);
|
}
|
};
|
try {
|
Future<String> future = exec.submit(call);
|
future.get(6 * 1000, TimeUnit.MILLISECONDS);
|
} catch (Exception ex) {
|
destroyProcess(key);
|
return null;
|
}
|
exec.shutdown();
|
|
return fileName;
|
}
|
|
public static String cutImgByMediaAddr1(String mediaAddr, String outFile) {
|
final ExecutorService exec = Executors.newFixedThreadPool(1);
|
String key = ContextUtil.getUUID();
|
Callable<String> call = new Callable<String>() {
|
public String call() throws Exception {
|
return cutImgByMediaAddr(mediaAddr, outFile, key);
|
}
|
};
|
try {
|
Future<String> future = exec.submit(call);
|
future.get(5 * 1000, TimeUnit.MILLISECONDS);
|
} catch (Exception ex) {
|
destroyProcess(key);
|
return "=============后台保存图片执行时间超时======";
|
}
|
exec.shutdown();
|
|
return outFile;
|
}
|
|
public static String cutImgByMediaAddr2(String mediaAddr, String outFile) {
|
final ExecutorService exec = Executors.newFixedThreadPool(1);
|
String key = ContextUtil.getUUID();
|
Callable<String> call = new Callable<String>() {
|
public String call() throws Exception {
|
return cutImgByMediaAddr(mediaAddr, outFile, key);
|
}
|
};
|
try {
|
Future<String> future = exec.submit(call);
|
future.get(5 * 1000, TimeUnit.MILLISECONDS);
|
} catch (Exception ex) {
|
destroyProcess(key);
|
return "=============后台保存图片执行时间超时======";
|
}
|
exec.shutdown();
|
|
return outFile;
|
}
|
|
public static String cutImgByMediaAddr3(String mediaAddr, String outFile) {
|
final ExecutorService exec = Executors.newFixedThreadPool(1);
|
String key = ContextUtil.getUUID();
|
Callable<String> call = new Callable<String>() {
|
public String call() throws Exception {
|
return cutImgByMediaAddr(mediaAddr, outFile, key);
|
}
|
};
|
try {
|
Future<String> future = exec.submit(call);
|
future.get(5 * 1000, TimeUnit.MILLISECONDS);
|
} catch (Exception ex) {
|
destroyProcess(key);
|
return "=============后台保存图片执行时间超时======";
|
}
|
exec.shutdown();
|
|
return outFile;
|
}
|
|
public static String getImgStr(String imgFile) {
|
// FileInputStream in = null;
|
// byte[] data = null;
|
// // 读取图片字节数组
|
// try {
|
// in = new FileInputStream(imgFile);
|
// data = new byte[in.available()];
|
// in.read(data);
|
// in.close();
|
// } catch (IOException e) {
|
// e.printStackTrace();
|
// return null;
|
// }
|
// String imgData = "data:image/jpg;base64,"
|
// + Base64Util.encode2String(data);
|
// return imgData;
|
|
return Base64Util.getImageStr(imgFile);
|
}
|
|
public static void main(String[] args) {
|
|
String rtsp = "rtsp://admin:admin@192.168.1.102:554/h264/ch1/sub/av_stream";
|
|
rtsp = "rtsp://admin:smd123456@192.168.2.61:554/cam/realmonitor?channel=1&subtype=1&proto=Dahua3";
|
|
FfmpegUtil
|
.cutImgByMediaAddr1(rtsp,
|
"D:\\IGDS\\FILE\\INOUT\\" + System.currentTimeMillis()
|
+ ".jpg");
|
|
System.out.println("===================");
|
}
|
|
}
|