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