之前作的一個基於ffmpeg的視頻格式轉換的程序,如今抽空整理一下,不少地方都是從別的大神那借鑑的,只是把本身的以爲有用的,對別人有幫助的拿出來分享分享,下面是代碼java
package video;app
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.DefaultFFMPEGLocator;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.FFMPEGLocator;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;ide
import java.io.File;
import java.util.List;函數
public class ConvertVideo {
private static File inFolder;//須要轉換的文件夾
private static File outFolder;//保存avi文件的文件夾地址
private static File inFile;//每一個文件的file對象
private static String name;//文件名稱,用在保存avi文件,name.avi,不帶擴展名的
private static String Name;//文件名稱,(帶擴展名的)
public ConvertVideo(File inFolder,File outFolder){//構造函數,進行測試時傳入兩個文件夾地址
this.inFolder=inFolder;
this.outFolder=outFolder;
}
public static void Test(){
String[] fileList=inFolder.list();
for(int i=0;i<fileList.length;i++){
inFile=new File(inFolder.getAbsolutePath()+"\\"+fileList[i]);
Name=inFile.getName();
name=getFileNameNoEx(Name);//得到沒有擴展名的文件名
System.out.println(name);
File txtFile=new File(inFolder.getAbsolutePath()+"\\"+name+".txt");//視頻上傳完畢會生成一個同名的txt文件
if(txtFile.exists()){//判斷是否上傳完畢
convert();
}
}
}
/**
* 截取不帶擴展名的文件名
* @param : String
* @return : String
*
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
/**
* 視頻轉換調用函數
*@return : void
*
*/
public static void convert() {
if(!checkfile(inFile)){
System.out.println(inFile+" is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
/**
*視頻轉換函數
* @return : boolean
*
*
*/
private static boolean process() {
int type = checkContentType();
boolean status = false;
System.out.println("name="+name+" "+"Name="+Name+" "+"type="+type);
if (type==0) {
status = processFLV(inFile);// 直接將文件轉爲avi文件
} else if (type==1) {
processAVI(inFile);
status=true;
}else if(type==2){
System.out.println("你的文件已是avi格式,無須轉換!");
}
return status;
}
/**
*檢查視頻類型
* @param : void
* @return : int
*
*/
private static int checkContentType() {
String path=inFile.getName();//path是絕對路徑
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 2;
} 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("mkv")){
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;
}
/**
* 檢查文件是否是File對象
* @param : File
* @return : boolean
*
*/
private static boolean checkfile(File path){
if(!path.isFile()){
return false;
}
return true;
}
/**
*對於ffmpeg沒法解析的wmv9,rm,rmvb視頻類型,利用mencoder進行解析,menconder視頻轉換
* @param : File
* @return : String
*
*
*/
private static String processAVI(File inFile) {
String inpath=inFile.getAbsolutePath();//PATH爲file對象,先得到絕對路徑,賦值給path
String outpath =outFolder.getAbsolutePath()+"\\"+name+".avi";//爲輸出的絕對路徑
File Test=new File(outpath);
if(Test.exists()){
System.out.println("視頻已經轉換,無須再次轉換!");
return null;
}else{
List<String> commend=new java.util.ArrayList<String>();
commend.add("e:\\mencoder");
commend.add(inpath);
commend.add("-o");
commend.add(outpath);
commend.add("-oac");
commend.add("mp3lame");
commend.add("-lameopts");
commend.add("cbr:br=32");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=1024");
commend.add("-vf");
commend.add("scale=720:-3");
//commend.add("-o");
StringBuffer sb=new StringBuffer();
for(int i=0;i<commend.size();i++){
sb.append(commend.get(i)+" ");
}
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "ok";
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
/**
*
* 對於ffmpeg能夠解析的格式(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) ,用ffmpeg進行解析,ffmpeg視頻轉換
* @param : File
* @return : loolean
*
*/
private static boolean processFLV(File oldfilepath) {
FFMPEGLocator locator = new FFMPEGLocator() {
@Override
protected String getFFMPEGExecutablePath() {
// TODO Auto-generated method stub
return "c://ffmpeg//ffmpeg.exe";
}
};
File source = oldfilepath;
String out=outFolder.getAbsolutePath()+"\\"+name+".avi";//輸出的絕對路徑
File target=new File(out);
//GetVideoInfo getVideoInfo = new GetVideoInfo();
//String decoder=getVideoInfo.getDecoder(source);
if(target.exists()/*||decoder.equals("h264")*/){
System.out.println("視頻無需進行轉換!");
return false;
}else{
AudioAttributes audio = new AudioAttributes();//設置音頻屬性
audio.setCodec("libmp3lame");//音頻使用libmp3lame編碼
audio.setBitRate(new Integer(128000));//設置比特率
audio.setChannels(new Integer(1));//設置通道,缺省爲1
audio.setSamplingRate(new Integer(22050));//
VideoAttributes video = new VideoAttributes();//設置視頻屬性
video.setCodec("mpeg4");//設置編碼格式
video.setBitRate(new Integer(1024000));//設置比特率
video.setFrameRate(new Integer(10));//設置
/*getVideoSize getvideoSize=new getVideoSize();
VideoInfo videoInfo=new VideoInfo();
videoInfo=getvideoSize.getVideoInfo(source);
VideoSize videoSize=videoInfo.getSize();
if(videoSize.getHeight()!=1280||videoSize.getWidth()!=400){
ResourceBundle resource=ResourceBundle.getBundle("parameter",Locale.getDefault());
String a=resource.getString("height");
String b=resource.getString("width");
int height=Integer.valueOf(a);
int width=Integer.valueOf(b);
video.setSize(new VideoSize(width,height));//設置大小
}*/
//setSize set=new setSize();
//set.set(video,source);
video.setSize(new VideoSize(1080,720));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("avi");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder(locator);
try {
encoder.encode(source, target, attrs);
return true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (InputFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (EncoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
}工具
這是一個簡單的視頻處理的方法,輕噴測試