采用s2sh设计。转换器使用的是FormatFactory软件。这里以视频转flv作为例子。
工具类ConvertVideoUtil如下:
package org.forever.convertVideo.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 视频转换工具类
*
* @author 陈均
*/
public class ConvertVideoUtil {
private String formatFactoryPath;
public static String FORMATFACTORYPATH = "#formatFactoryPath#";
public static String TYPE = "#type#";
public static String RESOLUTION = "#resolution#";
public static String resolutionDefault = "320x240";
public static String SOURCEFILEPATH = "#sourceFilePath#";
//private String cmdTemplate = "#formatFactoryPath# \"All to #type#\" \"#resolution#\" \"#sourceFilePath#\"";// 命令模板语句
public String allToType(String resolution, String sourceFileDir,
String fileName,String type) throws Exception {
Runtime rt = Runtime.getRuntime();
String typeDir = "All to " + type + "\\\\";
String convertFileName = fileName.split("\\.")[0] + "." + type;
String cmd = formatFactoryPath+" \"All to "+type+"\" \""+resolution+"\" \""+sourceFileDir + fileName+"\"";
System.out.println(cmd);
Process process = rt.exec(cmd);
process.waitFor();
process.destroy();
String src = sourceFileDir + typeDir + convertFileName;
String target = sourceFileDir + convertFileName;
File srcFile = new File(src);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int bytes_read = 0;
try {
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
} catch (Exception e) {
throw new Exception(e);
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
throw new Exception(e);
}
}
if (srcFile.exists())
srcFile.delete();
return target;
}
public String getFormatFactoryPath() {
return formatFactoryPath;
}
public void setFormatFactoryPath(String formatFactoryPath) {
this.formatFactoryPath = formatFactoryPath;
}
}
附上完整代码。