兼容Linux和Windows下获取视频截图

package com.hncy.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

/**
* @author Bashan
* FFMPEG homepage http://ffmpeg.org/about.html
* By Google Get first and last thumb of a video using Java and FFMpeg
* From http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html
*/

public class VideoThumbTaker
{
protected String ffmpegApp;

public VideoThumbTaker(String ffmpegApp)
{
this.ffmpegApp = ffmpegApp;
}

@SuppressWarnings("unused")
/****
* 获取指定时间内的图片
* @param videoFilename:视频路径
* @param thumbFilename:图片保存路径
* @param width:图片长
* @param height:图片宽
* @param hour:指定时
* @param min:指定分
* @param sec:指定秒
* @throws IOException
* @throws InterruptedException
*/
public void getThumbWindows(String videoFilename, String thumbFilename, int width,
int height, int hour, int min, float sec) throws IOException,
InterruptedException
{
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y",
"-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min
+ ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
"-an", thumbFilename);

Process process = processBuilder.start();

InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
;
process.waitFor();

if(br != null)
br.close();
if(isr != null)
isr.close();
if(stderr != null)
stderr.close();
}

public static boolean getThumbLinux(String inFile, String outFile,String startTime) {
String command = "ffmpeg -i " + inFile
+ " -y -f image2 -ss "+startTime+" -t 00:00:01 -s 480x450 "
+ outFile;
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
} catch (Throwable t) {
t.printStackTrace();
return false;
}
return true;
}

 

这儿有两个方法,一个是获取Linux一个是获取Windows的

****************************************************************

当我的视频上传结束后

//结束前保存改视频的截图
if(fileType.equals("3gp")&&fileType.equals("mp4")){

//判断是否为视频
if(osType.equals("windows")){
//这个地方获取到我ffmpeg.exe所在的位置
VideoThumbTaker videoThumbTaker = new VideoThumbTaker("E:\\ffmpeg\\ffmpeg.exe");
try
{
String videoFilePath = "E:\\uploadfiles\\"+mac+"\\"+userId+"\\"+DateFormatUtils.format(System.currentTimeMillis(),"yyyyMMddHHmmsss")
+ RandomStringUtils.randomAlphabetic(5);
String thumbFilename = videoFilePath+".jpg";
videoThumbTaker.getThumbWindows(videoFilePath, thumbFilename,150, 100, 0, 0, 1);
System.out.println("over");
} catch (Exception e)
{
e.printStackTrace();
}
}else if(osType.equals("linux")){

String inFile =baseFileFolder +"/"+ mac
+ "/"
+ userId
+ "/"
+ DateFormatUtils.format(
System.currentTimeMillis(),
"yyyyMMddHHmmsss")
+ RandomStringUtils.randomAlphabetic(5);

String outFile =inFile+".jpg";
boolean transfer = VideoThumbTaker.getThumbLinux(inFile, outFile,"0");
System.out.print(transfer);
}

 

获取视频的截图,上传到相应文件夹,当然,我在之前就写好了一个获取系统信息的函数,用来判断现在所处的环境

posted @ 2017-04-13 21:04  handbang  阅读(1158)  评论(0编辑  收藏  举报