JavaCV FFmpegFrameRecorder save images to video

2

I am a bit confused about the use of JavaCV FFmpegFrameRecorder. I have several byte[] or short[] arrays (depending if my images are 8 or 16 bit) were I have the data related for several images. Now, my idea is to use JavaCPP to send each image to ffmpeg so it creates me a mute video from this collection at the framerate I wish. Up to now I have:

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);
    }

}

But I am getting errors like

A fatal error has been detected by the Java Runtime Environment:

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

what is wrong there? And how should I select the bitrate? My images, at maximum, will be 16bit 1 channel 2048*2048 pixels.

Thanks!

java
ffmpeg
javacv
video-recording
javacpp

1 Answer

0

You should use Parallel.run() method in JavaCV, when you are using an event/thread.

answered on Stack Overflow Aug 28, 2019 by MR.Mohebian

User contributions licensed under CC BY-SA 3.0