简体   繁体   中英

Use ffmpeg to Convert Video to Gif android studio

I am currently making a simple Androidapp that converts a video from the SD card into a gif.

I learnt ffmpeg is the most efficient method to handle the conversion. But I have no idea how to add ffmeg to my android studio project.

In your app's build.gradle add

compile 'com.writingminds:FFmpegAndroid:0.3.2'

After that add a button for selecting the path of video and in the onClick method of the button create a intent that will help the user to select video from his phone

 Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_CONTACT_REQUEST);

Now retreive the above result using the below method

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();

            // OI FILE Manager
            String filemanagerstring = selectedImageUri.getPath();


            // MEDIA GALLERY

            String selectedImagePath =getPath(selectedImageUri);
            if (selectedImagePath != null) {
                String[] cmd = {"-i"
                        , selectedImagePath
                        , "Image.gif"};
                conversion(cmd);

            }
        }
    }
}

define getPath method to get the path of selected video

 public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}

then Create a method to convert the video into gif

 public static void conversion(String[] cmd) {
    FFmpeg ffmpeg = FFmpeg.getInstance(this);

    try {


        // to execute "ffmpeg -version" command you just need to pass "-version"
        ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

            @Override
            public void onStart() {
            }

            @Override
            public void onProgress(String message) {
            }

            @Override
            public void onFailure(String message) {
            }

            @Override
            public void onSuccess(String message) {
            }

            @Override
            public void onFinish() {
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
        e.printStackTrace();
    }
}

you can refer to https://github.com/WritingMinds/ffmpeg-android-java

for JNI, NDK/AS you could start with this

and then check that users (ph0b) SO posts regarding JNI/NDK

Then ffmpeg on android is a pretty tall order for which you will need some time.

Search 'android-ffmpeg' on github looking for highest community rating ( stars, clones, ) and with good build instructions.

IMO - 'guardianproject' and 'halfninja' are good.

You could spend some time outside of android ( just building ffmpeg static libs on a VM ) so you are familiar with just the config/build on ffmpeg.

Then, go to full android studio, using ph0b integration technique for running NDK/build inside gradle builds, using the alterations to gradle in his blog post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM