java 实现视频转换通用工具类:获取视频元数据信息(一)
java 实现视频转换通用工具类:视频相互转换-Ffmpeg(三)
1.ffmpeg 截图,自定义命令行
/**
* ffmpeg 截图,自定义命令行
* @param srcVideoPath 源文件
* @param shellLine 自定义shell命令行
* @param tarImagePath 目标文件
* @return
*/
public static boolean processFfmpegImage(String srcVideoPath,String shellLine,String tarImagePath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(srcVideoPath);
commend.add(shellLine);
commend.add(tarImagePath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
InputStream is = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader inputBufferedReader = new BufferedReader(
inputStreamReader);
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while ((line = inputBufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputBufferedReader.close();
inputBufferedReader = null;
inputStreamReader.close();
inputStreamReader = null;
is.close();
is = null;
if (!checkfile(tarImagePath)) {
logger.info(tarImagePath + " is not exit! processFfmpegImage 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegImage 转换不成功 !");
return false;
}
}
2.ffmpeg 截图,并指定图片的大小
/**
* ffmpeg 截图,并指定图片的大小
* @param srcVideoPath
* @param tarImagePath 截取后图片路径
* @param width 截图的宽
* @param hight 截图的高
* @param offsetValue 表示相对于文件开始处的时间偏移值 可以是分秒
* @param vframes 表示截图的桢数
*
* @return
*/
public static boolean processFfmpegImage(String srcVideoPath,String tarImagePath,int width,int hight,float offsetValue,float vframes) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(srcVideoPath);
commend.add("-y");
commend.add("-f");
commend.add("image2");
commend.add("-ss");
commend.add(offsetValue+""); //在视频的某个插入时间截图,例子为5秒后
commend.add("-vframes");
commend.add(vframes + ""); //截图的桢数
commend.add("-s");
commend.add(width + "x" +hight); //截图的的大小
commend.add(tarImagePath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarImagePath)) {
logger.info(tarImagePath + " is not exit! processFfmpegImage 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegImage 转换不成功 !");
return false;
}
}