一、官网: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");
};
}