FFmpeg - cannot find ExecuteBinaryResponseHandler

2019-06-22 00:18发布

问题:

I am trying to make a module for react-native that will change a video into a gif. I have little to no experience with android studios/java, but I would love to learn more! I am using this library to convert the video to a gif. Here is my code:

package com.reactlibrary;

import android.widget.Toast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;

public class RNGifMakerModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;

  public RNGifMakerModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "RNGifMakerModule";
  }

  @ReactMethod
  public void alert(String message) {
      Toast.makeText(getReactApplicationContext(), "Error", Toast.LENGTH_LONG).show();
      String[] cmd = {"-i"
              , message
              , "Image.gif"};
      conversion(cmd);
  }

  public void conversion(String[] cmd) {

    FFmpeg ffmpeg = FFmpeg.getInstance(this.reactContext);

    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();
    }
  }
}

And I get this error:

Error:(43, 31) error: cannot find symbol class ExecuteBinaryResponseHandler

This seems odd to be, because in the documentation for ffmpeg-android-java it says to use almost exactly the same code.

Bounty

The bounty will be awarded to you if you can find a way to convert a video.mp4 into a gif. You do not necessarily have to use FFmpeg, but your solution has to work with java/android studios.

回答1:

First of all you should init ffmpeg correctly.

FFmpeg ffmpeg = FFmpeg.getInstance(this.reactContext);

// please add following method after

ffmpeg.loadBinary(new FFmpegLoadBinaryResponseHandler() {
            @Override
            public void onFailure() {
                // probably your device not supported
            }

            @Override
            public void onSuccess() {
                // you should init flag here (isLoaded, isReady etc.)
            }

Only after onSuccess() you can work with commands.

Then please check following answer by LordNeckbeard.

So your code should be something like this:

if (isFFmpegLoaded) {
    // ffmpeg.execute(commands from link from the answer) 
}

Please do not forget to remove all spaces from command's string and "ffmpeg" word. To keep command more readable I will recommend to build command like this:

final String[] command = new String[11]; // example of the first command in the answer
        command[0] = "-y";
        command[1] = "-ss";
        command[2] = "30";
        command[3] = "-t";
        command[4] = "3";
        command[5] = "-i";
        command[6] = "-t";
        command[7] = "filePath";
        command[8] = "-vf"; 
        command[9] = "fps=10,scale=320:-1:flags=lanczos,palettegen";
        command[10] = "palette.png"; 

Please make sure that you have storage permission to work with file just in case you are working on external storage.

Based on this strategy ffmpeg works well for me. Thanks and good luck!



回答2:

First of all, you should use: File - Invalidate Caches/Restart - Invalidate and Restart and try to reimport ExecuteBinaryResponseHan‌dler. If the problem hasn't been resolved you can try the small hack. Inside your project create package com.github.hiteshsondhi88.libffmpeg and class:

public class ExecuteBinaryResponseHandler implements FFmpegExecuteResponseHandler {

    @Override
    public void onSuccess(String message) {

    }

    @Override
    public void onProgress(String message) {

    }

    @Override
    public void onFailure(String message) {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onFinish() {

    }
}

It should be as on image:

Then inside your build.gradle file in defaultConfig block add multiDexEnabled true

Then you will be able to use that class