Trim video Like whatsapp
Asked Answered
S

4

11

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?

Seduction answered 5/3, 2014 at 5:9 Comment(5)
Have you got any solution or example?Lampoon
hey dev, have you got solution for that question i have same task please help me.Cornwell
Hey, did you get any library or something to achieve this ?Hawthorn
@Seduction ..have u got any solution???Feodore
@Feodore take a look at this github.com/a914-gowtham/Android-video-trimmer i've made to support on android 10Mablemabry
M
10

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

Mcvey answered 2/8, 2015 at 7:57 Comment(18)
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.Selmore
@Selmore There is no need to pay for every installation of app..check out the update in my answerMcvey
This is trimming , but user is asking for croppingHarelip
@BhuvneshVarma, any suggestions on how to pass the selected four corner co-ordinates of the video via ffmpeg and perform cropping ?Lumenhour
How to reduce video file size before uploading server using this library?Iolanthe
@Harelip I think the question is about trimming, because that's what WhatsApp has...Shimmer
@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.Caudal
@Smogen I don't use anything yet, but I think this one could be a nice alternative : github.com/titansgroup/k4l-video-trimmerShimmer
@Smogen FFMpeg can be used nowadays also..That error comes only in x86 devices.You can use this to fix that..Mcvey
@AndroidDeveloper Thanks. Ive used this already. Can you tell me anything about which one is better?Caudal
@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: https://mcmap.net/q/740062/-using-mp4parser-how-can-i-handle-videos-that-are-taken-from-uri-and-contentresolver . 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.Shimmer
@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?Caudal
@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.Shimmer
@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.Caudal
@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...Shimmer
@androiddeveloper VideoCropActivity.handleCropStart() Mine just built. However, the app crashed on startup.Caudal
@Smogen How odd. How did you succeed? I got a weird error while building...Shimmer
@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?Caudal
S
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 .

Shimmer answered 11/2, 2019 at 11:36 Comment(0)
P
0

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

Pringle answered 5/3, 2014 at 5:14 Comment(3)
Hi navin, thanks for reply. the solution you have given is for andorid?Seduction
yes. you have to include the libraries to your application and you can crop the videoPringle
can you provide me example or code how can I implement this?Seduction
M
0

Use Android-video-trimmer android library.it's using FFmpeg with exoplpayer.it makes video trimming easier

Mablemabry answered 20/7, 2020 at 7:21 Comment(2)
i have tried using your library and its failing with "failed to trim" toastWaxwork
use the latest github.com/a914-gowtham/Android-video-trimmer it supports android 10 nowMablemabry

© 2022 - 2024 — McMap. All rights reserved.