linux下使用ffmpeg將flv、mp4、rmvb轉換爲libx264的mp4

1.示例

       ffmpeg -i  testrmvb.rmvb  -c:v libx264 -strict -2 testrmvb.mp4

2.java批量轉換

  

public static void dirGoThrough(){
File baseDir=new File("/home/wwwroot/www/shs/upload/file/");//遍歷的文件夾
File[] fs = baseDir.listFiles();
int total = 0;
int mp4total = 0;
        if(fs!=null){
	        for(File dir:fs){
	        	File[] files = dir.listFiles();
	        	for(File f:files){
	        		if(f.isFile()){
	        			if(f.getName().toLowerCase().endsWith(".flv")||f.getName().toLowerCase().endsWith(".mp4")||
	        					f.getName().toLowerCase().endsWith(".rmvb")){
	        				boolean flag = ffmpegTransfer(f.getAbsolutePath());
	        				if(!flag){
	        					LogWriteDao.WriteLog(f.getAbsolutePath(), "transfererror", "/pdf/log/");
	        				}
	        				total++;
	        			}
//	        			if(f.getName().toLowerCase().endsWith(".mp4")){
//	        				mp4total++;
//	        			}
	        		}
	        		
	        	}
	        }
        }
        System.out.println("總數量:"+total+"--mp4的總數量:"+mp4total);
        LogWriteDao.WriteLog("total:"+total, "transferfinished", "/pdf/log/");
	}
	
	public static boolean ffmpegTransfer(String infile) {  
		String outfile = infile.replace(".mp4", "_libx264.mp4").replace(".flv", "_libx264.mp4")
				.replace(".rmvb", "_libx264.mp4");
		String avitoflv = "ffmpeg -i "+infile+" -c:v libx264 -strict -2 "+outfile;  
		try {  
			Runtime rt = Runtime.getRuntime();  
			Process proc = rt.exec(new String[]{"sh","-c",avitoflv});  
			InputStream stderr = proc.getErrorStream();  
			InputStreamReader isr = new InputStreamReader(stderr);  
			BufferedReader br = new BufferedReader(isr);  
			String line = null;  
			
			while ( (line = br.readLine()) != null)  
				System.out.println(line);  
	            int exitVal = proc.waitFor();  
	            System.out.println("Process exitValue: " + exitVal);  
		} catch (Throwable t) {  
			t.printStackTrace();  
			return false;  
		}  
		return true;  
	} 

       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章