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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With