Ask Your Question
0

Saving a buffer of mat to a video

asked 2016-05-13 07:27:45 -0600

2D3D gravatar image

have a buffer which has the Mats from which i want to create a video

I transform those mats into byte array and write them to the outputstream yet for some reason whenever I watch the video only one image is shown heres the code

try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/ran");
dir.mkdirs();
File file = new File(dir, "goal_ran.mp4");
FileOutputStream out = new FileOutputStream(file);

for(int i=0;i<buffer.size();i++)
{

    Mat mat = buffer.get(i);
    MatOfByte matOfByte = new MatOfByte();
    Imgcodecs.imencode(".png", mat, matOfByte);
    byte[] byteArray = matOfByte.toArray();
    out.write(byteArray);
    out.flush();
    /*byte[] return_buff = new byte[(int) (buffer.get(i).total() * buffer.get(i).elemSize())];
    buffer.get(i).get(0, 0, return_buff);*/
    /*int length = (int) (mat.total() * mat.elemSize());
    byte buffer[] = new byte[length];
    mat.get(0, 0, buffer);
    out.write(buffer);*/
}

out.close();
Log.d("Video","Successful"+ buffer.size());



} catch (Exception e) {
    e.printStackTrace();
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-05-13 07:43:16 -0600

berak gravatar image

this is not the way, you write a video file. an mp4 is not simply a chain of concatenated png's.

if you're using opencv3, you can use the VideoWriter class with VideoWriter.fourcc('M','J','P','G') as codec(sorry, but that's the only one available on android, atm) and ".avi" as container.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-05-13 07:27:45 -0600

Seen: 819 times

Last updated: May 13 '16