简体   繁体   English

使用ffmpeg将视频转换为Gif Android Studio

[英]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. 我目前正在制作一个简单的Androidapp,可将SD卡中的视频转换为gif。

I learnt ffmpeg is the most efficient method to handle the conversion. 我了解到ffmpeg是处理转换的最有效方法。 But I have no idea how to add ffmeg to my android studio project. 但是我不知道如何将ffmeg添加到我的android studio项目中。

In your app's build.gradle add 在您应用的build.gradle中添加

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 之后,添加一个用于选择视频路径的按钮,并在该按钮的onClick方法中创建一个意图,该意图将帮助用户从手机中选择视频

 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 定义getPath方法以获取所选视频的路径

 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 然后创建一种将视频转换为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 您可以参考https://github.com/WritingMinds/ffmpeg-android-java

for JNI, NDK/AS you could start with this 对于JNI,NDK / AS,您可以从这里开始

and then check that users (ph0b) SO posts regarding JNI/NDK 然后检查有关JNI / NDK的用户(ph0b)SO帖子

Then ffmpeg on android is a pretty tall order for which you will need some time. 那么android上的ffmpeg是一个很高的要求,您需要一些时间。

Search 'android-ffmpeg' on github looking for highest community rating ( stars, clones, ) and with good build instructions. 在github上搜索“ android-ffmpeg”,以寻找最高的社区评级(星号,克隆号),并提供良好的构建说明。

IMO - 'guardianproject' and 'halfninja' are good. IMO-'guardianproject'和'halfninja'很好。

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. 您可能会在android之外花费一些时间(仅在VM上构建ffmpeg静态库),因此您仅对ffmpeg上的config / build熟悉。

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. 然后,进入完整的android studio,使用ph0b集成技术在gradle构建中运行NDK / build,并使用他的博客文章中的更改进行gradle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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