process space in the path with multi-input

pull/209/head
xufuji456 3 years ago
parent 4dd4ce954b
commit 2d43ced77b
  1. 67
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -21,7 +21,7 @@ public class FFmpegUtil {
* insert inputPath and outputPath into target array * insert inputPath and outputPath into target array
*/ */
private static String[] insert(String[] cmd, int position, String inputPath, String outputPath) { private static String[] insert(String[] cmd, int position, String inputPath, String outputPath) {
if (cmd == null || inputPath == null || position < 1) { if (cmd == null || inputPath == null || position < 2) {
return cmd; return cmd;
} }
int len = (outputPath != null ? (cmd.length + 2) : (cmd.length + 1)); int len = (outputPath != null ? (cmd.length + 2) : (cmd.length + 1));
@ -35,6 +35,24 @@ public class FFmpegUtil {
return result; return result;
} }
public static String[] insert(String[] cmd, int position1, String inputPath1,
int position2, String inputPath2, String outputPath) {
if (cmd == null || inputPath1 == null || position1 < 2 || inputPath2 == null || position2 < 4) {
return cmd;
}
int len = (outputPath != null ? (cmd.length + 3) : (cmd.length + 2));
String[] result = new String[len];
System.arraycopy(cmd, 0, result, 0, position1);
result[position1] = inputPath1;
System.arraycopy(cmd, position1, result, position1 + 1, position2 - position1 - 1);
result[position2] = inputPath2;
System.arraycopy(cmd, position2 - 1, result, position2 + 1, cmd.length - (position2 - 1));
if (outputPath != null) {
result[result.length - 1] = outputPath;
}
return result;
}
/** /**
* transform audio, according to your assigning the output format * transform audio, according to your assigning the output format
* *
@ -409,9 +427,9 @@ public class FFmpegUtil {
int offsetXY, String outputPath) { int offsetXY, String outputPath) {
String mBitRate = bitRate + "k"; String mBitRate = bitRate + "k";
String overlay = obtainOverlay(offsetXY, offsetXY, location); String overlay = obtainOverlay(offsetXY, offsetXY, location);
String waterMarkCmd = "ffmpeg -i %s -i %s -b:v %s -filter_complex %s -preset:v superfast %s"; String waterMarkCmd = "ffmpeg -i -i -b:v %s -filter_complex %s -preset:v superfast";
waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, mBitRate, overlay, outputPath); waterMarkCmd = String.format(waterMarkCmd, mBitRate, overlay);
return waterMarkCmd.split(" "); return insert(waterMarkCmd.split(" "), 2, inputPath, 4, imgPath, outputPath);
} }
/** /**
@ -430,9 +448,9 @@ public class FFmpegUtil {
String mBitRate = bitRate + "k"; String mBitRate = bitRate + "k";
int offset = ScreenUtil.INSTANCE.dp2px(FFmpegApplication.getInstance(), offsetXY); int offset = ScreenUtil.INSTANCE.dp2px(FFmpegApplication.getInstance(), offsetXY);
String overlay = obtainOverlay(offset, offset, location) + ":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"; String waterMarkCmd = "ffmpeg -i -ignore_loop 0 -i -b:v %s -filter_complex %s -preset:v superfast";
waterMarkCmd = String.format(waterMarkCmd, inputPath, imgPath, mBitRate, overlay, outputPath); waterMarkCmd = String.format(waterMarkCmd, mBitRate, overlay);
return waterMarkCmd.split(" "); return insert(waterMarkCmd.split(" "), 2, inputPath, 6, imgPath, outputPath);
} }
/** /**
@ -481,11 +499,11 @@ public class FFmpegUtil {
*/ */
public static String[] generateGifByPalette(String inputPath, String palette, int startTime, int duration, public static String[] generateGifByPalette(String inputPath, String palette, int startTime, int duration,
int frameRate, int width, String outputPath) { int frameRate, int width, String outputPath) {
String paletteGifCmd = "ffmpeg -ss %d -accurate_seek -t %d -i %s -i %s -lavfi fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]" + String paletteGifCmd = "ffmpeg -ss %d -accurate_seek -t %d -i -i -lavfi fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]" +
"paletteuse=dither=bayer:bayer_scale=3 %s"; "paletteuse=dither=bayer:bayer_scale=3";
paletteGifCmd = String.format(Locale.getDefault(), paletteGifCmd, startTime, paletteGifCmd = String.format(Locale.getDefault(), paletteGifCmd, startTime,
duration, inputPath, palette, frameRate, width, outputPath); duration, frameRate, width);
return paletteGifCmd.split(" "); return insert(paletteGifCmd.split(" "), 7, inputPath, 9, palette, outputPath);
} }
/** /**
@ -543,12 +561,11 @@ public class FFmpegUtil {
* @return join success or not * @return join success or not
*/ */
public static String[] multiVideo(String input1, String input2, String outputPath, int videoLayout) { public static String[] multiVideo(String input1, String input2, String outputPath, int videoLayout) {
String multiVideo = "ffmpeg -i %s -i %s -filter_complex hstack %s";//hstack: horizontal String multiVideo = "ffmpeg -i -i -filter_complex hstack";//hstack: horizontal
if (videoLayout == VideoLayout.LAYOUT_VERTICAL) {//vstack: vertical if (videoLayout == VideoLayout.LAYOUT_VERTICAL) {//vstack: vertical
multiVideo = multiVideo.replace("hstack", "vstack"); multiVideo = multiVideo.replace("hstack", "vstack");
} }
multiVideo = String.format(multiVideo, input1, input2, outputPath); return insert(multiVideo.split(" "), 2, input1, 4, input2, outputPath);
return multiVideo.split(" ");
} }
/** /**
@ -608,17 +625,17 @@ public class FFmpegUtil {
/** /**
* convert videos into picture-in-picture mode * convert videos into picture-in-picture mode
* *
* @param inputFile1 input one * @param inputPath1 input one
* @param inputFile2 input two * @param inputPath2 input two
* @param outputPath output file * @param outputPath output file
* @param x x coordinate point * @param x x coordinate point
* @param y y coordinate point * @param y y coordinate point
* @return convert success or not * @return convert success or not
*/ */
public static String[] picInPicVideo(String inputFile1, String inputFile2, int x, int y, String outputPath) { public static String[] picInPicVideo(String inputPath1, String inputPath2, int x, int y, String outputPath) {
String pipVideo = "ffmpeg -i %s -i %s -filter_complex overlay=%d:%d %s"; String pipVideo = "ffmpeg -i -i -filter_complex overlay=%d:%d";
pipVideo = String.format(Locale.getDefault(), pipVideo, inputFile1, inputFile2, x, y, outputPath); pipVideo = String.format(Locale.getDefault(), pipVideo, x, y);
return pipVideo.split(" "); return insert(pipVideo.split(" "), 2, inputPath1, 4, inputPath2, outputPath);
} }
/** /**
@ -736,15 +753,13 @@ public class FFmpegUtil {
* @return command of inserting picture * @return command of inserting picture
*/ */
public static String[] insertPicIntoVideo(String inputPath, String picturePath, String outputPath) { public static String[] insertPicIntoVideo(String inputPath, String picturePath, String outputPath) {
String insertPicCmd = "ffmpeg -i %s -i %s -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic %s"; String insertPicCmd = "ffmpeg -i -i -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic";
insertPicCmd = String.format(insertPicCmd, inputPath, picturePath, outputPath); return insert(insertPicCmd.split(" "), 2, inputPath, 4, picturePath, outputPath);
return insertPicCmd.split(" ");
} }
public static String[] addSubtitleIntoVideo(String inputPath, String subtitlePath, String outputPath) { public static String[] addSubtitleIntoVideo(String inputPath, String subtitlePath, String outputPath) {
String subtitleCmd = "ffmpeg -i %s -i %s -map 0:v -map 0:a -map 1:s -c copy %s"; String subtitleCmd = "ffmpeg -i -i -map 0:v -map 0:a -map 1:s -c copy";
subtitleCmd = String.format(subtitleCmd, inputPath, subtitlePath, outputPath); return insert(subtitleCmd.split(" "), 2, inputPath, 4, subtitlePath, outputPath);
return subtitleCmd.split(" ");
} }
/** /**

Loading…
Cancel
Save