java 实现视频转换通用工具类:获取视频元数据信息(一)
java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二)
Mencoder方法暂时没有用上,同时ffmpeg都是采用的编译好的静态文件。视频转换时没有加额外的参数,如果有需要,可添加扩展就ok。
1.判断系统支持那些编码
/**
* 判断系统支持那些编码
* @param srcVideoPath
* @return
*/
public static void processFfmpegCodeFormat() {
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-formats");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.redirectErrorStream(true);
Process p= builder.start();
BufferedReader buf = null; // 保存ffmpeg的输出结果流
String line = null;
buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb= new StringBuffer();
while ((line = buf.readLine()) != null) {
sb.append(line + "\n");
continue;
}
p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
logger.info("【系统支持的视频编码】" + sb.toString());
} catch (Exception e) {
logger.error("获取失败 !");
}
}
2.ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数)
/**
* ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数)
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
* @param srcVideoPath 视频文件(原)
* @param tarVideoPath 视频文件(新)
* @return
*/
public static boolean processFfmpegOther(String srcVideoPath,String tarVideoPath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
// String type =tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1, tarVideoPath.length());
commend.add(ffmpegPath);
commend.add( "-y");
commend.add( "-i");
commend.add(srcVideoPath);
// if(type.toUpperCase().equals("MP4")){
// commend.add( " -f h264 ");
// }else{
//
// }
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.info(tarVideoPath + " is not exit! processFfmpegOther 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】processFfmpegOther 转换不成功 !");
return false;
}
}
/**
* ffmpeg按照时间段进行截取
* @param srcVideoPath 视频文件(原)
* @param tarImgPath 图片文件Path
* @return
*/
public static boolean processFfmpegBySureTime(String srcVideoPath,String tarImgPath) {
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(BaseCommonUtil.STARTTIME);
commend.add("-t");
commend.add(BaseCommonUtil.ENDTIME);
//commend.add("-s"); //指定图片大小
//commend.add("500x400");
commend.add(tarImgPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarImgPath)) {
logger.error("【" + srcVideoPath + "】 processFfmpegBySureTime 截图不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegOther 截图不成功 !");
return false;
}
}
4.ffmpeg按照时间段进行截取(加时间段)
/**
* ffmpeg按照时间段进行截取
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
* @param srcVideoPath 视频文件(原)
* @param tarVideoPath 视频文件(新)
* @param startTime 开始时间 形如:00:12:20
* @param endTime 结束时间 形如:01:20:10
* @return
*/
public static boolean processFfmpegByTime(String srcVideoPath,String tarVideoPath,String startTime,String endTime) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-ss");
if(startTime != null && !startTime.equals("")){
commend.add(startTime);
}else{
commend.add(BaseCommonUtil.STARTTIME);
}
commend.add("-t");
if(endTime != null && !endTime.equals("")){
if(!calTime(getSplitStr(startTime),getSplitStr(endTime)).equals("")){
commend.add(calTime(getSplitStr(startTime),getSplitStr(endTime)));
}else{
return false;
}
}else{
commend.add(BaseCommonUtil.ENDTIME);
}
commend.add("-y"); //覆盖
commend.add("-i");
commend.add(srcVideoPath);
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.error("【" + tarVideoPath + "】processFfmpegByTime 截图不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegByTime 截图不成功 !");
return false;
}
}
5.ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数)
/**
* ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数)
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
* @param prefix 前缀
* @param srcVideoPath 视频文件(原)
* @param middlefix 中间的字符转
* @param srcVideoPath 视频文件(转换后的路径)
* @param suffix 结束的字符串
* @return
*/
public static boolean processFfmpegShellScript(String prefix,String srcVideoPath,String middlefix,String tarVideoPath,String suffix) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在!");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-y");
commend.add("-i");
if(prefix != null && !prefix.equals("")){
commend.add(prefix);
}
commend.add(srcVideoPath);
if(middlefix != null && !middlefix.equals("")){
commend.add(middlefix);
}
commend.add(tarVideoPath);
if(suffix != null && !suffix.equals("")){
commend.add(suffix);
}
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.error("【" + tarVideoPath + "】 processFfmpegShellScript 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegShellScript 转换不成功 !");
return false;
}
}
6.ffmpeg转换成swf文件
/**
* ffmpeg转换成swf文件
* @param srcVideoPath
* @param tarVideoPath
* @return
*/
public static boolean processFfmpegSwf(String srcVideoPath,String tarVideoPath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】不存在!");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-y");
commend.add("-i");
commend.add(srcVideoPath);
commend.add("-b");
commend.add("360");
commend.add("-r");
commend.add("25");
commend.add("-s");
commend.add("640x480");
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-ac");
commend.add("1");
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.error("【" + tarVideoPath + "】 processFfmpegSwf 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】 processFfmpegSwf 转换不成功 !");
return false;
}
}
7.ffmpeg将其他格式转换成WEBM格式文件
/**
* ffmpeg将其他格式转换成WEBM格式文件
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
* @param srcVideoPath 视频文件(原)
* @param tarVideoPath 视频文件(新)
* @return
*/
public static boolean processFfmpegToWebm(String srcVideoPath,String tarVideoPath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add( "-y");
commend.add( "-i");
commend.add(srcVideoPath);
commend.add("-f");
commend.add("webm");
commend.add("-vcodec");
commend.add("libvpx");
commend.add("-acodec");
commend.add("libvorbis");
commend.add("-vb");
commend.add("1600000");
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.info(tarVideoPath + " is not exit! processFfmpegToWebm 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】processFfmpegToWebm 转换不成功 !");
return false;
}
}
8.ffmpeg将其他格式转换成WEBM格式文件(1)
/**
* ffmpeg将其他格式转换成WEBM格式文件
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
* @param srcVideoPath 视频文件(原)
* @param tarVideoPath 视频文件(新)
* @return
*/
public static boolean processFfmpegToOggOrOgv(String srcVideoPath,String tarVideoPath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpeg2theoraPath);
commend.add( "-V");
commend.add( "4000");
commend.add("-A");
commend.add("128");
commend.add(srcVideoPath);
commend.add("-o");
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");
return false;
}
}
9.ffmpeg将其他音频格式转换成ogg格式文件
/**
* ffmpeg将其他音频格式转换成ogg格式文件
* ffmpeg能解析的格式:(aac;ac3;au;wav;wma等)
* @param srcVideoPath 音频文件(原)
* @param tarVideoPath 音频文件(新)
* @return
*/
public static boolean processFfmpegToOgg(String srcVideoPath,String tarVideoPath) {
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("-acodec");
commend.add("libvorbis");
commend.add("-ab");
commend.add("64k");
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
doWaitFor(process);
process.destroy();
if (!checkfile(tarVideoPath)) {
logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");
return false;
}
return true;
} catch (Exception e) {
logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");
return false;
}
}