JAVA 利用 ffmpeg 和 mencoder 对视频进行转换

主要对转换成FLV 格式的视频文件

package com.hd.hse.util;

import java.io.File;
import java.util.List;

/**
 * 
 * @author LH
 *  2011-06-23
 */
public class Video {

	/**
	 * @param args测试
	 */
	public static void main(String[] args) {
		 
		String path = "D:\\肯德基KFC孜然烤鸡腿堡之师傅下山篇视频广告.wmv";  
		//转换之前文件的路径
		
		String outFlvPath = "E:\\750c2eba-0162-4713-b906-cbd8e9acd3cc.flv"; 
		// 转换后文件的路径 同时 定义好 名称
		
		String outAviPath = "E:\\肯德基KFC孜然烤鸡腿堡之师傅下山篇视频广告.avi"; 
		// 类型为不识别的格式文件  先转AVI 格式 然后再转FLV  定义一个AVI 名称文件
		
		String realPath = "E:\\video\\" ;
		//  ffmpeg.exe 与  mencoder.exe  FLV 解码器存放路径 
		
		boolean a = checkfile(path);
		System.out.println(a);

		boolean b = process(path, outFlvPath,outAviPath,realPath);
		System.out.println(b);

		System.out.println("ok");

	}

	/**
	 * 判断文件存在与否
	 * 
	 * @param path
	 * @return
	 */
	public static boolean checkfile(String path) {
		File file = new File(path);
		if (!file.isFile()) {
			return false;
		}
		return true;
	}

	/**
	 * 转换
	 * 
	 * @return
	 */
	public static boolean process(String path, String outflvPath,String outAviPath,String realPath) {
		int type = checkContentType(path);

		boolean status = true;
		if (type == 0) {
		
			processFLV(path, outflvPath,realPath);// 如果为零表示ffmpeg可以识别,直接将文件转为flv文件
			
		} else if (type == 1) {
			String avifilepath = processAVI(type, path,outAviPath,realPath);

			if (avifilepath == null)
				return false;// avi文件没有得到
			status = processFLV(avifilepath, outflvPath,realPath);// 将avi转为flv

			System.out.println("status====" + status);
		}
		return status;
	}

	/**
	 * 判断文件格式
	 * 
	 * @return
	 */
	private static int checkContentType(String 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 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("vob")) {
			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;
	}

	/**
	 * 转换为flv
	 * 
	 * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
	 * 
	 * @param oldfilepath
	 * @return
	 */
	private static boolean processFLV(String filepath, String outflvPath,String realPath) {

		// 检查文件是否存在
		if (!checkfile(filepath)) {
			System.out.println(filepath + " is not file");
			return false;
		}
		List<String> commend = new java.util.ArrayList<String>();
		commend.add(realPath+"ffmpeg.exe");
		
		commend.add("-sameq");
		commend.add("-i");
		commend.add(filepath);
		commend.add("-ab");
		commend.add("64");
		// commend.add(" -acodec ");
		// commend.add("codec");
		commend.add("-ac");
		commend.add("2");
		commend.add("-ar");
		commend.add("22050");
		commend.add("-qscale");
		commend.add("4");
		// 清晰度 -qscale 4 为最好可是文件大, -qscale 6就可以了
		commend.add("-b");
		commend.add("600");
		// commend.add("-s");
		// commend.add("350x240");
		commend.add("-r");
		commend.add("29.97");
		commend.add("-y");
		commend.add(outflvPath);
		System.out.println(commend);
		System.out.println("----");
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commend);
			builder.start();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 无法解析的 先转换为avi
	 * 
	 * 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可
	 * 以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
	 *
	 * @param type
	 * @return
	 */
	private static String processAVI(int type, String path,String outaviPath,String realPath) {
		
		List<String> commend = new java.util.ArrayList<String>();
		commend.add(realPath+"mencoder.exe");
		commend.add(path);
		// 音频采用mp3编码
		commend.add("-oac");
		commend.add("mp3lame");
		// 采用高质DivX视频编码,视频码率为112kbps
		commend.add("-ovc");
		commend.add("lavc");
		commend.add("-lavcopts");
		commend
				.add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");
		commend.add("-lameopts");
		commend.add("abr:br=56");
		// 声音采样频率设置,现为22K
		commend.add("-srate");
		commend.add("22050");
		// -sws就是用来设置品质的,默认值为2
		commend.add("-sws");
		commend.add("3");
		// 宽度为208,高度自动调整保持比例;
		// -vf scale=-3:176宽度自动调整保持比例,高度为176;如果想保持原来的大小可以不要这个参数
		commend.add("-vf");
		commend.add("scale=512:-3");
		// 帧速率设置
		commend.add("-ofps");
		commend.add("18");
		/*
		 * mode=3:cbr:br=24单声道 音频码率为24kbps;-lameopts
		 * mode=0:cbr:br=24立体声,音频码率为24kbps; 还可设置音量,-lameopts
		 * mode=3:cbr:br=32:vol=1,设置范置为1~10,但不宜设得太高
		 */
		commend.add("-lameopts");
		commend.add("vbr=3:br=128");
		commend.add("-o");
		commend.add(outaviPath);
		// 控制台显示执行的命令
		// System.out.println(commend);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commend);
			builder.start();
			
			return outaviPath;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

 

你可能感兴趣的:(java,vb)