windows下java使用ffmpeg截取图片

package com.blue.common.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
* @author lin
* FFMPEG
* By Google Get first and last thumb of a video using Java and FFMpeg
* From
*/

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 getThumb(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();
}
/**
* linux下截取第一帧
* @param inFile
* @param outFile
* @return
*/

public static boolean transfer(String inFile, String outFile) {
String command = "ffmpeg -i " + inFile + " -y -f image2 -ss 00:00:10 -t 00:00:01 -s 176x144 " + 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;
}

public static void main(String[] args)
{
VideoThumbTaker videoThumbTaker = new VideoThumbTaker("F:\\toutiao2\\wemedia\\src\\main\\resources\\ffmpeg\\ffmpeg.exe");
try
{
// videoThumbTaker.convertCommand("d:/f2d3451313514914a1c306de724b2792.mp4", "m.mp4");
System.out.println("截取完毕!");
} catch (Exception e)
{
e.printStackTrace();
}
}


}

posted @ 2018-01-05 10:25  coding_forever  阅读(1206)  评论(0)    收藏  举报