Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Video Compression , +
Below code is not working to re-generate my compressed video. Could you please any one help me on this ?
 
atform, 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;
    }
}
Posted 17-Feb-13 20:54pm
Daveedu403
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Deflater and Inflater are presumably for zip file handling and not for video usage. Try finding a library for video handling instead.
  Permalink  
Comments
Daveedu at 18-Feb-13 4:33am
   
As i new to Java, i am not able to find the alternative way for this. Could you please direct the way to compress the video?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Be aware that characters (not bytes) in java are wide characters - each character is 16 bits wide. A byte is 8 bits. In almost every other language, a character defaults to 8 bit form.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100