通过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/

时间: 2024-12-28 22:04:12

通过javacv对视频每隔1秒钟截取1张图片的相关文章

实现姓名和学号每隔一秒钟互换的效果

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <!--引入框架--> 7 <link href="resource/bootstrap/css/bootstrap.css" rel="stylesh

每隔10秒钟打印一个“Helloworld”

/** * 每隔10秒钟打印一个"Helloworld" */ public class Test03 { public static void main(String[] args) throws InterruptedException { ThreadImp threadImp = new ThreadImp(); Thread thread1 = new Thread(threadImp); thread1.start(); } } class ThreadImp extend

shell每隔一秒钟就记录下netstat状态

说明 木马可能类似随机发送心跳包的操作,随机sleep.对这类情况写好了一个监听shell脚本,每隔一秒钟就记录下netstat状态. 代码 #!/bin/bash #功能:用于定时执行lsof 和 netstat 的执行状态并记录到文件 slptime=1 #默认一秒执行一次 filedir=/tmp #默认存储在/tmp下 lsoffile=lsof.log #存储lsof的执行结果 netstatfile=netstatfile.log #存储netstat执行结果 while true

两个线程每隔一秒钟交替打印5个数

package test.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExecutorTest { private static int i=0; private static final int PRINT_COUNT=5; private static final int CYCLE_COUNT=50; private stat

蓝懿IOS阶段测试

今天刘国斌老师给我们出了七道题,作为阶段性的测试题,我还是蛮喜欢这样的经常来一点考试对我的学习很有帮助. 0.在 Table view 列表中显示:第1题.第2题.第3题…… 题目列表  点击某一题跳转到解答这道题的页面 1.Progress view 和 Slider 实现拖动 Slider,Progress view 的进度跟着移动 2.每隔一秒钟在界面位置随机出现图片按钮,界面添加一个 Label 显示已经出现的按钮数量,并且每添加一个按钮,Label 所显示数量自动增加,点击某一个按钮时

ViewPager实现图片轮翻效果

很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的.今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的状态也在变化),用定时器来实现不难. [java] view plaincopyprint? import java.util.ArrayList; import java.util.concurrent.Executors; import java.util.concurrent.Schedule

用cocos2d-html5做的消除类游戏《英雄爱消除》(1)——系统主菜单

系统主菜单如下图所示: 首先,介绍下这个主菜单,它包含了一个动画logo以及一个按钮选项,动画logo每隔1秒钟切换一张图片,点击相应的按钮选项会切换不同的游戏场景. 下面看下这个界面的源码: /** * Power by html5中文网(html5china.com) * author: jackyWHJ */ cc.dumpConfig(); var winSize; var SysMenu = cc.Layer.extend({ _hero:null, _logo:null, _logo

viewpager实现轮播图效果

在很多app中我们都有轮播图的效果,特别是一些电商类的app,大部分都有此功能,今天就简单的模仿下,还有一个自动跳动的小功能,这个用定时器实现就行,话不多说直接上代码: import java.util.ArrayList; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import

ViewPager 自动播放主要代码部分(借鉴)

第一步 初始化viewpager //初始化bannerprivate void initBannerView(View v) { vpBanner = new ArrayList<ImageView>(); for (int i = 0; i < bannerResource.length; i++) { ImageView iv = new ImageView(getActivity()); iv.setImageResource(bannerResource[i]); vpBann