4

How to use image for watermark from drawable folder or assets folder in Android.

I found many examples with command but all the examples are working with static image path of internal storage.

I have done following code to make video with watermark.

    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();
    }

Anyone know how to use image for watermark from drawable or assets folder? Can anyone help?

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • Please use this helpful links. https://stackoverflow.com/a/38299320/3992798 I did it with watermarking and change size of water marking. – parik dhakan Sep 17 '19 at 05:30

2 Answers2

5

FFMpeg is not Android Environment . Drawable and Assets Directory is only use for Android Environment So if you wish to add Image as water mark you should go for File storage path only

Vinesh Chauhan
  • 1,288
  • 11
  • 27
2

I have created saveImage() to save drawable into external storage.

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();
        }
    }
}

And your command will be:

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};

Note : You must have to take permission as below:

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

Thank you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437