如何使用JavaCV从网络摄像头捕获和录制视频 [英] How to capture and record video from webcam using JavaCV

查看:980
本文介绍了如何使用JavaCV从网络摄像头捕获和录制视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaCV的新手,我很难找到有关我感兴趣的主题的各种问题的优秀教程.我已经成功地从网络摄像头实现了某种实时视频流传输,但是问题是我使用了我在网上找到的代码片段:

I'm new to JavaCV and I have difficult time finding good tutorials about different issues on the topics that I'm interested in. I've succeed to implement some sort of real time video streaming from my webcam but the problem is that I use this code snippet which I found on the net :

@Override
    public void run() {

        FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next
                                                                // camera
        int i = 0;
        try {
            grabber.start();
            IplImage img;
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
                    cvSaveImage((i++) + "-aa.jpg", img);
                    // show image on window
                    canvas.showImage(img);
                }

会生成多个jpg文件.

that results in multiple jpg files.

我真正想做的是捕获我的网络摄像头输入,并显示它,我想将其保存在适当的视频文件中.我发现了有关FFmpegFrameRecorder的信息,但不知道如何实现.另外,我一直想知道视频文件格式有哪些不同的选项,因为flv可能对我更有用.

What I really want to do is capture my webcam input and along with showing it I want to save it in a proper video file. I find out about FFmpegFrameRecorder but don't know how to implement it. Also I've been wondering what are the different options for the format of the video file, because flv maybe would be more useful for me.

推荐答案

这是一段漫长的旅程.还有一些我不确定它们背后的含义是什么,但这是一个使用JavaCV从网络摄像头捕获和录制视频的有效示例:

It's been quite a journey. Still a few things that I'm not sure what's the meaning behind them, but here is a working example for capturing and recording video from a webcam using JavaCV:

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.avutil;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class CameraTest {

    public static final String FILENAME = "output.mp4";

    public static void main(String[] args) throws Exception {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();

        CanvasFrame canvasFrame = new CanvasFrame("Cam");
        canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());

        System.out.println("framerate = " + grabber.getFrameRate());
        grabber.setFrameRate(grabber.getFrameRate());
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(FILENAME,  grabber.getImageWidth(),grabber.getImageHeight());

        recorder.setVideoCodec(13);
        recorder.setFormat("mp4");
        recorder.setPixelFormat(avutil.PIX_FMT_YUV420P);
        recorder.setFrameRate(30);
        recorder.setVideoBitrate(10 * 1024 * 1024);

        recorder.start();
        while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
            canvasFrame.showImage(grabbedImage);
            recorder.record(grabbedImage);
        }
        recorder.stop();
        grabber.stop();
        canvasFrame.dispose();
    }
}

如果您遵循有关如何在Windows 7/64bit上设置JavaCV的官方指南,并希望使用上述代码捕获视频,那么除了可能存在相同问题的问题之外,使我难以完成这项工作您应该在C:\:C:\ffmpeg中创建一个新目录,并从ffmped发行版中提取文件(已在官方指南中告知您下载该文件).然后,应将C:\ffmpeg\bin添加到Enviorment variable PATH中,仅此而已.关于此步骤,所有功劳归于 karlphillip

It was somewhat hard for me to make this work so in addition to those that may have the same issue, if you follow the official guide about how to setup JavaCV on Windows 7/64bit and want to capture video using the code above you should create a new directory in C:\ : C:\ffmpeg and extract the files from the ffmped release that you've been told to download in the official guide. Then you should add C:\ffmpeg\bin to your Enviorment variable PATH and that's all. About this step all credits go to karlphillip

和他的帖子此处

这篇关于如何使用JavaCV从网络摄像头捕获和录制视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆