我们在用ffmpeg做视频上传功能时,经常会用到视频的转码和视频抓图的俩个功能,这里贴出俩个常用的工具类代码
1.ffmpeg视频转码,多数在做视频上传功能时,会将视频转为flv格式,因为flv体积小便于快速读取播放,这里以转FLV为例(attachmentContentType为视频类型,若转为其他类型只需更改该参数即可)代码逻辑这里不做的过多解释,直接上代码
/**
* 功能:将任意格式的视频转化为flv格式,有利于在线视频播放
* 前置条件:E盘下放有 ffmpeg.exe、ffplay.exep、threadGC2.dll(ffmpeg来自 ffmpeg.rev12665.7z)
* E盘下还需 mencoder.exe、drv43260.dll
* ps: ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等
* mencoder 解析剩下的格式:wmv9,rm,rmvb
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 功能函数
* @param inputFile 待处理视频,需带路径
* @param outputFile 处理后视频,需带路径
* @return
*/
@Service
@Transactional
public class ConvertFlv {
public static boolean convert(String inputFile, String outputFile, String ffmpeg, String mencoder, String video_temp_path, String attachmentContentType)
{
if (!checkfile(inputFile)) {
System.out.println(inputFile + " is not file");
return false;
}
if (process(inputFile,outputFile,ffmpeg,mencoder,video_temp_path,attachmentContentType)) {
System.out.println("ok");
return true;
}
return false;
}
//检查文件是否存在
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
/**
* 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
* @param inputFile
* @param outputFile
* @param ffmpeg
* @param mencoder
* @param video_temp_path
* @param attachmentContentType
* @return
*/
private static boolean process(String inputFile,String outputFile, String ffmpeg, String mencoder, String video_temp_path, String attachmentContentType) {
int type = checkContentType(attachmentContentType);
boolean status =