update the command of watermark

update the command of watermark
pull/166/head
xufulong 4 years ago
parent ce2c2561e6
commit be2280eacf
  1. 5
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  2. 46
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -26,7 +26,6 @@ import com.frank.ffmpeg.util.FileUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
@ -51,6 +50,7 @@ public class VideoHandleActivity extends BaseActivity {
private final static int TYPE_IMAGE = 1;
private final static int TYPE_GIF = 2;
private final static int TYPE_TEXT = 3;
private final static int waterMarkType = TYPE_IMAGE;
private String appendPath = PATH + File.separator + "snow.mp4";
private String outputPath1 = PATH + File.separator + "output1.ts";
@ -201,10 +201,9 @@ public class VideoHandleActivity extends BaseActivity {
//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 photo = PATH + File.separator + "hello.png";
String photoMark = PATH + File.separator + "photoMark.mp4";
commandLine = FFmpegUtil.addWaterMarkImg(srcFile, photo, location, bitRate, offsetXY, photoMark);
break;

@ -219,6 +219,20 @@ public class FFmpegUtil {
return screenShotCmd.split(" ");
}
private static String obtainOverlay(int offsetX, int offsetY, int location) {
switch (location) {
case 2:
return "overlay='(main_w-overlay_w)-" + offsetX + ":" + offsetY + "'";
case 3:
return "overlay='" + offsetX + ":(main_h-overlay_h)-" + offsetY + "'";
case 4:
return "overlay='(main_w-overlay_w)-" + offsetX + ":(main_h-overlay_h)-" + offsetY + "'";
case 1:
default:
return "overlay=" + offsetX + ":" + offsetY;
}
}
/**
* add watermark with image to video, you could assign the location and bitRate
*
@ -230,21 +244,11 @@ public class FFmpegUtil {
* @param outputPath output file
* @return add watermark success or not
*/
public static String[] addWaterMarkImg(String inputPath, String imgPath, int location, int bitRate, int offsetXY, String outputPath) {
public static String[] addWaterMarkImg(String inputPath, String imgPath, int location, int bitRate,
int offsetXY, String outputPath) {
String mBitRate = bitRate + "k";
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 overlay = obtainOverlay(offset, offset, location);
String waterMarkCmd = "ffmpeg -i %s -i %s -b:v %s -filter_complex %s -preset:v superfast %s";
waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, mBitRate, overlay, outputPath);
return waterMarkCmd.split(" ");
@ -261,21 +265,11 @@ public class FFmpegUtil {
* @param outputPath output file
* @return add watermark success or not
*/
public static String[] addWaterMarkGif(String inputPath, String imgPath, int location, int bitRate, int offsetXY, String outputPath) {
public static String[] addWaterMarkGif(String inputPath, String imgPath, int location, int bitRate,
int offsetXY, String outputPath) {
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 overlay = obtainOverlay(offset, offset, location) + ":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, inputPath, imgPath, mBitRate, overlay, outputPath);
return waterMarkCmd.split(" ");

Loading…
Cancel
Save