How to add watermark to android studio project using FFMPEG
- Download this file Download Now
- File => New => import module => go find the folder downloaded at step1:
ffmpeg4android_demo_studio\ffmpeg4android_lib
- Add permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
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 ...
<Add the following code>
ndk{
abiFilter "armeabi-v7a"
}
}
}
.....
dependencies{
...
<Add the following code if doesn't exist>
compile project(':ffmpeg4android_lib')
}
- add following code in “gradle.properties”
android.useDeprecatedNdk=true
How to use WaterMarkHelper 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
example code:
myHelper = new WaterMarkHelper("/sdcard/in.mp4", //be sure that in.mp4 exist in /sdcard/
"/sdcard/watermark.png", //be sure that watermark.png exist in /sdcard/
"/sdcard/testOut.mp4", //
"main_w/2 - overlay_w/2", // in the middle of X-axis
"main_h/2 - overlay_h/2"); // in the middle of Y-axis
- adding watermark on video
myHelper.addVideoWaterMark(MainActivity.this); //MainActivity
there should be try/catch exception outside the above code, press Alt+Enter should work.
example code:
try {
myHelper.addVideoWaterMark(MainActivity.this);
} catch (CommandValidationException e) {
e.printStackTrace();
}