博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过javacv对视频每隔1秒钟截取1张图片
阅读量:6570 次
发布时间:2019-06-24

本文共 5389 字,大约阅读时间需要 17 分钟。

 

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutilat 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/

package com.egova.ffmpeg.java; import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.List; import javax.imageio.ImageIO; import org.bytedeco.javacv.FFmpegFrameGrabber;import org.bytedeco.javacv.Frame;import org.bytedeco.javacv.FrameGrabber.Exception;import org.bytedeco.javacv.Java2DFrameConverter; public abstract class FrameGrabberKit {     public static void main(String[] args) throws Exception {        randomGrabberFFmpegImage("F:/CodeSpace/java/ffmpeg_java/resource/月赋情长.mp4", "./target", "月赋情长", 10);    }          public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName, int randomSize)            throws Exception {        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 < ffLength) { 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, int index) { if (null == f || null == f.image) { return; } Java2DFrameConverter converter = new Java2DFrameConverter(); String imageMat = "jpg"; String FileName = targerFilePath + File.separator + targetFileName + "_" + index + "." + imageMat; BufferedImage bi = converter.getBufferedImage(f); File output = new File(FileName); try { ImageIO.write(bi, imageMat, output); } catch (IOException e) { e.printStackTrace(); } } public static List
random(int baseNum, int length) { List
list = new ArrayList<>(length); while (list.size() < length) { Integer next = (int) (Math.random() * baseNum); if (list.contains(next)) { continue; } list.add(next); } Collections.sort(list); return list; }}

 

 

 

之前每一秒钟截取一张图片,发现有些图片报了“[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/ 

 

转载于:https://www.cnblogs.com/softidea/p/6102749.html

你可能感兴趣的文章
微软发布Azure Cosmos DB产品以及新的物联网解决方案
查看>>
理解BERT Transformer:Attention is not all you need!
查看>>
Nginx 学习书单整理
查看>>
从 Google 的一道面试题说起·
查看>>
大搜车孙信宇:一个好的团队应该去中心化
查看>>
VS2017 15.4提供预览版,面向Windows 10秋季更新(FCU)
查看>>
微软以75亿美元收购GitHub
查看>>
re:Invent第三天:除了拥抱混合云,AWS还一口气发了这些新产品
查看>>
Stack Overflow监控系统内部架构初探
查看>>
十大性能方面的错误
查看>>
Comparator改写
查看>>
更名OpenShift容器平台,红帽实现战略性转变
查看>>
如何组建开源社区
查看>>
Peter Cnudde谈雅虎如何使用Hadoop、深度学习和大数据平台
查看>>
Elixir 1.2带来多项功能增强和性能提升
查看>>
C#未来新特性:静态委托和函数指针
查看>>
过去一年,被我们“高估”的技术清单
查看>>
魅族技术晚场回顾,算法、架构、AI之外,还要考虑未来
查看>>
Windows Server 2008 \u0026 2012在GCE上正式商用了
查看>>
自动化测试中的测试执行自动化
查看>>