0

我需要降低某些 mp4 文件的视频分辨率,我决定使用jcodec。我试图搜索一些如何做到这一点的例子,但没有找到任何例子。任何有关这方面的信息都会很有用。

4

1 回答 1

2

我也在寻找这个问题的答案,但没有找到任何东西。

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

这是我使用的课程。请注意,这是适用于 Android 的代码,它使用最新的适用于 Android 的 JCodec。

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; 
}
于 2014-02-12T13:46:35.577 回答