ffmpeg在视频中添加水印/文本反应原生

时间:2018-02-21 07:47:03

标签: android react-native ffmpeg android-ffmpeg

现在我被困在android部分,这是我的代码

我不想定义字体样式或盒子它只是我在某个地方找到了这个代码所以我只是复制粘贴它,因为我不知道ffmpeg,我只想要一个简单的正确文本像这样在视频右上方的2行中对齐。 Expected output

我在这部分代码中收到错误,因为视频已生成但无法播放且总是262B大小

 String[] cmd = new String[] {
            "-i", path, "-vf", String.format("drawtext=\"fontfile=/systems/fonts/DroidSans.ttf: text='%s': " + "box=1: boxcolor=black@0.5: boxborder=5: x=(w-text_w)/t: y=(h-text_h)/2\"", text), "-codec:a", "aac", out.getAbsolutePath()
    };

这是完整的代码

@ReactMethod
public void embedTextOnVideo(String text, String path, int fontSize, String fontColor, final Callback successCallback, final Callback errorCallback)
{
 FFmpeg ffmpeg = FFmpeg.getInstance(_reactContext);
 try
 {
  ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onFailure() {}

    @Override
    public void onSuccess() {}

    @Override
    public void onFinish() {}
  });
} catch (FFmpegNotSupportedException e) {
  // Handle if FFmpeg is not supported by device
}

File out = getOutputFile(TYPE_VIDEO);

String[] cmd = new String[] {
        "-i", path, "-vf", String.format("drawtext=\"fontfile=/systems/fonts/DroidSans.ttf: text='%s': " + "box=1: boxcolor=black@0.5: boxborder=5: x=(w-text_w)/t: y=(h-text_h)/2\"", text), "-codec:a", "aac", out.getAbsolutePath()
};
try {
  ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onProgress(String message) {}

    @Override
    public void onFailure(String message) {
      errorCallback.invoke("Error ffmpeg executing with message:\n\t" + message);
    }

    @Override
    public void onSuccess(String message) {
      successCallback.invoke("Successfully output file with message:\n\t");
    }

    @Override
    public void onFinish() {}
  });
} catch (FFmpegCommandAlreadyRunningException e) {
  // Handle if FFmpeg is already running
}
}

@Nullable
private Throwable writeDataToFile(byte[] data, File file) {
try {
  FileOutputStream fos = new FileOutputStream(file);
  fos.write(data);
  fos.close();
} catch (FileNotFoundException e) {
  return e;
} catch (IOException e) {
  return e;
}

return null;
}
@Nullable
private File getOutputFile(int type) {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM;

// Create storage dir if it does not exist
if (!storageDir.exists()) {
  if (!storageDir.mkdirs()) {
    Log.e(TAG, "Failed to create directory:" + storageDir.getAbsolutePath());
    return null;
  }
}

// media file name
String fileName = String.format("%s", new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()));


enter code hereif (type == TYPE_VIDEO) {
  fileName = String.format("VID_%s.mp4", fileName);
} else {
  Log.e(TAG, "Unsupported media type:" + type);
  return null;
}

return new File(String.format("%s%s%s", storageDir.getPath(), File.separator, fileName));
}

0 个答案:

没有答案