java怎么给视频文件压缩和加水印
时间: 2024-06-12 17:06:47 浏览: 145
Java可以使用FFmpeg库来压缩视频文件和加水印。以下是一个简单的示例代码:
压缩视频文件:
```
String inputFilePath = "input.mp4";
String outputFilePath = "output.mp4";
String[] cmd = {"ffmpeg", "-i", inputFilePath, "-c:v", "libx264", "-crf", "20", "-preset", "slow", "-c:a", "copy", outputFilePath};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
```
加水印:
```
String inputFilePath = "input.mp4";
String outputFilePath = "output.mp4";
String watermarkPath = "watermark.png";
String[] cmd = {"ffmpeg", "-i", inputFilePath, "-i", watermarkPath, "-filter_complex", "overlay=10:10", "-codec:a", "copy", outputFilePath};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
```
以上代码仅供参考,实际使用中需要根据具体需求进行修改和扩展。同时,需要注意安装FFmpeg库并将其加入系统环境变量中。
阅读全文