首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

上传视频用 ffmpeg 转换FLV 格式,出错。解决办法

2012-03-29 
上传视频用 ffmpeg转换FLV 格式,出错。private final static String PATH F:\\wywork\\Flashplay\\WebRo

上传视频用 ffmpeg 转换FLV 格式,出错。
private final static String PATH = "F:\\wywork\\Flashplay\\WebRoot\\FLV\\test3.RMVB"; //需要转换的视频路径
 
 public static void main(String[] args) {
  if(!checkfile(PATH)){ //判断是否存在这个视频
  System.out.println(PATH+" is not file");
  return;
  }  
  //调用转换视频的方法,判断是否转换成功
  if (process()) {  
  System.out.println("ok"); 
   
  }
 }
 
 private static boolean process() {  
  int type = checkContentType();
  boolean status = false;
  if (type==0) {
  status = processFLV(PATH);// 直接将文件转为flv文件  
  } else if (type==1) {
  String avifilepath = processAVI(type);
  if (avifilepath == null)
  return false;// avi文件没有得到
  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("F:\\playtest\\zhuanhuan\\mencoder");
  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("F:\\home\\a.avi");
   


  try{
  ProcessBuilder builder = new ProcessBuilder();
  builder.command(commend);
  builder.start();
  return "F:\\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+" is not file");
  return false;
  }  
   
  List<String> commend=new java.util.ArrayList<String>();
  commend.add("F:\\playtest\\zhuanhuan\\ffmpeg");
  commend.add("-i");
  commend.add(oldfilepath);
  commend.add("-ab");
  commend.add("64");
  commend.add("-acodec");
  //commend.add("mp3");
  commend.add("libmp3lame");
  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("F:\\home\\b.flv");
   
  try {
  ProcessBuilder builder = new ProcessBuilder();
  builder.command(commend);
  builder.start();
  return true;
  } catch (Exception e) {
  e.printStackTrace();
  return false;
  }
  }

上面是我在网上找个一个 转换视频格式的,但是我使用了下,他转换RMVB或其他格式的时候,转换成一个0KB的avi格式的,而不能转换FLV格式的。我不知道怎么回事,求各位高手指教。有点小急用,他的判断也成功了的,但是我在Flashplay 中没法播放
 

[解决办法]
首先你要搞清楚两种格式转换的算法,然后自己实现或者copy别人的。
如果copy的代码错误了,就杯具了,还不如自己写一个呢。
[解决办法]
我刚好昨天也做了一个这个功能。。
 你要同时使用二种转换的工具
你要实现的功能是想把所有上传的视频都转换成FLV格式的吧?
给我加分吧,我贴我昨天做的源码给你。。

热点排行