How can I convert a video to byte array and then byte array back to video in parse?

I am working on a project where I need to first convert video to byte array to reduce its size and then convert it back to video.

Upvotes: 0

Views: 3802

Answers (1)

Max Worg
Max Worg

Reputation: 2972

I'm not sure that you will reduce a video's size by putting each byte into an array.

If I have a video that is 4,000 bytes then you will have an array of exactly 4,000 bytes (including all of the overhead of the array itself in memory).

There is a method on a ParseFile object called .getData() that will return a byte[] array. So if you have a video stored in Video class in the data column you can do something like this:

byte[] bytes_of_video = videoObject.getParseFile("data").getBytes();

Now you have a byte array of the video object but the byte array will be just as big as the video file since no compression took place. If you want to compress the video file you can use Android's zlib compression with Deflater:

byte[] originalBytes = bytes_of_video;

     Deflater deflater = new Deflater();
     deflater.setInput(originalBytes);
     deflater.finish();

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buf = new byte[8192];
     while (!deflater.finished()) {
         int byteCount = deflater.deflate(buf);
         baos.write(buf, 0, byteCount);
     }
     deflater.end();

     byte[] compressedBytes = baos.toByteArray();

I don't know if you are wanting to save space on the server or on the device but to save bandwidth costs you could store the compressed video data in your ParseFile object in the server and then decompress the file on the device when it's ready to be viewed/used. But a compressed video file on the device is not very useful since you won't be able to view/edit/use it while it's in a compressed format.

Upvotes: 1

Related Questions