转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/89388603 谢谢
下载ffmpeg
工具类
public class ConvertVideoUtils {
private static Logger logger = LoggerFactory.getLogger(ConvertVideoUtils.class);
// public static void main(String[] args) {
// // getPath();
// //视频的地址
// String inputPath = "D:\\File\\VIDEOMATERIAL\\2019-04-11\\ORIGINAL\\2019041117072504231015.avi";
// //视频转完格式存放地址
// String outputPath = "D:\\File\\VIDEOMATERIAL\\2019-04-11\\MP4\\2019041117072504231015.mp4";
// //转换视频的插件
// String ffmpegPath = "E:\\ffmpeg\\";
//
// if (!checkfile(inputPath)) {
// logger.debug(inputPath + " is not file");
// return;
// }
// if (process(inputPath,outputPath,ffmpegPath)) {
// logger.debug("ok");
// }
// }
//
// public static boolean process(String inputPath,String outputPath,String ffmpegPath) {
// int type = checkContentType(inputPath);
// boolean status = false;
// logger.debug("直接转成mp4格式");
// // 直接转成mp4格式
// status = processJpg(inputPath,outputPath,ffmpegPath);
// return status;
// }
public static int checkContentType(String inputPath) {
String type = inputPath.substring(inputPath.lastIndexOf(".") + 1).toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if ("avi".equals(type)) {
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("mp4")) {
return 0;
} else if (type.equals("mov")) {
return 0;
} else if (type.equals("asf")) {
return 0;
} else if (type.equals("asx")) {
return 0;
} else if (type.equals("flv")) {
return 0;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
// 可以先用别的工具(mencoder)转换为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;
}
/**
* @Description: 检查该路径是否是一个文件
* @method: checkfile
* @Param: path
* @return: boolean
* @auther: LHL
* @Date: 2019/4/11 14:27
*/
public static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
/**
* 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
*/
public static boolean processAVI(String inputPath, String outputPath, String ffmpegPath) {
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath + "/mencoder");
commend.add(inputPath);
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("mp4");
commend.add("-o");
commend.add(outputPath);
Process process = null;
try {
ProcessBuilder builder = new ProcessBuilder();
process = builder.command(commend).redirectErrorStream(true).start();
new PrintStream(process.getInputStream());
new PrintStream(process.getErrorStream());
process.waitFor();
if (!checkfile(outputPath)) {
logger.info(outputPath + " is not exit! processFfmpegAVI 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
process.destroy();
}
}
/**
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) + "a.flv"
*/
public static boolean processFlv(String oldfilepath, String outputPath, String ffmpegPath) {
if (!checkfile(oldfilepath)) {
logger.error("【" + oldfilepath + "】 不存在 !");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "/ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-ab");
command.add("56");
command.add("-ar");
command.add("22050");
command.add("-qscale");
command.add("8");
command.add("-r");
command.add("15");
command.add("-s");
command.add("600x500");
command.add(outputPath);
Process videoProcess = null;
try {
videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
videoProcess.waitFor();
if (!checkfile(outputPath)) {
logger.info(outputPath + " is not exit! processFfmpegFlv 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.debug("【" + outputPath + "】processFfmpegFLV 转换不成功 !");
e.printStackTrace();
return false;
} finally {
videoProcess.destroy();
}
}
public static boolean processMp4(String oldfilepath, String outputPath, String ffmpegPath) {
if (!checkfile(oldfilepath)) {
logger.error("【" + oldfilepath + "】 不存在 !");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "/ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-mbd");
command.add("0");
command.add("-c:a");
command.add("aac");
command.add("-strict");
command.add("-2");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-movflags");
command.add("faststart");
command.add(outputPath);
Process videoProcess = null;
try {
logger.debug("视频转换mp4开始...");
videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
videoProcess.waitFor();
if (!checkfile(outputPath)) {
logger.debug(outputPath + " is not exit! processFfmpegMP4 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.debug("【" + outputPath + "】processFfmpegMP4 转换不成功 !");
e.printStackTrace();
return false;
} finally {
videoProcess.destroy();
}
}
public static boolean processJpg(String inputPath, String outputPath, String ffmpegPath) {
if (!checkfile(inputPath)) {
logger.error("【" + inputPath + "】 不存在 !");
return false;
}
List<String> cutpic = new ArrayList<String>();
cutpic.add(ffmpegPath + "/ffmpeg");
cutpic.add("-i");
cutpic.add(inputPath); // 同上(指定的文件即可以是转换为flv格式之前的文件,也可以是转换的flv文件)
cutpic.add("-y");
cutpic.add("-f");
cutpic.add("image2");
cutpic.add("-ss"); // 添加参数"-ss",该参数指定截取的起始时间
cutpic.add("0"); // 添加起始时间为第17秒
cutpic.add("-t"); // 添加参数"-t",该参数指定持续时间
cutpic.add("0.001"); // 添加持续时间为1毫秒
cutpic.add("-s"); // 添加参数"-s",该参数指定截取的图片大小
cutpic.add("500*400"); // 添加截取的图片大小为350*240
cutpic.add(outputPath); // 添加截取的图片的保存路径
ProcessBuilder builder = new ProcessBuilder();
try {
builder.command(cutpic);
builder.redirectErrorStream(true);
logger.debug("视频截图开始...");
Process process = builder.start();
new PrintStream(process.getErrorStream()).start();
new PrintStream(process.getInputStream()).start();
process.waitFor();
if (!checkfile(outputPath)) {
logger.debug(outputPath + " is not exit! 截图不成功 !");
return false;
}
return true;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
logger.debug("视频截图失败!");
return false;
} finally {
builder.directory();
}
}
}
class PrintStream extends Thread {
InputStream __is = null;
public PrintStream(java.io.InputStream is) {
__is = is;
}
@Override
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch != -1) {
System.out.print((char) _ch);
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}