繁体   English   中英

如何从视频java中提取帧?

[英]How to extract frames from a video java?

我正在做一个对视频进行分析的项目,我想将视频拆分成帧以单独处理。 我查看了几个开源库,包括 Xuggler 和 FFMPEG,但它们都已过时且无法使用。 有没有一种简单的方法可以从视频中提取帧并将它们作为BufferedImage的集合进行处理?

按照这个代码

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;

public class Read{
    public static void main(String []args) throws IOException, Exception
    {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("D:/video.mp4");
        frameGrabber.start();
        IplImage i;
        try {

            i = frameGrabber.grab();
            BufferedImage  bi = i.getBufferedImage();
            ImageIO.write(bi,"png", new File("D:/Img.png"));
            frameGrabber.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

您可以使用OpenCV图像和视频处理免费开源框架。 并且还有一个很好的 Java 包装器。

接下来我留下@Rohit 提出的代码,但更新到2021

package com.example;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter; 
public class Main { 
    public static void main(String []args) throws IOException, Exception
    {
        File myObj = new File("D:\\x\\x\\x\\x\\video.mp4");
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(myObj.getAbsoluteFile());
        frameGrabber.start(); 
        Frame f; 
        try {
            Java2DFrameConverter c = new Java2DFrameConverter(); 
            f = frameGrabber.grab();  
            BufferedImage bi = c.convert(f);
            ImageIO.write(bi,"png", new File("D:\\x\\x\\x\\x\\img.png"));
            frameGrabber.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM