如何使用jcodec更改视频分辨率

时间:2013-09-08 19:01:56

标签: java android blackberry-10 video-processing

我需要降低某些mp4文件的视频分辨率,我决定使用jcodec。我试图搜索一些示例如何执行此操作,但没有找到任何示例。任何有关此信息都将非常有用。

1 个答案:

答案 0 :(得分:2)

我也在寻找答案,但没有找到任何答案。

所以我查看了代码并意识到在将Bitmap传递给JCodec之前对其进行缩放可以解决问题。

这是我使用的课程。请注意,这是Android的代码,它使用最新的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);
    }
}

然后你可以使用这样的类:

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; 
}