Trim video Like whatsapp

dev picture dev · Mar 5, 2014 · Viewed 15.6k times · Source

I have seen unique feature in whatsapp messenger.In which before sending video application allow user to select frames and user can send only those selected frames as video.

So, My question is how can we divide video in frames and again ganerate video from divided frames? How whatsapp messagnes had done?

Answer

Android Developer picture Android Developer · Aug 2, 2015

You can use ffmpeg-android for cutting/cropping videos. For using it add

compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5'

to dependencies.

For cutting videos run this command

 execFFmpegCommand("-i " + path.getAbsolutePath() + " -ss " + startMs / 1000 + " -to " + endMs / 1000 + " -strict -2 -async 1 " + dest.getAbsolutePath());

where path is the path of original video.

startMs is the initial time of video form where you want to cut(start time of cropped video)

endMs is the time of video upto which you want to cut(end time of cropped video)

dest is the path where you want to save the cut/cropped video

If your filename or foldername contain whitespaces,use String formatter to handle spaces.

private void execFFmpegCommand(final String command) {
    try {
        ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.e("FFMPEG", "FAILED with output : " + s);
            }

            @Override
            public void onSuccess(String s) {
                Log.e("FFMPEG", "SUCCESS with output : " + s);
            }

            @Override
            public void onProgress(String s) {
                Log.e("FFMPEG", "Started command : ffmpeg " + command);
                Log.e("FFMPEG", "progress : " + s);
            }

            @Override
            public void onStart() {
                Log.e("FFMPEG", "Started command : ffmpeg " + command);

            }

            @Override
            public void onFinish() {
                Log.e("FFMPEG", "Finished command : ffmpeg " + command);



            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // do nothing for now
    }
}

Before cutting videos you have to load FFMPEG by calling below method inside onCreate() or onCreateView().Its better to execute this method in AsyncTask.

private void loadFFMpegBinary() {
        try {
            if (ffmpeg == null) {

                ffmpeg = FFmpeg.getInstance(getActivity());
            }
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onFailure() {

                }

                @Override
                public void onSuccess() {
                    Log.e("FFMPEG", "ffmpeg : correct Loaded");
                }
            });
        } catch (FFmpegNotSupportedException e) {

        } catch (Exception e) {

        }
    }

You can also make a RangeSeekbar for allowing user to select the time range of cropped video from original video using anothem/android-range-seek-bar library.

UPDATE

Below link contains ffmpeg video editor tutorial which I have written on my blog which inculdes how to cut video using FFmpeg library-

https://androidlearnersite.wordpress.com/2017/03/17/ffmpeg-video-editor/

Below link contains complete source code for that tutorial-

https://github.com/bhuvnesh123/FFmpeg-Video-Editor-Android

Below is the playstore link for the app created in that turorial-

https://play.google.com/store/apps/details?id=videoeditor.bhuvnesh.com.ffmpegvideoeditor