Android ffmpeg : 使用可绘制或资产文件夹中的图像作为水印

Android ffmpeg : Use image for Watermark from drawable or assets folder

如何使用 Android 中可绘制文件夹或资产文件夹中的图像作为水印。

我发现了很多使用命令的示例,但所有示例都使用内部存储的静态图像路径。

我已经完成了以下代码来制作带水印的视频。

    try {
        FFmpeg ffmpeg = FFmpeg.getInstance(mContext);
        // to execute "ffmpeg -version" command you just need to pass "-version"

        String mSelectedPath = PathUtils.getPathFromUri(mContext, selectedUri);
        String mFileName = PathUtils.getFileName(mSelectedPath);
        final String mDestinationPath = "/storage/emulated/0/" + mFileName;

        String[] array = new String[]{"-i", mSelectedPath , "-i", "/storage/emulated/0/logo.png", "-filter_complex", "[0:v][1:v]overlay=main_w-overlay_w-10:10", "-codec:a", "copy", mDestinationPath};

        ffmpeg.execute(array, new ExecuteBinaryResponseHandler() {

            @Override
            public void onStart() {
                Log.d(TAG, "onStart: ");
                Toast.makeText(mContext, "Preparing video...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProgress(String message) {
                Log.d(TAG, "onProgress: " + message);
                Toast.makeText(mContext, "Processing for compression...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(String message) {
                Log.d(TAG, "onFailure: " + message);
                Toast.makeText(mContext, "Failed to upload, Try Again.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(String message) {
                Log.d(TAG, "onSuccess: " + message);
                Toast.makeText(mContext, "Uploading started", Toast.LENGTH_SHORT).show();
                mFinalUploadUri = PathUtils.getUriFromPath(mContext, new File(mDestinationPath));
                uploadVideo();
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "onFinish: ");

            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
        e.printStackTrace();
    }

有谁知道如何使用 drawable 或 assets 文件夹中的图像作为水印?有人可以帮忙吗?

FFMpeg 不是Android环境。 Drawable 和 Assets Directory 仅用于 Android Environment 所以如果你想添加图像作为水印你应该只选择文件存储路径

我创建了 saveImage() 以将可绘制对象保存到外部存储器中。

private void saveImage() {
    //TODO Change logo.png in drawable folder for watermark
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File file = new File(extStorageDirectory, "logo.png");
    if (!file.exists()) {
        try {
            FileOutputStream outStream = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你的命令是:

String[] array = new String[]{"-y", "-i", mSelectedPath, "-i", Environment.getExternalStorageDirectory() + "logo.png", "-filter_complex", "[0:v][1:v]overlay=main_w-overlay_w-10:10", "-codec:a", "copy", mDestinationPath};

注意:您必须获得以下许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

谢谢。