视频截图Util

时间:2023-12-26 17:22:55

视频截图Util视频截图Util​​

VideoToPicUtil.java

 package com.zhwy.util;

 import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* 视频转码_截图
*/
public class VideoToPicUtil { public static void main(String[] args) {
String videoPathURL = "D:/ffmpeg_mencoder_File/sourceVideos/短视频.mp4";//源视频文件路径
VideoToPic(videoPathURL);
}
/**
* 视频截图工具
* @param videoPath 源视频文件路径
* @return
*/
public static String VideoToPic(String videoPath){
if (!is_File(videoPath)) { //判断路径是不是一个文件
System.out.println(videoPath + " is not file");
}
String VideoToPicResult=executeCodecs(videoPath);
return VideoToPicResult;
} /**
* 判断路径是不是一个文件
*
* @param file
* 源视频文件路径
*/
private static boolean is_File(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
} /**
* 判断视频格式
*/
private static int is_VideoType(String srcFilePath) {
String type = srcFilePath.substring(srcFilePath.lastIndexOf(".") + 1, srcFilePath.length()).toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 0;
} else if (type.equals("mpg")) {
return 0;
} else if (type.equals("wmv")) {
return 0;
} else if (type.equals("3gp")) {
return 0;
} else if (type.equals("mov")) {
return 0;
} else if (type.equals("mp4")) {
return 0;
} else if (type.equals("asf")) {
return 0;
} else if (type.equals("asx")) {
return 0;
} else if (type.equals("flv")) {
return 0;
}
// 对ffmpeg.exe无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder.exe)转换为.avi(ffmpeg能解析的格式).
else if (type.equals("wmv9")) {
return 1;
} else if (type.equals("rm")) {
return 1;
} else if (type.equals("rmvb")) {
return 1;
}
return 9;
}
/**
* 源视频转换成AVI格式
*
* @param type
* 视频格式
*/
// 对ffmpeg.exe无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder.exe)转换为avi(ffmpeg能解析的)格式.
private static String convertToAVI(int type,String srcFilePath) {
String mencoderPath = "D:\\ffmpeg_mencoder_File\\Tools\\mencoder.exe"; // 转换工具路径
String fileNameWithoutSuffix=getFileNameWithoutSuffix(srcFilePath);
String codcFilePath ="D:\\ffmpeg_mencoder_File\\targetVideos\\"+fileNameWithoutSuffix+".avi";//【存放转码后视频的路径,记住一定是.avi后缀的文件名】
List<String> commend = new ArrayList<String>();
commend.add(mencoderPath);
commend.add(srcFilePath);
commend.add("-oac");
commend.add("lavc");
commend.add("-lavcopts");
commend.add("acodec=mp3:abitrate=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("avi");
commend.add("-o");
commend.add(codcFilePath);
try {
//调用线程命令启动转码
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return codcFilePath;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 源视频转换成FLV格式
* @param srcFilePathParam
* 源:用于指定要转换格式的文件,要截图的源视频文件路径
*/
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static String convertToFLV(String srcFilePathParam) {
if (!is_File(srcFilePathParam)) {
System.out.println(srcFilePathParam + " is not file");
}
// 文件命名
String fileNameWithoutSuffix=getFileNameWithoutSuffix(srcFilePathParam);
String codcFilePath ="D:\\ffmpeg_mencoder_File\\targetVideos\\"+fileNameWithoutSuffix+".flv";//【存放转码后视频的路径,记住一定是.flv后缀的文件名】
String mediaPicPath ="d:\\ffmpeg_mencoder_File\\cutPicture\\"+ fileNameWithoutSuffix+".jpg";
System.out.println("fileNameWithoutSuffix:"+fileNameWithoutSuffix);
Calendar c = Calendar.getInstance();
String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
String ffmpegPath = "D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe";
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(srcFilePathParam);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("600x500");
commend.add(codcFilePath);
String cutPicPath =null;
try {
Runtime runtime = Runtime.getRuntime();
Process proce = null;
cutPicPath = " D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe -i "
+ srcFilePathParam
+ " -y -f image2 -ss 2 -t 0.001 -s 600x500 "+ mediaPicPath; //截图文件的保存路径
proce = runtime.exec(cutPicPath);
//调用线程命令进行转码
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
builder.start();
} catch (Exception e) {
e.printStackTrace();
}
String tempFile= codcFilePath;
boolean status=deleteAVIFile(tempFile);//删除转码后的目标文件
System.out.println("是否删除成功呢"+status);
return cutPicPath;
}
/**
* 获取不带后缀名的文件名
* @param fileUrl 源视频文件路径
*/
public static String getFileNameWithoutSuffix(String fileUrl){
File file=new File(fileUrl);
String fileNameWithoutSuffix=file.getName().replaceAll("[.][^.]+$", "");
System.out.println("获取不带后缀名的文件名方法中输出:"+fileNameWithoutSuffix);
return fileNameWithoutSuffix;
}
/**
* 删除转换后的单个目标视频文件
* @param tempFile 转换后的目标视频文件
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteAVIFile(String tempFile) {
File file = new File(tempFile);
if (file.exists()==true) {
if (file.delete()==true) {
return true;
}else{
return false;
}
}else{
return false;
}
} /**
* 视频转码(mencoder.exe或ffmpeg.exe执行编码解码)
*/
private static String executeCodecs(String videoPath) {
// 判断视频的类型
int type = is_VideoType(videoPath);
String picResult = null;
//如果是ffmpeg可以转换的类型直接转码,否则先用mencoder转码成AVI
if (type == 0) {
System.out.println("直接将文件转为flv文件");
picResult = convertToFLV(videoPath);// 直接将文件转为flv文件 ,并返回截图路径
} else if (type == 1) { //
String codcFilePath = convertToAVI(type,videoPath); //视频格式转换后的目标视频文件路径 if (codcFilePath == null){
String message="avi文件没有得到";
return message;// avi文件没有得到
}
picResult = convertToFLV(codcFilePath);// 将avi转为flv,并返回截图路径
}
return picResult;
}
}