Skip to content

Permanent video effects

Sheraz Ahmad Khilji edited this page Jun 1, 2018 · 3 revisions

Permanent Video Effects

FFmpeg is the most famous library to process videos and apply effects/filters. It is a hectic task to integrate in android but not impossible :). There are many library projects available which have precompiled FFmpeg. Javacv is one of them and most commonly used. It is a wrapper for FFmpeg and Opencv.

How to use it

Add following dependencies to app level build.gradle

compile group: 'org.bytedeco', name: 'javacv', version: '1.1'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0-1.1', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.8.1-1.1', classifier: 'android-arm'

As video processing is a heavy task, we should do it in the background thread. e.g AsyncTask.

public class VideoProcessing extends AsyncTask {
    private final File myDirectory;
    private FFmpegFrameGrabber VIDEO_GRABBER;
    private FFmpegFrameRecorder videoRecorder;
    private File file;
    private int totalLength;
    private Context mContext;
    private FFmpegFrameFilter filter;

    public VideoProcessing(Context context, String path) {
        mContext = context;
        file = new File(path);
        VIDEO_GRABBER = new FFmpegFrameGrabber(file);
        myDirectory = new File(Environment.getExternalStorageDirectory() + "/Edited Video/");
        if (!myDirectory.exists()) {
            myDirectory.mkdirs();
        }
    }


    @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);
                }
            }
            filter.stop();
            videoRecorder.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 {
            //FFmpeg effect/filter that will be applied
            filter = new FFmpegFrameFilter("colorchannelmixer=.393:.769:.189:0:.349:.686:.168:0:.272:.534:.131", VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
            videoRecorder = FFmpegFrameRecorder.createDefault(path, VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
            videoRecorder.start();
        } catch (FrameRecorder.Exception e) {
            e.printStackTrace();
        }
    }
}

Supported video effects

List of all the video effects that FFmpeg provides.

Special Thanks to

  • Samuel Audet for providing javacv

Authored by