0

I need to reduce video resolution for some mp4 file, and I decide to use jcodec. I tried to search some example how to do this but not found any examples. Any information about this will be useful.

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
  • Did some research and jcodec was no made for android and you would get alot of problems.. You should use : http://stackoverflow.com/questions/2645041/ffmpeg-for-a-android-using-tutorial-ffmpeg-and-android-mk – Hansjörg Hofer Sep 08 '13 at 20:32
  • `jcodec was no made for android` - [this isn't true](http://jcodec.org/news/no_deps.html). Also I can't use `ffmpeg` because I'm will port app to Blackberry10 – CAMOBAP Sep 09 '13 at 05:12

1 Answers1

2

I was looking for an answer to this too, but didn't find anything.

So I looked into the code and realized that scaling the Bitmap before passing it to JCodec solves the problem.

Here is the class I use. Please note that this is code for Android, which uses the latest JCodec for Android.

public class DDVideoEncoder  {

    private static final String TAG = DDVideoEncoder.class.getSimpleName();

    private SequenceEncoder encoder;


    public DDVideoEncoder(String filepath) throws IOException, OutOfMemoryError {
        File out = new File(filepath);
        encoder = new SequenceEncoder(out);

    }

    // returns false on out of memory error.
    public Boolean addFrame(String filePath, int newSize) throws IOException {

        // newSize indicates size of bitmap in percent
        Bitmap bi = getResizedBitmap(filePath, newSize);

        try { encoder.encodeImage(bi); }
        catch (OutOfMemoryError outOfMemoryError) { Log.d(TAG, "encodeImage:" + outOfMemoryError); return  false; }
        catch (NullPointerException nil) { Log.d(TAG, "encodeImage:"+nil); return  false; }
        return true;

    }

    public void finishEncoding(){
        try { encoder.finish(); }
        catch (IOException io) {
            Log.d("DDVideoEncoder", "IOException"); }
    }

    // newSize indicates size of bitmap in percent of original.
    //decodes image and scales it to reduce memory consumption
    private Bitmap getResizedBitmap(String filePath, int newSize) {

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        //BitmapFactory.decodeStream(new FileInputStream(f),null,o);
        BitmapFactory.decodeFile(filePath, o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=newSize*10;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        //return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        return BitmapFactory.decodeFile(filePath, o2);
    }
}

And you can then use the class like this:

try {
    DDVideoEncoder tmpEncoder = new DDVideoEncoder(dir+vidName);

    // write x frames
    Log.d("test", "adding frame");

    try { tmpEncoder.addFrame(path, 50); }
    catch (OutOfMemoryError outOfMemoryError){  Log.d("test", "oom");  break; }
    catch (IOException io) { Log.d("CactusMemChecker", io.getMessage());  break; }
    catch (IndexOutOfBoundsException oob) { Log.d("test", "oob");  break; }

    // finish up
    tmpEncoder.finishEncoding();

} catch (IOException e) { 
    Log.d("test", e.getMessage()); 
    testResult = false; 
} catch (OutOfMemoryError e) { 
    Log.d("test", e.getMessage()); 
    testResult = false; 
} catch (IndexOutOfBoundsException e) { 
    Log.d("test", e.getMessage()); 
    testResult = false; 
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
r0ber7
  • 329
  • 1
  • 10