Home » Android » android – Trim video Like whatsapp

android – Trim video Like whatsapp

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

I think that using ffmpeg as the others have suggested, it has a very restrictive license that requires your app to also be open sourced and have its license, as I remember.

If you wish, you can use VideoTrimmer library I’ve made, which is based on other, permissive libraries .

Answer:

https://lists.ffmpeg.org/pipermail/libav-user/2012-May/001964.html

how to use ffmpeg on android

read about the issue with static vars in ‘ffmpeg.c’ …

I would bet that u have the same problem and need to do something (3 alternate choices) to reset or GC those vars:

  1. get the java classloader that loaded the lib and GC it

  2. in the c-layer do what the OP did in above link

  3. write a 2nd shared lib that uses ‘dlsym’ and ‘dlclose’ on the first library during each call cycle

github , see the ‘README’ here