Search in sources :

Example 1 with ExtractVideoFrame

use of com.lansosdk.box.ExtractVideoFrame in project LanSoEditor_advance by LanSoSdk.

the class ExtractVideoFrameDemoActivity method testExtractVideoFrame.

/**
 * 从这里开始演示.
 */
private void testExtractVideoFrame() {
    if (isExecuting)
        return;
    isExecuting = true;
    mInfo = new MediaInfo(videoPath);
    if (mInfo.prepare() == false || mInfo.isHaveVideo() == false) {
        return;
    }
    /**
     * 初始化.
     */
    mExtractFrame = new ExtractVideoFrame(ExtractVideoFrameDemoActivity.this, videoPath);
    /**
     * 设置在获取图片的时候, 可以指定图片的宽高, 指定后, 视频帧画面会被缩放到指定的宽高.
     *
     * 不调用则内部认为, 如果视频大于等于1280x720则自动缩小一倍.不然使用原来大小.
     * 大部分的使用场景是:作为预览横条用, 建议缩放, 这样可减少app内存
     * @param width  缩放宽度
     * @param height 缩放高度
     */
    if (mInfo.vWidth * mInfo.vHeight > 960 * 540) {
        mExtractFrame.setBitmapWH(mInfo.vWidth / 2, mInfo.vHeight / 2);
    }
    // 
    /**
     * 设置提取间隔.
     * 取bitmap的间隔, 即解码好后, 每隔几个返回一个bitmap, 用在需要列出视频一部分,但不需要全部的场合,比如预览缩略图.
     *
     * 如果设置时间,则从开始时间后, 查当前解码好几个图片,然后做间隔返回bitmap
     *
     * 比如设置间隔是3, 则bitmap在第 0个返回, 第4 8,12,16个返回.
     *
     * 可以用MediaInfo或FrameInfo得到当前视频中总共有多少帧,用FrameInfo可以得到每一帧的时间戳.
     *
     * @param frames
     */
    // mExtractFrame.setExtractInterval(5);
    /**
     * 设置处理完成监听.
     */
    mExtractFrame.setOnExtractCompletedListener(new onExtractVideoFrameCompletedListener() {

        @Override
        public void onCompleted(ExtractVideoFrame v) {
            // TODO Auto-generated method stub
            // 单位毫秒.
            long timeOut = System.currentTimeMillis() - startTime;
            float leftF = ((float) timeOut / 1000);
            // 保留一位小数.
            float b = (float) (Math.round(leftF * 10)) / 10;
            String str = "解码结束:\n" + "解码的视频总帧数:" + frameCount + "\n" + "解码耗时:" + b + "(秒)" + "\n" + "\n" + "\n" + "视频宽高:" + mInfo.vWidth + " x " + mInfo.vHeight + "\n" + "解码后图片缩放宽高:" + mExtractFrame.getBitmapWidth() + " x " + mExtractFrame.getBitmapHeight() + "\n";
            Log.i("TIME", "解码结束::" + str);
            tvProgressHint.setText("Completed" + str);
            isExecuting = false;
            frameCount = 0;
        }
    });
    /**
     * 设置处理进度监听.
     */
    mExtractFrame.setOnExtractProgressListener(new onExtractVideoFrameProgressListener() {

        /**
         * 当前帧的画面回调,,  ptsUS:当前帧的时间戳,单位微秒.
         * 拿到图片后,建议放到ArrayList中, 不要直接在这里处理.
         */
        @Override
        public void onExtractBitmap(Bitmap bmp, long ptsUS) {
            // TODO Auto-generated method stub
            frameCount++;
            String hint = " 当前是第" + frameCount + "帧" + "\n" + "当前帧的时间戳是:" + String.valueOf(ptsUS) + "微秒";
            tvProgressHint.setText(hint);
            Log.i(TAG, "bitmap ptsUs is:" + ptsUS);
        // savePng(bmp);  //测试使用.
        // if(bmp!=null && bmp.isRecycled()){
        // bmp.recycle();
        // bmp=null;
        // }
        // if(ptsUS>15*1000*1000){ //  你可以在指定的时间段停止.
        // mExtractFrame.stop();   //这里演示在15秒的时候停止.
        // }
        }
    });
    frameCount = 0;
    /**
     * 开始执行.  或者你可以从指定地方开始解码.
     * mExtractFrame.start(10*1000*1000);则从视频的10秒处开始提取.
     */
    mExtractFrame.start();
    /**
     * 您可以一帧一帧的读取, seekPause是指:seek到指定帧后, 调用回调就暂停.
     * 单位是us, 微秒.
     * 单独读取的话, 把这里打开
     * 如果您频繁的读取, 建议直接一次性读取完毕,放到sd卡里,然后用的时候, 从sd卡中读取.
     */
    // mExtractFrame.seekPause(seekCount*1000*1000);
    startTime = System.currentTimeMillis();
}
Also used : Bitmap(android.graphics.Bitmap) MediaInfo(com.lansosdk.videoeditor.MediaInfo) com.lansosdk.box.onExtractVideoFrameProgressListener(com.lansosdk.box.onExtractVideoFrameProgressListener) ExtractVideoFrame(com.lansosdk.box.ExtractVideoFrame) com.lansosdk.box.onExtractVideoFrameCompletedListener(com.lansosdk.box.onExtractVideoFrameCompletedListener)

Example 2 with ExtractVideoFrame

use of com.lansosdk.box.ExtractVideoFrame in project LanSoEditor_advance by LanSoSdk.

the class DisplayFramesActivity method startGetFrames.

/**
 * 从这里开始演示.
 */
private void startGetFrames() {
    if (isExecuting)
        return;
    mInfo = new MediaInfo(videoPath);
    if (mInfo.prepare() == false || mInfo.isHaveVideo() == false) {
        return;
    }
    isExecuting = true;
    /**
     * 初始化.
     */
    mExtractFrame = new ExtractVideoFrame(DisplayFramesActivity.this, videoPath);
    if (mInfo.vWidth * mInfo.vHeight > 960 * 540) {
        // 视频分辨率过大,则缩小一倍.
        mExtractFrame.setBitmapWH(mInfo.vWidth / 2, mInfo.vHeight / 2);
    }
    if (mTpye == FRAME_TYPE_25) {
        // 25帧, 先检查用 内存释放够,如果不够,再用SD卡来缓存.
        mExtractFrame.setExtract25Frame();
        long desireSize = mExtractFrame.getBitmapHeight() * mExtractFrame.getBitmapWidth() * 4 * 25;
        long cachesize = BitmapLruCache.getMaxCacheSize();
        if (desireSize > cachesize) {
            mDiskCache = new MemoryDiskCache(getApplication());
        } else {
            mLruCache = new BitmapLruCache();
        }
    } else if (mTpye == FRAME_TYPE_60) {
        mExtractFrame.setExtract60Frame();
        mDiskCache = new MemoryDiskCache(getApplication());
    } else {
        // 不设置,则默认是全部解码
        // 全部解码,则用DiskLruCache
        mDiskCache = new MemoryDiskCache(getApplication());
    }
    if (mDiskCache != null) {
        Log.i(TAG, "写入到 硬盘.....");
    } else {
        Log.i(TAG, "写入到 memory....");
    }
    /**
     * 设置处理完成监听.
     */
    mExtractFrame.setOnExtractCompletedListener(new onExtractVideoFrameCompletedListener() {

        @Override
        public void onCompleted(ExtractVideoFrame v) {
            mImageAdapter.notifyDataSetChanged();
        }
    });
    /**
     * 设置处理进度监听.
     */
    mExtractFrame.setOnExtractProgressListener(new onExtractVideoFrameProgressListener() {

        /**
         * 当前帧的画面回调,,  ptsUS:当前帧的时间戳,单位微秒.
         */
        @Override
        public void onExtractBitmap(Bitmap bmp, long ptsUS) {
            if (mDiskCache != null) {
                mDiskCache.pushBitmap(bmp);
                count++;
                // if(count%10==0){
                mImageAdapter.notifyDataSetChanged();
            // }
            } else if (mLruCache != null) {
                mLruCache.pushBitmap(bmp);
                count++;
                // if(count%10==0){
                mImageAdapter.notifyDataSetChanged();
            // }
            }
        }
    });
    /**
     * 开始执行.  或者你可以从指定地方开始解码.
     * mExtractFrame.start(10*1000*1000);则从视频的10秒处开始提取.
     */
    mExtractFrame.start();
}
Also used : BitmapLruCache(com.lansosdk.videoeditor.BitmapLruCache) Bitmap(android.graphics.Bitmap) MediaInfo(com.lansosdk.videoeditor.MediaInfo) MemoryDiskCache(com.lansosdk.videoeditor.MemoryDiskCache) com.lansosdk.box.onExtractVideoFrameProgressListener(com.lansosdk.box.onExtractVideoFrameProgressListener) ExtractVideoFrame(com.lansosdk.box.ExtractVideoFrame) com.lansosdk.box.onExtractVideoFrameCompletedListener(com.lansosdk.box.onExtractVideoFrameCompletedListener)

Aggregations

Bitmap (android.graphics.Bitmap)2 ExtractVideoFrame (com.lansosdk.box.ExtractVideoFrame)2 com.lansosdk.box.onExtractVideoFrameCompletedListener (com.lansosdk.box.onExtractVideoFrameCompletedListener)2 com.lansosdk.box.onExtractVideoFrameProgressListener (com.lansosdk.box.onExtractVideoFrameProgressListener)2 MediaInfo (com.lansosdk.videoeditor.MediaInfo)2 BitmapLruCache (com.lansosdk.videoeditor.BitmapLruCache)1 MemoryDiskCache (com.lansosdk.videoeditor.MemoryDiskCache)1