视频上传以转为FLV格式为宜,以下收藏了部分网上代码,以记录一下:
public class ConvertVideo {
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");
}
public static void convertToFLV(String projectPath, String inputFile,
String outputFile) {
INPUT_PATH = inputFile;
OUTPUT_PATH = outputFile;
PROJECT_PATH = projectPath;
if (checkContentType())
processFLV();// 直接将文件转为flv文件
}
private static boolean checkContentType() {
String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1,
INPUT_PATH.length()).toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
return "true".equals(fileType.get(type));
}
private static void processFLV() {
if (new File(INPUT_PATH).isFile()) {
try {
String cmd = "cmd /c start X:\\ffmpeg.bat \"" + PROJECT_PATH
+ "\" \"" + INPUT_PATH + "\" \"" + OUTPUT_PATH + "\"";
Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @Title: processFLV
* @Description: 视频转换
* @param @param ffmpegpath
* @param @param inputpath
* @param @param outputpath
* @param @return 参数
* @return boolean 返回
* @throws
*/
public static boolean processFLV(String ffmpegpath, String inputpath,
String outputpath) {
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegpath);
commend.add("-i");
commend.add(inputpath);
commend.add("-y");
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("300*200");
commend.add(outputpath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Title: processDVImage
* @Description: 视频截图
* @param @return 参数
* @return boolean 返回
* @throws
*/
public static boolean processDVImage(String ffmpegpath, String inputpath,
String outputpath)
{
List<String> commend = new java.util.ArrayList<String>();
commend.add(ffmpegpath);
commend.add("-i");
commend.add(inputpath);
commend.add("-y");
commend.add("-f");
commend.add("image2");
commend.add("-ss");
commend.add("8");
commend.add("-t");
commend.add("0.001");
commend.add("-s");
commend.add("350x240");
commend.add(outputpath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}