现在的位置: 首页 > 综合 > 正文

将视频文件转为flv文件

2013年12月05日 ⁄ 综合 ⁄ 共 4412字 ⁄ 字号 评论关闭

package mature;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

public class ConvertVideo {

 private final static String S_PATH = "C://input//hunan.rm";
 
 public ConvertVideo() {
  super();
  // TODO Auto-generated constructor stub
 }

 private static boolean isFileExist(String path) {
  File file = new File(path);
  if (!file.isFile()) {
   return false;
  }
  return true;
 }
 
 
 /**将视频文件转为flv文件*/
 private static boolean process(){
  int type = checkFileType();
  boolean statu = false;
  if(type==0){
   /**直接将文件转为flv文件*/
   statu = convertVideoFLV(S_PATH);
  }else if(type==1){
   String aviFilePath = convertVideoAVI(type);
   if(aviFilePath==null || aviFilePath.trim().equals("")){
    return false;
   }
   statu = convertVideoFLV(aviFilePath);
  }
  return statu;
 }
 

   /** 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.*/
 private static String convertVideoAVI(int type) {
  List commend = new ArrayList();    
  commend.add("c://input//mencoder");  
  commend.add(S_PATH);
  commend.add("-srate");
  commend.add("22050");
  commend.add("-vf");
  commend.add("scale=208:-3");
  commend.add("-ofps");
  commend.add("12");
  commend.add("-oac");
  commend.add("mp3lame");
  commend.add("-lameopts");
  commend.add("mode=3:cbr:br=24");
  commend.add("-ovc");
  commend.add("lavc");
  commend.add("-ffourcc");
  commend.add("DX50");
  commend.add("-lavcopts");
  commend.add("vcodec=mpeg4:vhq:vbitrate=112");
  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://input//b.avi");

       try {
         Process process = null;
   ProcessBuilder builder = new ProcessBuilder();
   builder.command(commend);
   //process = Runtime.getRuntime().exec(commend.toString());
   process = builder.start();
   
   BufferedReader reader =new BufferedReader(new InputStreamReader(process.getInputStream()));
   String dLine = "";
   while((dLine = reader.readLine()) != null){
   
   }
   process.waitFor();
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
  return "c://input//b.avi";
 }

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

  if (!isFileExist(S_PATH)) {
   System.out.println(oldfilepath + " is not file");
   return false;
  }

        /**文件命名*/
  Calendar calendar = Calendar.getInstance();  
  String saveFileName = String.valueOf(calendar.getTimeInMillis())+ Math.round(Math.random() * 100000);
  
  List commend = new ArrayList();  
  commend.add("c://input//ffmpeg");
  commend.add("-i");
  commend.add(oldfilepath);
  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("c://output//"+saveFileName+".flv");
  
  
  try {
   Process proce = null; 
   Runtime runtime = Runtime.getRuntime();
   String cmd = "";   
   String cut = "c://input//ffmpeg.exe   -i   "+ oldfilepath+ "   -y   -f   image2   -ss   8   -t   0.001   -s   600x500   c://output//"+ saveFileName + ".jpg";  
   String cutCmd = cmd + cut;   
   proce = runtime.exec(cutCmd);   
   ProcessBuilder builder = new ProcessBuilder(commend);    
   builder.start();    
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**检查文件类型*/
 private static int checkFileType(){
  String type = S_PATH.substring(S_PATH.lastIndexOf(".")+1,S_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;
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  if (!isFileExist(S_PATH)) {
   System.out.println(S_PATH + " is not file");
   return;
  }
  if (process()) {
   System.out.println("ok");
  }
 }

 
}

抱歉!评论已关闭.