使用ffmpeg 视频格式转换&&截图

一、官网http://ffmpeg.org/index.html

二、代码:

1、工具类

package wuyechun.myweb.test;

import java.io.File;
import java.util.HashMap;

/*
 * @(#) ConvertVideo.java 1.0  2011-7-7 
 *
 * (c) Copyright 2011 iflytek MyWeb 1.0
 *
 * FILENAME    : ConvertVideo.java
 * PACKAGE     : wuyechun.myweb.test
 * CREATE DATE : 2011-7-7
 * AUTHOR      : wuyechun
 */

public class FfmpegUtil {
	private static String INPUT_PATH;
	private static String OUTPUT_PATH;
	private static String PROJECT_PATH;
	private static HashMap<String, String> fileType;

	static {
		fileType = new HashMap<String, String>();
		fileType.put("avi", "true");
		fileType.put("mpg", "true");
		fileType.put("wmv", "true");
		fileType.put("3gp", "true");
		fileType.put("mov", "true");
		fileType.put("mp4", "true");
		fileType.put("asf", "true");
		fileType.put("asx", "true");
		fileType.put("flv", "true");
	}

	/***
	 * 
	 * 功能 :将视频转换成Flv格式&&截图
	 * 
	 * 开发:wuyechun 2011-7-7
	 * 
	 * @param projectPath
	 *            ffmpeg存放目录
	 * @param inputFile
	 *            需要转换的文件目录
	 * @param outputFile
	 *            Flv输出目录
	 */
	public static void convertToFLV(String projectPath, String inputFile,
			String outputFile) {
		INPUT_PATH = inputFile;
		OUTPUT_PATH = outputFile;
		PROJECT_PATH = projectPath;

		if (checkContentType())
			processFLV(); // 转换成Flv
	}

	public static void screenShot(String projectPath, String inputFile,
			String outputFile) {
		INPUT_PATH = inputFile;
		OUTPUT_PATH = outputFile;
		PROJECT_PATH = projectPath;

		if (checkContentType())
			processIMG(); // 截图
	}

	/***
	 * 
	 * 功能 :检查视频是否为可支持的格式(ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等))
	 * 
	 * 开发:wuyechun 2011-7-22
	 * 
	 */

	private static boolean checkContentType() {
		String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1,
				INPUT_PATH.length()).toLowerCase();
		return "true".equals(fileType.get(type));
	}

	private static void processFLV() {
		if (new File(INPUT_PATH).isFile()) {
			try {

				// 视频转换
				String cmd = "cmd /c start D:\\ffmpeg.bat \"" + PROJECT_PATH
						+ "\" \"" + INPUT_PATH + "\" \"" + OUTPUT_PATH + "\"";

				Runtime.getRuntime().exec(cmd);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void processIMG() {
		if (new File(INPUT_PATH).isFile()) {
			try {
				// 截图
				String cmd = "cmd /c start D:\\ffmpeg-img.bat \""
						+ PROJECT_PATH + "\" \"" + INPUT_PATH + "\" \""
						+ OUTPUT_PATH + "\"";
				Runtime.getRuntime().exec(cmd);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

 

2、测试类

/*
 * @(#) Ffmpeg.java 1.0  2011-7-7 
 *
 * (c) Copyright 2011 iflytek MyWeb 1.0
 *
 * FILENAME    : Ffmpeg.java
 * PACKAGE     : wuyechun.myweb.test
 * CREATE DATE : 2011-7-7
 * AUTHOR      : Administrator
 */

package wuyechun.myweb.test;

import wuyechun.myweb.test.FfmpegUtil;

/**
 * @author wuyechun
 * 
 */
public class Ffmpeg {
	public static void main(String[] args) {

		ffmpegService();

	}

	public static void ffmpegService() {
	FfmpegUtil.convertToFLV("C:\\Eclipse\\workspace\\corejava\\src",
				"d:\\a.wmv", "d:\\d.flv");
	FfmpegUtil.screenShot("C:\\Eclipse\\workspace\\corejava\\src",
				"d:\\a.wmv", "d:\\d.jpg");
	};

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值