arjunsinh
arjunsinh

Reputation: 11

How to Add Watermark like tic tok using ffmpeg in android studio

I am trying To add Watermark Like Tic Tok using FFMPEG but it's Not working

        GeneralUtils.checkForPermissionsMAndAbove(MainActivity.this, true);
        LoadJNI vk = new LoadJNI();
        try {

            //String complexCommand = "ffmpeg -y -i /storage/emulated/0/in.mp4 -strict experimental -vf transpose=1 -s 160x120 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k /storage/emulated/0/out.mp4";
            /*String complexCommand = "ffmpeg -i /storage/emulated/0/in.mp4 -framerate 30000/1001 -loop 1 -i /storage/emulated/0/abcd.jpg -filter_complex\n" +
                    "  \"[1:v] fade=out:st=30:d=1:alpha=1 [ov]; [0:v][ov] overlay=10:10 [v]\" -map \"[v]\"\n" +
                    "  -map 0:a -c:v libx264 -c:a copy -shortest /storage/emulated/0/out.mp4";*/
            //String complexCommand = "ffmpeg -y -i /storage/emulated/0/in.mp4 -strict experimental -vf movie=/storage/emulated/0/abcd.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out] -s 320x240 -r 30 -b 15496k -vcodec mpeg4 -ab 48000 -ac 2 -ar 22050 /storage/emulated/0/out.mp4";
            String complexCommand = "ffmpeg -i /storage/emulated/0/in.mp4 -i /storage/emulated/0/abcd.jpg -filter_complex \\\n" +
                    "\"overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2\" \\\n" +
                    "-codec:a copy /storage/emulated/0/out.mp4";

            String workFolder = getApplicationContext().getFilesDir().getAbsolutePath();
            //String[] complexCommand = {"ffmpeg","-i", "/sdcard/videokit/in.mp4"};
             vk.run(GeneralUtils.utilConvertToComplex(complexCommand) , workFolder , getApplicationContext());

            Log.i("test", "ffmpeg4android finished successfully");
        } catch (Throwable e) {
            Log.e("test", "vk run exception.", e);
        }

but noting work for me

as you can i have no knowledge about FFMPEG so if you give answere please write in brief.

thank you.

Upvotes: 1

Views: 813

Answers (2)

Ramesh Jangama
Ramesh Jangama

Reputation: 81

Its nothing to do with android but below are simple ffmpeg commands

Top-Left To Bottom-Right loop:

ffmpeg -i video.mp4 -i watermark.png -filter_complex \
 "[0:v][1:v]overlay=x='if(lt(mod(t,10),5),10,W-w-10)':y='if(lt(mod(t,10),5),10,H-h-10)'" \
 -codec:a copy out.mp4

Top-Right To Bottom-Left loop:

ffmpeg -i video.mp4 -i watermark.png -filter_complex \
 "[0:v][1:v]overlay=x='if(lt(mod(t,10),5),W-w-10,10)':y='if(lt(mod(t,10),5),10,H-h-10)'" \ 
 -codec:a copy out.mp4

Idea is very simple

  • Take every 10sec duration, get remainder sec i.e. mod(t,10)
  • if sec < 5, set top position else bottom position

Upvotes: 1

SaginiChan
SaginiChan

Reputation: 301

START HERE https://drive.google.com/file/d/0B2aT0QoEmtuaN0VJZ2Z4ODY3T2s/view File => New => import module => go find the folder downloaded at step1:

ffmpeg4android_demo_studio\ffmpeg4android_lib

Add permission in AndroidManifest.xml

if the version is higher than Android 6, add the following code in onCreate() of your Activity

 GeneralUtils.checkForPermissionsMAndAbove(MainActivity.this, false);

Alt+Enter if encounter red font, AndroidStudio would help you to modify "build.gradle(Module)"

(Not sure if necessary) In build.gradle(Module)

Android{ ....... defaultConfig{ .... targetSdkVersion ... ndk{ abiFilter "armeabi-v7a" } } } ..... dependencies{ ... compile project(':ffmpeg4android_lib') }

add following code in "gradle.properties"

android.useDeprecatedNdk=true

and then async project How to use WaterMarkHelper(by Johnny Tu) to add watermark on video After import "WaterMarkHelper.java" in your project, declare the following in your Activity

WaterMarkHelper myHelper;

initialize myHelper with arguments

Arguments description:

 String inputVideoPath        (ex:"/sdcard/input.mp4")
 String inputWaterMarkPath    (ex:"/sdcard/waterMark.png")
 String outputVideoPath        (ex:"/sdcard/output.mp4")
 String waterMarkX        the X-coordinate    
     Video width: main_w, Video height: main_h, Image Width: overlay_w, Image Height: overlay_h
 String waterMarkY

Download This

Upvotes: 0

Related Questions