1. 程式人生 > >java 視訊格式轉換-ffmpeg

java 視訊格式轉換-ffmpeg

*需要配置ffmpeg檔案!!!
常用視訊格式都支援,只是轉換效率不高!

import java.util.ArrayList;
import java.util.List;

import com.crm.util.common.PropertyUtil;

public class ConvertVideo {
	//輸出視訊地址
	private static String inputPath = "";
        //輸出視訊地址(本地配置)
	private static String outputPath = PropertyUtil.VIDEO_STORAGE_LOCATION;
        //所在位置(本地配置)
	private static String ffmpegPath = PropertyUtil.VIDEO_FFMPEG_STORAGE_LOCATION;

	public static String process(String path, String name) {
		//轉換後返回檔名
		return processVedio(path,name);
	}

	// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
	private static String processVedio(String path, String name) {
		List<String> command = new ArrayList<String>();
		command.add(ffmpegPath + "ffmpeg");
		command.add("-i");
		command.add(path+"/"+name);//輸入檔案路徑
		command.add("-ab");
		command.add("56");
		command.add("-ar");
		command.add("22050");
		command.add("-qscale");
		command.add("8");
		command.add("-r");
		command.add("15");
		command.add("-s");
		command.add("720x480"); //解析度
		name = name.substring(0, name.lastIndexOf(".")) + ".mp4";
		path = path +"/"+ name ;
		command.add(path);  //輸入檔案路徑
		try {
			Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
			new PrintStream(videoProcess.getErrorStream()).start();
			new PrintStream(videoProcess.getInputStream()).start();
			videoProcess.waitFor();
			return name;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

class PrintStream extends Thread {
	java.io.InputStream __is = null;

	public PrintStream(java.io.InputStream is) {
		__is = is;
	}

	public void run() {
		try {
			while (this != null) {
				int _ch = __is.read();
				if (_ch != -1)
					System.out.print((char) _ch);
				else
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}