import java.io.File;
import java.util.List;public class Demo { private final static String PATH = "c:\\test\\ceshi.rmvb"; public static void main(String[] args) {
if (!checkfile(PATH)) {
System.out.println(PATH + " 没有找到文件");
return;
}
if (process()) {
System.out.println("ok");
}
} private static boolean process() {
int type = checkContentType();
boolean status = false;
if (type == 0) {
System.out.println("执行0");
status = processFLV(PATH);// 直接将文件转为flv文件
} else if (type == 1) {
System.out.println("执行1");
String avifilepath = processAVI(type);
if (avifilepath == null) {
return false;// avi文件没有得到
} else {
System.out.println("执行转换flv");
status = processFLV(avifilepath);// 将avi转为flv
}
}
return status;
} private static int checkContentType() {
String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.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无法解析的文件格式(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无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend = new java.util.ArrayList<String>();
commend.add("E:\\1111111\\mencoder.exe");
commend.add(PATH);
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("c:\\home\\a.avi"); try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "c:\\home\\a.avi";
} catch (Exception e) {
e.printStackTrace();
return null;
}
} // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String oldfilepath) { if (!checkfile(PATH)) {
System.out.println(oldfilepath + " 没有找到文件");
return false;
} List<String> commend = new java.util.ArrayList<String>();
commend.add("D:\\FlvTools\\ffmpeg.exe");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ab");
commend.add("64");
commend.add("-acodec");
commend.add("mp3");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("230");
commend.add("-r");
commend.add("24");
commend.add("-y");
commend.add("c:\\home\\a.flv");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}}
如果转换ffmpeg不能解析的格式视频,程序会先调用mencoder把视频转化成avi的格式,再用ffmpeg将avi格式转成flv格式,现在的问题就是mencoder转换完以后ffmpeg不执行转换(mencoder可以生成一个a.avi);程序的运行也没有什么问题,这个问题到底出在哪了??是不是多线程的问题?
如果直接转换ffmpeg可以解析的格式,ffmpeg会直接转换成flv,这里的转换没问题;

解决方案 »

  1.   

    啊!!和我的问题一摸一样一摸一样!!!!
    关注ing!!!
      

  2.   

    我现在已经放弃了wmv9,rm,rmvb对这三种格式的转换了
    删掉了mencoder 只转ffmpeg能转的格式~
    因为时间紧迫 俺的毕设快完不成了 555
      

  3.   

    mencoder 一样可以转换成flash文件
      

  4.   

    楼主的这个代码其实是不完整的;
    需要process的输入输出流进行检测;
    ffmpege这一个模块需要添加
     Process pc=builder.start();
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pc.getInputStream()));
                    while ((s = bufferedReader.readLine()) != null) {
    System.out.println("~~~~~~~~~~");
    System.out.println(s);

    pc.waitFor();
    而且在mencoder命令行是错误的正确的命令行:
    x:\\xx\\mencoder input.rmvb -o output.rmvb -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames mp3 -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3
    你用List存的话
    但是 mencoder的process检测有问题;
    问题出现在当mencoder在进行:Opening video decoder: [realvid] RealVideo decoder 操作时候载入DLL文件失败 process就不检测不了。正在解决中。。