通過 ffmpeg 獲取視頻第一幀(指定時間)圖片


最近做一個上傳教學視頻的方法,上傳視頻的同時需要上傳視頻縮略圖,為了避免用戶上傳的縮略圖與視頻內容不符,經理要求直接從上傳的視頻中截圖視頻的某一幀作為縮略圖,並給我推薦了FFMPEG。FFMPEG 功能很強大,做視頻必備的軟件。

FFMPEG下載地址:https://ffmpeg.org/download.html

1、VideoThumbTaker.java 獲取視頻指定播放時間的圖片

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

/**
* 2017-08-10
* @author szq
*
*/

public class GrabberVideoThumb
{
protected String ffmpegApp;

public GrabberVideoThumb(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();
}

public static void main(String[] args)
{
GrabberVideoThumb videoThumbTaker = new GrabberVideoThumb("F:\\ffmpeg\\bin\\ffmpeg.exe");
try
{
videoThumbTaker.getThumb("f:/02-MFS介紹及特性說明_rec.mp4", "F:\\test.png", 800, 600, 0, 0, 9);
System.out.println("over");
} catch (Exception e)
{
e.printStackTrace();
}
}
}

2、GrabberVideoFirstThumb.java 獲取第一幀圖片

import java.io.IOException;

/***
*
* 得到第一秒(也是第一幀)圖片
*/
public class GrabberVideoFirstThumb extends GrabberVideoThumb
{
public VideoFirstThumbTaker(String ffmpegApp)
{
super(ffmpegApp);
}

public void getThumb(String videoFilename, String thumbFilename, int width,
int height) throws IOException, InterruptedException
{
super.getThumb(videoFilename, thumbFilename, width, height, 0, 0, 1);
}
}

3、GrabberVideoLastThumb.java 獲取最后一幀圖片

/**
* 得到最后一秒(也是最后一幀)圖片
*/

public class GrabberVideoLastThumb extends GrabberVideoThumb
{
public GrabberVideoLastThumb(String ffmpegApp)
{
super(ffmpegApp);
}

public void getThumb(String videoFilename, String thumbFilename, int width,
int height) throws IOException, InterruptedException
{
VideoInfo videoInfo = new VideoInfo(ffmpegApp);
videoInfo.getInfo(videoFilename);
super.getThumb(videoFilename, thumbFilename, width, height,
videoInfo.getHours(), videoInfo.getMinutes(),
videoInfo.getSeconds() - 0.2f);
}
}

參考:

http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM