JavaCV FFmpegFrameRecorder将图像保存到视频

时间:2018-05-29 21:09:55

标签: java ffmpeg javacv video-recording javacpp

我对使用JavaCV FFmpegFrameRecorder感到有些困惑。我有几个byte []或short []数组(取决于我的图像是8位还是16位)我有几个图像相关的数据。现在,我的想法是使用JavaCPP将每个图像发送到ffmpeg,因此它以我希望的帧速率从这个集合创建一个静音视频。到目前为止,我有:

package ffmpeg;

import java.awt.EventQueue;
import java.io.File;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import javax.swing.JFrame;

public class rwa {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    rwa window = new rwa();
                    window.frame.setVisible(true);

                    Frame myframe = new Frame();
                    myframe .imageHeight = 100;
                    myframe .imageWidth = 200;
                    myframe .imageChannels = 1;
                    myframe .imageDepth = 8;

                    byte[] myimage = new byte[20000];

                    //all black
                    for (int j = 0; j<myimage.length; j++){
                        myimage[j]=-128;
                    }

                    File dest = new File("C:\\out.mp4");
                    FFmpegFrameRecorder record = new FFmpegFrameRecorder(dest, 0);
                    //FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(dest);
                    record.setFrameRate(0.04);
                    record.setVideoCodec(13);
                    record.setFormat("mp4");
                    record.setPixelFormat(0);
                    record.setImageHeight(100);
                    record.setImageWidth(200);
                    record.setVideoBitrate(1000000);

                    record.start();
                    for (int i=0; i<100; i++){

                        if (myimage.length*(1+i)<20000) {
                         //this is just for debugging it. I'm creating a different image each frame to see if it works. In practice, I will read in each step the propper image
                        for (int j = myimage.length*i; j<myimage.length*(i+1); j++){
                            myimage[j]=127;
                        }
                      }

                        Buffer[] buf = {ByteBuffer.wrap(myimage)};

                        myframe.image = buf;
                        record.recordImage(200, 100, 8, 1, 0, 0, ByteBuffer.wrap(myimage));
                        //record.record(myframe);
                    }
                    record.stop();
                    record.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public rwa() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

但是我收到了像

这样的错误
  

Java Runtime Environment检测到致命错误:

     

EXCEPTION_ACCESS_VIOLATION(0xc0000005)at pc = 0x000007fefe4511d3,pid = 6432,tid = 0x000000000000027c

那边有什么不对?我该如何选择比特率?我的图像最多为16位1通道2048 * 2048像素。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用事件/线程时,应在JavaCV中使用Parallel.run()方法。