From 180e70f81e586b9df8248f522625bce8b9a4799e Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Sun, 10 May 2020 23:05:09 +0800 Subject: [PATCH] add gif waterMark add gif waterMark --- .../ffmpeg/activity/VideoHandleActivity.java | 54 ++++++++++++----- .../com/frank/ffmpeg/util/FFmpegUtil.java | 58 +++++++++++++++++-- .../com/frank/ffmpeg/util/ScreenUtil.java | 18 ++++++ 3 files changed, 112 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java index 1f010cb..d162480 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java @@ -1,6 +1,7 @@ package com.frank.ffmpeg.activity; import android.annotation.SuppressLint; +import android.media.MediaMetadataRetriever; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -16,6 +17,7 @@ import com.frank.ffmpeg.format.VideoLayout; import com.frank.ffmpeg.handler.FFmpegHandler; import com.frank.ffmpeg.model.MediaBean; import com.frank.ffmpeg.tool.JsonParseTool; +import com.frank.ffmpeg.util.BitmapUtil; import com.frank.ffmpeg.util.FFmpegUtil; import com.frank.ffmpeg.util.FileUtil; @@ -41,6 +43,10 @@ public class VideoHandleActivity extends BaseActivity { private FFmpegHandler ffmpegHandler; private final static boolean useFFmpegCmd = true; + private final static int TYPE_IMAGE = 1; + private final static int TYPE_GIF = 2; + private final static int TYPE_TEXT = 3; + @SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @Override @@ -165,19 +171,41 @@ public class VideoHandleActivity extends BaseActivity { commandLine = FFmpegUtil.screenShot(srcFile, time, screenShot); break; case R.id.btn_water_mark://add watermark to video - // picture - String photo = PATH + File.separator + "launcher.png"; - String photoMark = PATH + File.separator + "photoMark.mp4"; - String mResolution = "720x1280"; - int bitRate = 1024; - commandLine = FFmpegUtil.addWaterMark(srcFile, photo, mResolution, bitRate, photoMark); - // text -// String text = "Hello,FFmpeg"; -// String textPath = PATH + File.separator + "text.jpg"; -// boolean result = BitmapUtil.textToPicture(textPath, text, this); -// Log.i(TAG, "text to pitcture result=" + result); -// String textMark = PATH + File.separator + "textMark.mp4"; -// commandLine = FFmpegUtil.addWaterMark(srcFile, textPath, textMark); + // the unit of bitRate is kb + int bitRate = 500; + MediaMetadataRetriever retriever = new MediaMetadataRetriever(); + retriever.setDataSource(srcFile); + String mBitRate = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); + if (mBitRate != null && !mBitRate.isEmpty()) { + int probeBitrate = Integer.valueOf(mBitRate); + bitRate = (probeBitrate/1000/100) * 100; + } + //1:top left 2:top right 3:bottom left 4:bottom right + int location = 2; + int offsetXY = 5; + int waterMarkType = 1; + switch (waterMarkType) { + case TYPE_IMAGE:// image + String photo = PATH + File.separator + "launcher.png"; + String photoMark = PATH + File.separator + "photoMark.mp4"; + commandLine = FFmpegUtil.addWaterMarkImg(srcFile, photo, location, bitRate, offsetXY, photoMark); + break; + case TYPE_GIF:// gif + String gifPath = PATH + File.separator + "ok.gif"; + String gifWaterMark = PATH + File.separator + "gifWaterMark.mp4"; + commandLine = FFmpegUtil.addWaterMarkGif(srcFile, gifPath, location, bitRate, offsetXY, gifWaterMark); + break; + case TYPE_TEXT:// text + String text = "Hello,FFmpeg"; + String textPath = PATH + File.separator + "text.jpg"; + boolean result = BitmapUtil.textToPicture(textPath, text, this); + Log.i(TAG, "text to picture result=" + result); + String textMark = PATH + File.separator + "textMark.mp4"; + commandLine = FFmpegUtil.addWaterMarkImg(srcFile, textPath, location, bitRate, offsetXY, textMark); + break; + default: + break; + } break; case R.id.btn_generate_gif://convert video into gif String Video2Gif = PATH + File.separator + "Video2Gif.gif"; diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index 4742d12..05aba19 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -2,6 +2,7 @@ package com.frank.ffmpeg.util; import android.annotation.SuppressLint; +import com.frank.ffmpeg.FFmpegApplication; import com.frank.ffmpeg.format.VideoLayout; import java.util.List; @@ -221,17 +222,64 @@ public class FFmpegUtil { } /** - * add watermark to video, you could assign the resolution and bitRate + * add watermark with image to video, you could assign the location and bitRate * * @param srcFile input file - * @param waterMark the path of the watermark + * @param imgPath the path of the image + * @param location the location in the video(1:top left 2:top right 3:bottom left 4:bottom right) + * @param bitRate bitRate + * @param offsetXY the offset of x and y in the video * @param targetFile output file * @return add watermark success or not */ - public static String[] addWaterMark(String srcFile, String waterMark, String resolution, int bitRate, String targetFile) { + public static String[] addWaterMarkImg(String srcFile, String imgPath, int location, int bitRate, int offsetXY, String targetFile) { String mBitRate = bitRate + "k"; - String waterMarkCmd = "ffmpeg -i %s -i %s -s %s -b:v %s -filter_complex overlay=0:0 %s"; - waterMarkCmd = String.format(waterMarkCmd, srcFile, waterMark, resolution, mBitRate, targetFile); + String overlay; + int offset = ScreenUtil.dp2px(FFmpegApplication.getInstance(), offsetXY); + if (location == 1) { + overlay = "overlay='" + offset + ":" + offset + "'"; + } else if (location == 2) { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":" + offset + "'"; + } else if (location == 3) { + overlay = "overlay='" + offset + ":(main_h-overlay_h)-" + offset + "'"; + } else if (location == 4) { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":(main_h-overlay_h)-" + offset + "'"; + } else { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":" + offset + "'"; + } + String waterMarkCmd = "ffmpeg -i %s -i %s -b:v %s -filter_complex %s -preset:v superfast %s"; + waterMarkCmd = String.format(waterMarkCmd, srcFile, imgPath, mBitRate, overlay, targetFile); + return waterMarkCmd.split(" "); + } + + /** + * add watermark with gif to video, you could assign the location and bitRate + * + * @param srcFile input file + * @param imgPath the path of the gif + * @param location the location in the video(1:top left 2:top right 3:bottom left 4:bottom right) + * @param bitRate bitRate + * @param offsetXY the offset of x and y in the video + * @param targetFile output file + * @return add watermark success or not + */ + public static String[] addWaterMarkGif(String srcFile, String imgPath, int location, int bitRate, int offsetXY, String targetFile) { + String mBitRate = bitRate + "k"; + String overlay; + int offset = ScreenUtil.dp2px(FFmpegApplication.getInstance(), offsetXY); + if (location == 1) { + overlay = "overlay='" + offset + ":" + offset + ":shortest=1'"; + } else if (location == 2) { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":" + offset + ":shortest=1'"; + } else if (location == 3) { + overlay = "overlay='" + offset + ":(main_h-overlay_h)-" + offset + ":shortest=1'"; + } else if (location == 4) { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":(main_h-overlay_h)-" + offset + ":shortest=1'"; + } else { + overlay = "overlay='(main_w-overlay_w)-" + offset + ":" + offset + ":shortest=1'"; + } + String waterMarkCmd = "ffmpeg -i %s -ignore_loop 0 -i %s -b:v %s -filter_complex %s -preset:v superfast %s"; + waterMarkCmd = String.format(waterMarkCmd, srcFile, imgPath, mBitRate, overlay, targetFile); return waterMarkCmd.split(" "); } diff --git a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java index fddf183..24b89da 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/ScreenUtil.java @@ -32,4 +32,22 @@ public class ScreenUtil { return displayMetrics != null ? displayMetrics.heightPixels : 0; } + public static int dp2px(Context context, int dpValue) { + if (context == null) { + return 0; + } + DisplayMetrics displayMetrics = getDisplayMetrics(context); + float density = displayMetrics != null ? displayMetrics.density : 0; + return (int) (dpValue * density + 0.5f); + } + + public static int px2dp(Context context, int pxValue) { + if (context == null) { + return 0; + } + DisplayMetrics displayMetrics = getDisplayMetrics(context); + float density = displayMetrics != null ? displayMetrics.density : 0; + return (int) (pxValue / density + 0.5f); + } + }