首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用jcodec改变视频分辨率

如何用jcodec改变视频分辨率
EN

Stack Overflow用户
提问于 2013-09-08 19:01:56
回答 1查看 3K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-12 13:46:35

我也在找答案,但什么也没找到。

因此,我查看了代码,并意识到在将位图传递给JCodec之前缩放它可以解决这个问题。

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

代码语言:javascript
复制
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);
    }
}

然后您可以像这样使用这个类:

代码语言:javascript
复制
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; 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18687487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档