用于从java中的视频中提取帧的编解码器?

时间:2015-11-29 07:39:34

标签: java

我正在从视频中提取帧,但到目前为止我无法选择甚至运行代码。所以,如果你能给出指示,那么它可能会有所帮助。

1 个答案:

答案 0 :(得分:0)

@Syed,我可以使用 JavaCV 3.0 从网络摄像头视频中提取帧,代码非常简单,但 JavaCV 并不是那么容易做好准备。

要使用 JavaCV ,您需要在系统中安装所有必需的依赖项,否则您将无法获得错误。

在我的情况下,我正在运行Ubuntu 14.04 64位。经过多次尝试,我决定编译 JavaCV OpenCV 。这样我就能够最终安装所有必需的依赖项。

我必须安装:ffmpegflycapturelibdc1394libfreenectvideoinputartoolkitplusflandmarkfftw。如果您使用的是MS Windows,则可能略有不同。

我用JavaCV捕获帧的代码基于Ben Davenport的示例代码,如下所示:

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameRecorder.Exception;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;

public class WebCamTest {
    public static void main(String[] args) throws Exception, org.bytedeco.javacv.FrameGrabber.Exception {
        //Here you have to put index number of grabber device, it depends on your system
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(1);
        grabber.start();

        CanvasFrame cFrame = new CanvasFrame(
                "Capture Preview", 
                CanvasFrame.getDefaultGamma() / grabber.getGamma());
        Frame capturedFrame = null;

        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

        while ((capturedFrame = grabber.grab()) != null) {
            if (cFrame.isVisible()) {
                IplImage converted = converter.convert(capturedFrame);
                // TODO: Do what you want if Captured IPLImage
                // ...
            }
        }

        cFrame.dispose();
        grabber.stop();
    }
}

希望它有所帮助!

修改

尝试使用此pom.xml进行简单,编译,打包和执行:

<?xml version="1.0" encoding="UTF-8"?>
<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>video-capture</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

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

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>WebCamTest</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>