ffmpeg視頻格式轉化及flv格式截圖
- public static boolean process(String resourcePath) {
- int type = checkContentType(resourcePath);
- boolean status = false;
- if (type == 0) {
- status = processFLV(resourcePath);// 直接將文件轉爲flv文件
- }
- return status;
- }
- private static int checkContentType(String resourcePath) {
- String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
- resourcePath.length()).toLowerCase();
- // cuplayer.com 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;
- } else if (type.equals("mpeg")) {
- return 0;
- } else if (type.equals("mpe")) {
- return 0;
- }
- // cuplayer.com 對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能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- private static boolean processFLV(String resourcePath) {
- if (!checkfile(resourcePath)) {
- System.out.println(resourcePath + " is not file");
- return false;
- }
- // 文件命名
- Calendar c = Calendar.getInstance();
- String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
- List commend = new ArrayList();
- commend.add("e:\\ffmpeg");
- commend.add("-i");
- commend.add(resourcePath);
- // commend.add("-ab");//音頻流碼率:(默認是同源文件碼率)
- // commend.add("56");
- commend.add("-ar");//視頻流採樣率:(大多數情況下使用44100和48000,分別對用PAL和NTSC制式,根據需要選擇)
- commend.add("22050");
- commend.add("-qscale");//視頻量化指標
- commend.add("8");
- commend.add("-r");//視頻流幀數(一般來書PAL制式同常用25,ntsc制式通常用29)
- commend.add("15");
- commend.add("-s");//視頻解析度:(分辨率)可以自己定義所需要的大小: 改變視頻流的解析式很耗cpu的
- commend.add("600x500");
- commend.add("e:\\" + savename + ".flv");
- try {
- Runtime runtime = Runtime.getRuntime();
- Process proce = null;
- String cmd = "";
- String cut = " e://ffmpeg.exe -i "
- + resourcePath
- + " -y -f image2 -ss 8 -t 0.001 -s 600x500 e:\\"
- + savename + ".jpg";
- String cutCmd = cmd + cut;
- proce = runtime.exec(cutCmd);
- ProcessBuilder builder = new ProcessBuilder(commend);
- // builder.command(commend);
- builder.start();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public static void main(String[] args) {
- if (!checkfile("e:\\1-android_sdk.flv")) {
- System.out.println("" + " is not file");
- return;
- }
- if (process("e:\\1-android_sdk.flv")) {
- System.out.println("ok");
- }
- }