在ubuntu安装ffmpeg,windows安装以方便测试使用(windows的logo路径需要转义盘符,不然会报错,E\\\\:)。通过执行命令行增加水印,注意linux下执行命令和windows不同。
ubuntu安装命令
apt install ffmpeg
windows需要配置环境变量
ffmpeg免编译版本下载-WindowsServer文档类资源-CSDN下载
代码如下:
package com.xxxx.yyyy;
import java.nio.charset.StandardCharsets;
/**
* * @Creator Jacky
* * @CreateTime 2022/3/23
* * @Description
*/
public class FfmpegService {
public static String LINUX_PATH = "/usr/bin/ffmpeg";
public static String WINDOWS_PATH = "E:\\tools\\ffmpeg\\bin\\ffmpeg.exe";
public static boolean videoWaterMark(String inputPath,String outputPath,String logoPath,Integer logoLocation) {
String exeTools = "";
StringBuilder cmd = new StringBuilder();
if (!System.getProperty("os.name").contains("Windows")) {
// 非windows 系统
exeTools =LINUX_PATH;
cmd.append(exeTools)
.append(" -i ")
.append(inputPath)
.append(" -i ")
.append(logoPath)
.append(" -filter_complex overlay=")
.append(getLocation(logoLocation))
.append(" ")
.append(outputPath);
}else{
exeTools = WINDOWS_PATH;
cmd.append(exeTools)
.append(" -i ")
.append(inputPath)
.append(" -vf ")
.append(" \"")
.append("movie=")
.append(logoPath)
.append("[watermark];[in][watermark] overlay=")
.append(getLocation(logoLocation))
.append("[out]\"")
//.append("\" ")
.append(" ")
.append(outputPath);
}
System.out.println(cmd);
boolean result = true;
try {
String cmdInfo = new String(cmd.toString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
System.out.println(cmdInfo);
Process proc = Runtime.getRuntime().exec(cmdInfo);
HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
error.start();
output.start();
proc.waitFor();
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
private static String getLocation(int location){
switch (location){
case 1 : return "10:10"; //左上
case 2 : return "main_w-overlay_w-10:10"; //右上
case 3 : return "10:main_h-overlay_h-10"; //左下
case 4 : return "main_w-overlay_w-10:main_h-overlay_h-10";//右下
default: return "main_w-overlay_w-10:10";
}
}
public static void main(String[] args) {
String inputPath = "E:\\tools\\testtest.mp4";
String inputPathUrl = "http://192.168.1.21:8089/testtest.mp4";
String outputPath = "E:\\tools\\testtestAABB.mp4";
String logoPath = "E\\\\:/tools/logo.png";
videoWaterMark(inputPath,outputPath,logoPath,2);
}
}