网页功能: 加入收藏 设为首页 网站搜索  
java调用视频转换工具ffmpeg
发表日期:2009-09-07作者:[转贴] 出处:  

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TestVedio {
public static boolean process(String resourcePath) {
int type = checkContentType(resourcePath);
boolean status = false;
if (type == 0) {
status = processFLV(resourcePath);// 直接将文件转为flv文件
}
return status;
}

private static int checkContentType(String resourcePath) {
String type = resourcePath.substring(resourcePath.lastIndexOf(
".") + 1,
resourcePath.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;
} else if (type.equals(
"mpeg")) {
return 0;
} else if (type.equals(
"mpe")) {
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;
}

private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}

// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String resourcePath) {

if (!checkfile(resourcePath)) {
System.out.println(resourcePath +
"   is   not   file");
return false;
}
// 文件命名
Calendar c = Calendar.getInstance();
String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
List commend = new ArrayList();
commend.add(
"e:\\ffmpeg");
commend.add(
"-i");
commend.add(resourcePath);
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(
"e:\\" + savename + ".flv");
try {
Runtime runtime = Runtime.getRuntime();
Process proce = null;
String cmd =
"";
String cut =
"     e://ffmpeg.exe   -i   "
+ resourcePath
+
"   -y   -f   image2   -ss   8   -t   0.001   -s   600x500   e:\\"
+ savename +
".jpg";
String cutCmd = cmd + cut;
proce = runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
// builder.command(commend);
builder.start();

return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static void main(String[] args) {
if (!checkfile(
"e:\\hibernate1.avi")) {
System.out.println(
"" + "   is   not   file");
return;
}
if (process(
"e:\\hibernate1.avi")) {
System.out.println(
"ok");
}
}
}


import java.io.IOException;

public class TestMain {

/**
* @param args
*/

public static void main(String[] args) {
String videoRealPath =
"e:\\video.flv";
String imageRealPath =
"e:\\video.jpg";
String videoPath =
"e:\\videotes.flv";
try {
Runtime.getRuntime().exec(
"cmd /c start e:\\ffmpeg.bat " + videoRealPath + ","+ imageRealPath+","+ videoPath);
} catch (IOException e) {
e.printStackTrace();
}
}

}

ffmpeg.bat文件内容:
ffmpeg.exe -i %1 -ss 3 -vframes 1 -r 1 -ac 1 -ab 2 -s 160*120 -f image2 %2

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 java调用视频转换工具ffmpeg
本类热点文章
  Java读取文件中含有中文的解决办法
  Java读取文件中含有中文的解决办法
  简单加密/解密方法包装, 含encode(),de..
  EJB 3.0规范全新体验
  java简单的获取windows系统网卡mac地址
  让Java程序带着JRE一起上路
  抢先体验"野马"J2SE6.0
  Java连接各种数据库的实例
  Java连接各种数据库的实例
  JAVA的XML编程实例解析
  Java学习从入门到精通(附FAQ)
  新手必读:Java学习的捷径
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.01127