David Bekham
David Bekham

Reputation: 2175

Decompressed video file is not working in Java

Basically i compress video using the customized compressor class in Java. I have assembled my complete code snippets here. My actually problem is, generated video [ A.mp4] from the decompressed byte array is not running. I actually i got this compressor class code over the internet. As i new to Java platform, i am struggling to resolve this problem. Could you please any one help me on this.?

public class CompressionTest
{
    public static void main(String[] args)
    {
        Compressor compressor = new Compressor();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileInputStream fis=null;
        File file=null;

        try
        {
            URL uri=CompressionTest.class.getResource("/Files/Video.mp4");
            file=new File(uri.getPath());
            fis = new FileInputStream(file);
        } 
        catch ( FileNotFoundException fnfe )
        {
            System.out.println( "Unable to open input file");
        }

        try 
        {

            byte[] videoBytes = getBytesFromFile(file);

            System.out.println("CompressionVideoToCompress is: '" +videoBytes + "'");

            byte[] bytesCompressed = compressor.compress(videoBytes);

            System.out.println("bytesCompressed is: '" +bytesCompressed+ "'");

            byte[] bytesDecompressed=compressor.decompress(bytesCompressed);

            System.out.println("bytesDecompressed is: '" +bytesDecompressed+ "'");

            FileOutputStream out = new FileOutputStream("A.mp4");
            out.write(bytesDecompressed,0,bytesDecompressed.length-1);
            out.close();

        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            System.out.println("bytesCompressed is: '");
        }


    }

    public static byte[] getBytesFromFile(File file) throws IOException 
    {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[1064];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
        {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
}

class Compressor
{
    public Compressor()
    {}

    public byte[] compress(byte[] bytesToCompress)
    {        
        Deflater deflater = new Deflater();
        deflater.setInput(bytesToCompress);
        deflater.finish();

        byte[] bytesCompressed = new byte[Short.MAX_VALUE];

        int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);

        byte[] returnValues = new byte[numberOfBytesAfterCompression];

        System.arraycopy
        (
            bytesCompressed,
            0,
            returnValues,
            0,
            numberOfBytesAfterCompression
        );

        return returnValues;
    }

    public byte[] decompress(byte[] bytesToDecompress)
    {
        Inflater inflater = new Inflater();

        int numberOfBytesToDecompress = bytesToDecompress.length;

        inflater.setInput
        (
            bytesToDecompress,
            0,
            numberOfBytesToDecompress
        );

        int compressionFactorMaxLikely = 3;

        int bufferSizeInBytes =
            numberOfBytesToDecompress
            * compressionFactorMaxLikely;

        byte[] bytesDecompressed = new byte[bufferSizeInBytes];

        byte[] returnValues = null;

        try
        {
            int numberOfBytesAfterDecompression = inflater.inflate(bytesDecompressed);

            returnValues = new byte[numberOfBytesAfterDecompression];

            System.arraycopy
            (
                bytesDecompressed,
                0,
                returnValues,
                0,
                numberOfBytesAfterDecompression
            );            
        }
        catch (DataFormatException dfe)
        {
            dfe.printStackTrace();
        }

        inflater.end();

        return returnValues;
    }
}

Upvotes: 1

Views: 2375

Answers (2)

AlexWien
AlexWien

Reputation: 28757

Two tips:

1) Replace getBytesFromFile with a well known API call, either using Apache commons (IOUtils) or java 7 now provides such a method, too.
2) Test compress and decompress by writing a Junit test: Create a random huge byte array, write it out, read it back and compare it with the created one.

Upvotes: 1

gd1
gd1

Reputation: 11403

I've tested your code by compressing and decompressing a simple TXT file. The code is broken, since the compressed file, when uncompressed, is different from the original one.

Take for granted that the code is broken at least in the getBytesFromFile function. Its logic is tricky and troublesome, since it only allows files up to length 1064 and the check (throwing IOException when a longer file is read) does not work at all. The file gets read only partially and no exception is thrown.

What you are trying to achieve (file compression/decompression) can be done this way. I've tested it and it works, you just need this library.

import java.io.*;
import java.util.zip.*;
import org.apache.commons.io.IOUtils; // <-- get this from http://commons.apache.org/io/index.html

public class CompressionTest2 {

    public static void main(String[] args) throws IOException {
        File input = new File("input.txt");
        File output = new File("output.bin");
        Compression.compress(input, output);
        File input2 = new File("input2.txt");
        Compression.decompress(output, input2);
        // At this point, input.txt and input2.txt should be equal
    }

}

class Compression {

    public static void compress(File input, File output) throws IOException {
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);
        GZIPOutputStream gzipStream = new GZIPOutputStream(fos);
        IOUtils.copy(fis, gzipStream);
        gzipStream.close();
        fis.close();
        fos.close();
    }

    public static void decompress(File input, File output) throws IOException {
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);
        GZIPInputStream gzipStream = new GZIPInputStream(fis);
        IOUtils.copy(gzipStream, fos);
        gzipStream.close();
        fis.close();
        fos.close();
    }

}

This code doesn't come from "credible and/or official sources" but at least it works. :)

Moreover, in order to get more answers, adjust the title stating your real problem: your compressed files don't decompress the right way. There is no 'video' stuff here. Moreover, zipping a .mp4 file is no achievement (compression ratio will likely be around 99.99%).

Upvotes: 1

Related Questions