java 每隔一秒_通过javacv对视频每隔1秒钟截取1张图片

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize classorg.bytedeco.javacpp.avutil

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:274)

at org.bytedeco.javacpp.Loader.load(Loader.java:385)

at org.bytedeco.javacpp.Loader.load(Loader.java:353)

at org.bytedeco.javacpp.avformat$AVFormatContext.(avformat.java:2249)

at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:346)

at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:340)

http://stackoverflow.com/questions/27733142/could-not-initialize-class-org-bytedeco-javacpp-avutil-on-os-x-along-with-maven

org.bytedeco

javacv

1.0

FFmpegFrameGrabber g = new FFmpegFrameGrabber("textures/video/anim.mp4");

g.start();for (int i = 0 ; i < 50 ; i++) {

ImageIO.write(g.grab().getBufferedImage(),"png", new File("frame-dump/video-frame-" + System.currentTimeMillis() + ".png"));

}

g.stop();

http://stackoverflow.com/questions/15735716/how-can-i-get-a-frame-sample-jpeg-from-a-video-mov

https://github.com/bytedeco/javacv/

packagecom.egova.ffmpeg.java;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;importjavax.imageio.ImageIO;importorg.bytedeco.javacv.FFmpegFrameGrabber;importorg.bytedeco.javacv.Frame;importorg.bytedeco.javacv.FrameGrabber.Exception;importorg.bytedeco.javacv.Java2DFrameConverter;public abstract classFrameGrabberKit {public static void main(String[] args) throwsException {

randomGrabberFFmpegImage("F:/CodeSpace/java/ffmpeg_java/resource/月赋情长.mp4", "./target", "月赋情长", 10);

}public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName, intrandomSize)throwsException {

FFmpegFrameGrabber ff=FFmpegFrameGrabber.createDefault(filePath);

ff.start();int ffLength =ff.getLengthInFrames();

List randomGrab =random(ffLength, randomSize);int maxRandomGrab = randomGrab.get(randomGrab.size() - 1);

Frame f;int i = 0;while (i

f=ff.grabImage();if(randomGrab.contains(i)) {

doExecuteFrame(f, targerFilePath, targetFileName, i);

}if (i >=maxRandomGrab) {break;

}

i++;

}

ff.stop();

}public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName, intindex) {if (null == f || null ==f.image) {return;

}

Java2DFrameConverter converter= newJava2DFrameConverter();

String imageMat= "jpg";

String FileName= targerFilePath + File.separator + targetFileName + "_" + index + "." +imageMat;

BufferedImage bi=converter.getBufferedImage(f);

File output= newFile(FileName);try{

ImageIO.write(bi, imageMat, output);

}catch(IOException e) {

e.printStackTrace();

}

}public static List random(int baseNum, intlength) {

List list = new ArrayList<>(length);while (list.size()

Integer next= (int) (Math.random() *baseNum);if(list.contains(next)) {continue;

}

list.add(next);

}

Collections.sort(list);returnlist;

}

}

之前每一秒钟截取一张图片,发现有些图片报了“[mpeg4 @ 05938aa0] warning: first frame is no keyframe”这个警告,而且截出的图片都是灰屏,根本没有图片。后来在网上找了很久,终于弄明白了,原来是ffmpeg它有“关键帧”这个说法,所以如果设置的帧的位置不是关键帧的位置的话,就可能截出的图片有问题。后来经过改进,终于搞定了。

public static void main(String[] args) {

System.out.println(System.getProperty("java.library.path"));

// System.out.println("Welcome to OpenCV " + Core.VERSION);

// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

// Mat m = Mat.eye(3, 3, CvType.CV_8UC1);

// System.out.println("m = " + m.dump());

// 加载本地的OpenCV库,这样就可以用它来调用Java API

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Test t = new Test();

//t.test();

//t.run();

//t.run2();

t.run3();

//System.out.println(t.CmpPic("d:/img/219.jpg"));

}

public void run3() {

CvCapture capture = opencv_highgui.cvCreateFileCapture("D:/085402.crf");

//帧率

int fps = (int) opencv_highgui.cvGetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FPS);

System.out.println("帧率:"+fps);

IplImage frame = null;

double pos1 = 0;

int rootCount = 0;

while (true) {

//读取关键帧

frame = opencv_highgui.cvQueryFrame(capture);

rootCount = fps;

while(rootCount > 0 ){

//这一段的目的是跳过每一秒钟的帧数,也就是说fps是帧率(一秒钟有多少帧),在读取一帧后,跳过fps数量的帧就相当于跳过了1秒钟。

frame = opencv_highgui.cvQueryFrame(capture);

rootCount--;

}

//获取当前帧的位置

pos1 = opencv_highgui.cvGetCaptureProperty(capture,opencv_highgui.CV_CAP_PROP_POS_FRAMES);

System.out.println(pos1);

if (null == frame)

break;

opencv_highgui.cvSaveImage("d:/img/" + pos1 + ".jpg",frame);

}

opencv_highgui.cvReleaseCapture(capture);

}

http://www.voidcn.com/blog/kouwoo/article/p-4830734.html

http://stackoverflow.com/questions/33319899/java-lang-unsatisfiedlinkerror-no-opencv-java300-in-java-library-path-only-whil

http://git.oschina.net/leixiaohua1020/simplest_video_website

https://ffmpeg.zeranoe.com/builds/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值