cropsly / ffmpeg-android-java

Android java library for FFmpeg binary compiled using https://github.com/writingminds/ffmpeg-android
http://writingminds.github.io/ffmpeg-android-java
GNU General Public License v3.0
3.32k stars 828 forks source link

Add Watermark/Text in video #271

Open rf1804 opened 6 years ago

rf1804 commented 6 years ago

I want to add text/watermark on a video Expected camera_end with watermark

But getting the error

Error ffmpeg executing with message: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers built with gcc 4.8 (GCC) configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags= libavutil 55. 17.103 / 55. 17.103 libavcodec 57. 24.102 / 57. 24.102 libavformat 57. 25.100 / 57. 25.100 libavdevice 57. 0.101 / 57. 0.101 libavfilter 6. 31.100 / 6. 31.100 libswscale 4. 0.100 / 4. 0.100 libswresample 2. 0.101 / 2. 0.101 libpostproc 54. 0.100 / 54. 0.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'file:///storage/emulated/0/DCIM/VID_20180221_145120.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2018-02-21 09:21:22 com.android.version: 8.0.0 Duration: 00:00:01.53, start: 0.000000, bitrate: 1981 kb/s Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuvj420p(pc, smpte170m), 720x480, 1848 kb/s, SAR 1:1 DAR 3:2, 29.51 fps, 30 tbr, 90k tbn, 180k tbc (default) Metadata: rotate : 270 creation_time : 2018-02-21 09:21:22 handler_name : VideoHandle Side data: displaymatrix: rotation of 90.00 degrees Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default) Metadata: creation_time : 2018-02-21 09:21:22 handler_name : SoundHandle Fontconfig error: Cannot load default config file [Parsed_drawtext_0 @ 0xf68adfa0] impossible to init fontconfig [AVFilterGraph @ 0xf68d1040] Error initializing filter 'drawtext' with args 'text=Title of this Video: fontcolor=white: fontsize=24: x=(w-tw)/2: y=(h/PHI)+th box=0:' Error opening filters!

My Code

public void embedTextOnVideo(String text, String path, int fontSize, String fontColor, final Callback successCallback, final Callback errorCallback)
{
 FFmpeg ffmpeg = FFmpeg.getInstance(_reactContext);
 try
 {
  ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onFailure() {}

    @Override
    public void onSuccess() {}

    @Override
    public void onFinish() {}
  });
} catch (FFmpegNotSupportedException e) {
  // Handle if FFmpeg is not supported by device
}

File out = getOutputFile(TYPE_VIDEO);

 String[] cmd = new String[] {
  "-y", "-i", path,  "-vf", "drawtext=text='Title of this Video': fontcolor=white: fontsize=24: x=(w-tw)/2: y=(h/PHI)+th box=0:", out.getAbsolutePath()
};
try {
  ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onProgress(String message) {}

    @Override
    public void onFailure(String message) {
      errorCallback.invoke("Error ffmpeg executing with message:\n\t" + message);
    }

    @Override
    public void onSuccess(String message) {
      successCallback.invoke("Successfully output file with message:\n\t");
    }

    @Override
    public void onFinish() {}
  });
} catch (FFmpegCommandAlreadyRunningException e) {
  // Handle if FFmpeg is already running
}
}

@Nullable
private Throwable writeDataToFile(byte[] data, File file) {
try {
  FileOutputStream fos = new FileOutputStream(file);
  fos.write(data);
  fos.close();
} catch (FileNotFoundException e) {
  return e;
} catch (IOException e) {
  return e;
}

return null;
}
@Nullable
private File getOutputFile(int type) {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM;

// Create storage dir if it does not exist
if (!storageDir.exists()) {
  if (!storageDir.mkdirs()) {
    Log.e(TAG, "Failed to create directory:" + storageDir.getAbsolutePath());
    return null;
  }
}

// media file name
String fileName = String.format("%s", new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()));

enter code hereif (type == TYPE_VIDEO) {
  fileName = String.format("VID_%s.mp4", fileName);
} else {
  Log.e(TAG, "Unsupported media type:" + type);
  return null;
}

return new File(String.format("%s%s%s", storageDir.getPath(), File.separator, fileName));
}
kishorejethava commented 6 years ago

@rf1804 Did you found any solution? I have issue

kiranboghra commented 6 years ago

String[] cmd = new String[] { "-y", "-i", path, "-vf", "drawtext=text='Title of this Video':fontfile=/storage/emulated/0/text.ttf: fontcolor=white: fontsize=24: x=(w-tw)/2: y=(h/PHI)+th box=0:", out.getAbsolutePath() };

Try to add ttf file and run it's worked for me.

neelmevada commented 6 years ago

@kiranboghra I tried your command with below params to add text overlay on video as below String[] cmd2 = new String[] { "-y", "-i", "storage/emulated/0/DCIM/Camera/VID_20180904_215404.mp4", "-vf", "drawtext=text='NeelMevada':fontfile=/storage/emulated/0/DCIM/Camera/Roboto_Bold.ttf: fontcolor=white: fontsize=24: x=(w-tw)/2: y=(h/PHI)+th box=0:", out.getAbsolutePath() };

### Bt it gives me an error as below

ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers built with gcc 4.8 (GCC) configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags= libavutil 55. 17.103 / 55. 17.103 libavcodec 57. 24.102 / 57. 24.102 libavformat 57. 25.100 / 57. 25.100 libavdevice 57. 0.101 / 57. 0.101 libavfilter 6. 31.100 / 6. 31.100 libswscale 4. 0.100 / 4. 0.100 libswresample 2. 0.101 / 2. 0.101 libpostproc 54. 0.100 / 54. 0.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'storage/emulated/0/DCIM/Camera/VID_20180904_215404.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2018-09-04 16:24:09 com.android.version: 6.0 Duration: 00:00:03.88, start: 0.000000, bitrate: 11587 kb/s Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1088, 11667 kb/s, SAR 1:1 DAR 30:17, 19.94 fps, 25 tbr, 90k tbn, 180k tbc (default) Metadata: rotate : 90 creation_time : 2018-09-04 16:24:09 handler_name : VideoHandle Side data: displaymatrix: rotation of -90.00 degrees Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2018-09-04 16:24:09 handler_name : SoundHandle [Parsed_drawtext_0 @ 0xf5e5e680] [Eval @ 0xfffc54d8] Undefined constant or missing '(' in 'thbox=0' [Parsed_drawtext_0 @ 0xf5e5e680] Failed to configure input pad on Parsed_drawtext_0 Error opening filters!

Kindly do let me know where is wrong i am ??

yadavkohi commented 5 years ago

I have tried the following command and it works for me: -i /storage/emulated/0/Movies/Vid.mp4 -vf drawtext=text='Author':fontfile=/storage/emulated/0/Movies/Avenir-Medium.ttf:fontcolor=white:fontsize=32:x=10:y=H-th-10 -preset ultrafast -async 1 /storage/emulated/0/Movies/output.mp4

the ttf file is must for text watermark

junaidandroidcp commented 3 years ago

it does not work on android 10 and 10+