视频帧截取,处理类(ffmpeg.exe)
import java.io.IOException;
/**
* 文件信息处理公共类
*
* @author com.fhy.xyb
* @date 2014-6-30 上午09:08:29
*
*
*/
public class FileUtil {
/**
* 获取WEB-INF目录下面的文件路径
*
*@author com.fhy.xyb
*@createTime 上午09:14:42
*@createTime 上午09:14:42
*/
public String getFilePath(String fileName){
//获取WEB-INF全路径
String filepath = Thread.currentThread().getContextClassLoader().getResource("").toString();
//将/换成\
filepath = filepath.replace("/", "\\");
//去掉file
filepath = filepath.replace("file:", "");
//去掉class\
filepath = filepath.replace("classes\\", "");
//去掉第一个\
filepath = filepath.substring(1);
//得到文件全路径
filepath = filepath+fileName;
return filepath;
}
/**
* 视频帧截取处理方法
* ffmpeg.exe文件限定WEB-INF下
*@author com.fhy.xyb
*@createTime 上午11:35:53
*@createTime 上午11:35:53
*/
@SuppressWarnings("unused")
public boolean createVideoFrame(String videoLocation, String imageLocation,String fileName,long playLength) {
FileUtil file = new FileUtil();
//调用runtime使应用程序能够与其运行的环境相连接
Runtime runtime = Runtime.getRuntime();
String cmd = "";
Process proce = null;
//拼接cmd命令
String cut = " "+ file.getFilePath(fileName)+ " -i "+ videoLocation
+ " -y -f image2 -ss " + playLength/2/1000 + " -t 0.001 -s 352x240 " + imageLocation;
System.out.println(cut);
String cutCmd = cmd + cut;
try {
proce = runtime.exec(cutCmd);
return true;
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
}
}
获取视频信息
Encoder encoder = new Encoder();
File fileServiceUlrl = new File(path);
try {
System.out.println(fileServiceUlrl.length());
MultimediaInfo m = encoder.getInfo(fileServiceUlrl);
AudioInfo info = m.getAudio();
material.setLength((int) m.getDuration());
material.setCodedFormat(info.getDecoder());
material.setSize((int) file.getSize());
material.setBitRate(Integer.toString(info.getBitRate()));
material.setSamplingRate(Integer.toString(info.getSamplingRate()));
} catch (InputFormatException e) {
System.out.println("读取文件失败");
e.printStackTrace();
} catch (EncoderException e) {
System.out.println("编码错误");
e.printStackTrace();
}