在Windows和Linux下用JAVA调用FFMPEG 3.x进行视频截图的最新范例

ffmpeg3.x版本,相对2.x版本,在接口上,做了较大的改动。原来的程序可能就不能正常编译、运行了。网上很多范例程序,因此也就不能用了。

下面的例子,是本人在原ffmpeg2.X例子基础上,改写的支持ffmpeg3.x下对视频进行截图的例子。供大家参考。

在ffmpeg3.x在64位windows下,需要DLL动态链接库。下载地址:
https://download.csdn.net/download/tianfool/10369504

在linux下,需要的jar和链接库,下载地址:

https://download.csdn.net/download/tianfool/10377380

最新稳定版的下载:

最新全套Jar: FFMPEG3.4.1+JavaCV1.4.1+OpenCV3.4.1-含windows&linux平台链接库


最新Jar及链接库: FFMPEG3.4.1+JavaCV1.4.1+OpenCV3.4.1-安卓及macos 


import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 
import org.bytedeco.javacv.FFmpegFrameGrabber; 
import org.bytedeco.javacv.Frame; 
import java.io.IOException;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;

public class FFMPEGSimpleTest {
/**
* 获取指定视频的帧并保存为图片至指定目录
* @param videofile  源视频文件路径
* @param framefile  截取帧的图片存放路径
* @throws Exception
*/
  
	public static void fetchFrame(String videofile, String framefile) throws Exception {
	   
	   FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile); 
	   ff.start();
	   int lenght = ff.getLengthInFrames();
	
	
	   File targetFile = new File(framefile);
	   int i = 0;
	   Frame frame = null;
	   while (i < lenght) {
	       // 过滤前5帧,避免出现全黑的图片,依自己情况而定
	       frame = ff.grabFrame();
	       if ((i > 5) && (frame.image != null)) {
	           break;
	       }
	       i++;
	   }	   
		   
	
	   Java2DFrameConverter converter =new Java2DFrameConverter();
	   BufferedImage srcBi =converter.getBufferedImage(frame);
	   int owidth = srcBi.getWidth();
	   int oheight = srcBi.getHeight();
	   // 对截取的帧进行等比例缩放
	   int width = 800;
	   int height = (int) (((double) width / owidth) * oheight);
	   BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
	   bi.getGraphics().drawImage(srcBi.getScaledInstance(width, height, Image.SCALE_SMOOTH),0, 0, null);
	   try {
		   ImageIO.write(bi, "jpg", targetFile);
	   }catch (IOException e) {
	       e.printStackTrace();
	   }	   
	
	   ff.stop();
	
	}
	
	public static void main(String[] args) {
	   try {
	       FFMPEGSimpleTest.fetchFrame("http://test.com.cn/test.mp4", "c:/mytest.jpg");
	   } catch (Exception e) {
	       e.printStackTrace();
	   }
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值