FFmpeg - 找不到ExecuteBinaryResponseHandler - Android/Java

zoe*_*ver 5 java android ffmpeg

我正在尝试为react-native创建一个模块,将视频转换为gif.我对android studio/java几乎没有经验,但我想了解更多!我正在使用此库将视频转换为gif.这是我的代码:

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Error:(43, 31) error: cannot find symbol class ExecuteBinaryResponseHandler
Run Code Online (Sandbox Code Playgroud)

这似乎很奇怪是,因为在对的ffmpeg-的Android的Java文档,它说用几乎一模一样的代码.

赏金

如果您能找到将video.mp4转换为gif的方法,那么您将获得赏金.你没有一定要使用FFmpeg,但您的解决方案具有与Java/Android的工作室工作.

Joh*_*mic 3

首先,您应该正确初始化 ffmpeg。

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.)
            }
Run Code Online (Sandbox Code Playgroud)

只有在onSuccess()您可以使用命令之后。

那么请检查LordNeckbeard的以下回答

所以你的代码应该是这样的:

if (isFFmpegLoaded) {
    // ffmpeg.execute(commands from link from the answer) 
}
Run Code Online (Sandbox Code Playgroud)

请不要忘记删除命令字符串和“ffmpeg”单词中的所有空格。为了使命令更具可读性,我建议像这样构建命令:

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"; 
Run Code Online (Sandbox Code Playgroud)

请确保您拥有处理文件的存储权限,以防您使用外部存储。

基于这个策略 ffmpeg 对我来说效果很好。谢谢,祝你好运!