java获取视频的第一帧
//------------maven配置文件---------------
<dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>0.8</version> </dependency>
/**
* 获取指定视频的帧并保存为图片至指定目录
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放路径
* @throws Exception
*/
public static void fetchFrame(String videofile, String framefile)
throws Exception {
long start = System.currentTimeMillis();
File targetFile = new File(framefile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > 5) && (f.image != null)) {
break;
}
i++;
}
IplImage img = f.image;
int owidth = img.width();
int oheight = img.height();
// 对截取的帧进行等比例缩放
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
// ff.flush();
ff.stop();
System.out.println(System.currentTimeMillis() - start);
}
public static void main(String[] args) {
try {
ImageUtil.fetchFrame("https://jsjoke.net/upload-1529285839603-touristappid.o6zAJswEDExvrLeuQTdnRSIz87Vo.f0fd65c07d78b8fc831c97888e4d7a55.mp4", "D:/test4.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
--------------------------------------- 解决图片旋转问题---------------------------------------
通过一段时间的搜索了解到,如果拍摄的视频中带有旋转(rotate)信息,那么截取出来的图片就会被旋转。通过查询API发现FFmpegFrameGrabber的getVideoMetadata("rotate")方法可以获取到视频的旋转信息。根据获取到的rotate信息对ff.grabImage()得到的Frame进行旋转,但是Frame并没有提供旋转接口。但有一个IpImage对象提供了旋转方法
public static void main(String[] args) {
try {
ImageUtil.fetchFrame("https://jsjoke.net/upload-1529285839603-touristappid.o6zAJswEDExvrLeuQTdnRSIz87Vo.f0fd65c07d78b8fc831c97888e4d7a55.mp4", "D:/test4.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
public static IplImage rotate(IplImage src, int rotate) {
IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
opencv_core.cvTranspose(src, img);
opencv_core.cvFlip(img, img, angle);
return img;
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
public abstract class FrameGrabberKit {
public static void main(String[] args) throws Exception {
randomGrabberFFmpegImage("C:/Users\\Administrator\\Desktop\\VID_20171229_162251.mp4", "G:\\test", "111");
}
public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName)
throws Exception {
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
ff.start();
String rotate =ff.getVideoMetadata("rotate");
Frame f;
int i = 0;
while (i <1) {
f =ff.grabImage();
IplImage src = null;
if(null !=rotate &&rotate.length() > 1) {
OpenCVFrameConverter.ToIplImage converter =new OpenCVFrameConverter.ToIplImage();
src =converter.convert(f);
f =converter.convert(rotate(src, Integer.valueOf(rotate)));
}
doExecuteFrame(f,targerFilePath,targetFileName);
i++;
}
ff.stop();
}
/*
* 旋转角度的
*/
public static IplImage rotate(IplImage src, int angle) {
IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
opencv_core.cvTranspose(src, img);
opencv_core.cvFlip(img, img, angle);
return img;
}
public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName) {
if (null ==f ||null ==f.image) {
return;
}
Java2DFrameConverter converter =new Java2DFrameConverter();
String imageMat ="jpg";
String FileName =targerFilePath + File.separator +targetFileName +"." +imageMat;
BufferedImage bi =converter.getBufferedImage(f);
System.out.println("width:" + bi.getWidth());
System.out.println("height:" + bi.getHeight());
File output =new File(FileName);
try {
ImageIO.write(bi,imageMat,output);
}catch (IOException e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.3.1</version>
</dependency>
来源: