java - FFmpeg - 找不到 ExecuteBinaryResponseHandler - Android/Java

标签 java android ffmpeg

我正在尝试为 react-native 制作一个模块,它将视频转换为 gif。我对 android studios/java 几乎没有经验,但我很想了解更多!我正在使用 this library将视频转换为 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();
    }
  }
}

我得到这个错误:

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

这似乎很奇怪,因为在 the documentation for ffmpeg-android-java 中它说要使用几乎 完全相同的代码。

赏金

如果您能找到将 video.mp4 转换为 gif 的方法,您将获得赏金。您不一定必须使用 FFmpeg,但您的解决方案必须适用于 java/android 工作室。

最佳答案

首先你应该正确地初始化 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.)
            }

只有在 onSuccess() 之后,您才能使用命令。

然后请检查以下answer来自 LordNeckbeard。

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

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

请不要忘记删除命令字符串和“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"; 

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

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

关于java - FFmpeg - 找不到 ExecuteBinaryResponseHandler - Android/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549701/

相关文章:

java - Android 库中的 Android NoClassDefFound

java - 在 Windows 7 中运行进程并获取其 ID

java - 如何将 EditText 中的 PHP 变量的值传递到 Android Studio?

android - AAPT : No resource found that matches the given name

android - 带圆角的 FrameLayout

android,videoView打不开这个视频在线视频

ffmpeg - drawbox 不能在远程服务器上工作,只能在本地工作

FFMPEG:剪切、添加水印和合并

java - JScrollPane 中的 GridBagLayout 无法正确调整大小

ios - 初始化 FlutterFFmpegConfig() 时,Flutter 应用程序在 Release模式下崩溃;