通過javacv對視頻每隔1秒鍾截取1張圖片


 

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.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.<clinit>(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

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.0</version>
</dependency>

 

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/


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<Integer> 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<Integer> random(int baseNum, int length) {
 
        List<Integer> 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/ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM