Search code examples
androidbitmapffmpegjavacv

Convert a javacv Frame Into Bitmap using AndroidFrameConverter


I am trying to convert a Frame of size 1280x720 into Bitmap using javacv AndroidFrameConverter and it takes long time to convert. Here is the sample code of converting.

FrameGrabber grabber = new FFmpegFrameGrabber(videoUrl);
frame = grabber.grab();
AndroidFrameConverter converter = new AndroidFrameConverter();
Bitmap originalBitmap = converter.convert(frame);

is there any other solution which is faster than this?


Solution

  • You can use this code and change filter according to your needs.

    public class FilterAsync extends AsyncTask {
    
        private FFmpegFrameGrabber VIDEO_GRABBER;
        private FFmpegFrameRecorder videoRecorder;
        private File file = new File(Environment.getExternalStorageDirectory() + "/Download/Abc.mp4");
        private Context mContext;
        private FFmpegFrameFilter filter;
        private boolean isTrue = false;
        private ArrayList<String> videoPaths;
        private File myDirectory;
    
    
        public FilterAsync(Context context) {
            mContext = context;
            VIDEO_GRABBER = new FFmpegFrameGrabber(file);
            myDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder/");
            if (!myDirectory.exists()) {
                myDirectory.mkdirs();
            }
            videoPaths = new ArrayList<>();
        }
    
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
        }
    
        @Override
        protected Object doInBackground(Object[] params) {
            Frame tempVideoFrame;
            try {
                VIDEO_GRABBER.start();
                initVideoRecorder(myDirectory + "/video" + System.currentTimeMillis() + ".mp4");
                filter.start();
                while (VIDEO_GRABBER.grab() != null) {
                    tempVideoFrame = VIDEO_GRABBER.grabImage();
                    if (tempVideoFrame != null) {
                        filter.push(tempVideoFrame);
                        tempVideoFrame = filter.pull();
                        videoRecorder.record(tempVideoFrame);
                    }
                }
                videoRecorder.stop();
                filter.stop();
                videoRecorder.release();
                VIDEO_GRABBER.stop();
                VIDEO_GRABBER.release();
            } catch (FrameGrabber.Exception e) {
                e.printStackTrace();
            } catch (FrameRecorder.Exception e) {
                e.printStackTrace();
            } catch (FrameFilter.Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
    
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
        }
    
    
        private void initVideoRecorder(String path) {
            try {
                filter = new FFmpegFrameFilter("transpose=clock ,crop=w=640:h=480:x=0:y=0", VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
    
                videoRecorder = FFmpegFrameRecorder.createDefault(path, VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
               videoRecorder.setAudioChannels(VIDEO_GRABBER.getAudioChannels());
                videoRecorder.start();
            } catch (FrameRecorder.Exception e) {
                e.printStackTrace();
            }
        }
    }