Trim video Like whatsapp

16,956

Solution 1

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

Solution 2

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 .

Share:
16,956

Related videos on Youtube

dev
Author by

dev

Updated on June 04, 2022

Comments

  • dev
    dev almost 2 years

    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?

  • dev
    dev about 10 years
    Hi navin, thanks for reply. the solution you have given is for andorid?
  • Naveen
    Naveen about 10 years
    yes. you have to include the libraries to your application and you can crop the video
  • dev
    dev about 10 years
    can you provide me example or code how can I implement this?
  • Nativ
    Nativ over 7 years
    Good to know that ffmpeg has High level library for Android. But - (1) can you give an example how to crop video with it? (2) It's licence is GPL which means that you'll have to pay for every installation of your app.
  • Android Developer
    Android Developer about 7 years
    @Nativ There is no need to pay for every installation of app..check out the update in my answer
  • karanatwal.github.io
    karanatwal.github.io over 6 years
    This is trimming , but user is asking for cropping
  • Babs
    Babs over 5 years
    @BhuvneshVarma, any suggestions on how to pass the selected four corner co-ordinates of the video via ffmpeg and perform cropping ?
  • Yogesh Nikam Patil
    Yogesh Nikam Patil over 5 years
    How to reduce video file size before uploading server using this library?
  • android developer
    android developer about 5 years
    @karanatwal.github.io I think the question is about trimming, because that's what WhatsApp has...
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @AndroidDeveloper How do you guys use ffmpeg? Ive seen this issue where it seems incompatible to andriod now link. I already issued a question specific to my problem at link. Im not able to use ffmpeg and as far as I can tell it cant be used since a few years. Your comments are just a few months old though so this is confusing to me.
  • android developer
    android developer about 5 years
    @Smogen I don't use anything yet, but I think this one could be a nice alternative : github.com/titansgroup/k4l-video-trimmer
  • Android Developer
    Android Developer about 5 years
    @Smogen FFMpeg can be used nowadays also..That error comes only in x86 devices.You can use this to fix that..
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @AndroidDeveloper Thanks. Ive used this already. Can you tell me anything about which one is better?
  • android developer
    android developer about 5 years
    @Smogen Can you use it without any issues of license? GPL... Anyway, I've found that I can actually use Android framework itself to trim videos, and wrote about it here: stackoverflow.com/a/54504603/878126 . Problem is that the library I use for the UI has some bugs that I don't know how to reproduce, and I'm not sure if what I wrote is safe in all cases.
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @androiddeveloper I didnt try to trim beginning or end of a video but rather to crop a part of the video feed to show only the upper right quarter for example. Do you know an other alternative? You seem like a real pro when it comes to this stuff. Is there a way to contact you directly?
  • android developer
    android developer about 5 years
    @Smogen You are in luck. I've found a library today that offers cropping and trimming of a video: github.com/VRGsoftUA/VideoCrop . However, for some reason I've failed to import it, so reported here: github.com/VRGsoftUA/VideoCrop/issues/3 . If you know how to fix it and make the project work (I want to try it before I really use it), please write about it there. I don't think the developers look at the issues.
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @androiddeveloper Ive looked at the sourcecode and all they do is use the crop-filter that I also use on ffmpeg. So sadly, this is not really helping me. I did manage to crop my video the way I wanted to with mobile-ffmpeg, but its really slow, so another solution would be nice.
  • android developer
    android developer about 5 years
    @Smogen I don't see that they use ffmpeg. Where do you see that they use it? And how did you run their sample? It failed to build for me...
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @androiddeveloper VideoCropActivity.handleCropStart() Mine just built. However, the app crashed on startup.
  • android developer
    android developer about 5 years
    @Smogen How odd. How did you succeed? I got a weird error while building...
  • Benjamin Basmaci
    Benjamin Basmaci about 5 years
    @AndroidDeveloper I dont know, but as I posted in your issue, we have the same IDE versions. Maybe somehow there are some rouge files or something not accounted for? difficult to tell. I downloaded via .ZIP download and unpacked the project instead of cloning it. Maybe try that way?
  • Pallab Banerjee
    Pallab Banerjee over 3 years
    i have tried using your library and its failing with "failed to trim" toast
  • gowtham6672
    gowtham6672 over 3 years
    use the latest github.com/a914-gowtham/Android-video-trimmer it supports android 10 now