Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 225
Pratik Butani Avatar asked Mar 03 '23 11:03

Pratik Butani


2 Answers

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

like image 118
Vinesh Chauhan Avatar answered May 10 '23 12:05

Vinesh Chauhan


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.

like image 22
Pratik Butani Avatar answered May 10 '23 14:05

Pratik Butani