本帖最后由 苏紫方璇 于 2023-7-27 23:37 编辑
先上ffmpeg软件 下载地址:有window和lunix版本(window解压后添加环境变量,lunix安装有教程不详细介绍)
链接:https://pan.baidu.com/s/1qZEJKKCWTs4n_lA4f77myQ
提取码:52pj
压缩命令依据自己的要求,可自行修改。
代码如下,有帮助给点热心值
[Java] 纯文本查看 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.LocalDateTime;
public class VideoCompressionFFmpegCmd {
public static void main(String[] args) throws IOException {
String inputFileName = "C:\\******\\***.mp4";
String outputFileName = "C:\\******\\***.mp4";
String waterMarkFileName = "C:\\******\\***.png";
String ffmpegPath = "D:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe";
String[] cmd = new String[]{ffmpegPath, "-i", inputFileName,"-i",waterMarkFileName,"-filter_complex" ,"overlay=x=W-w-30:y=30","-preset", "medium", "-crf", "23","-b:v","1M","-c:a","aac","-s","1280x720","-y", outputFileName};
// 执行 FFmpeg 命令
compressVideoFFmpeg(cmd);
}
private static void compressVideoFFmpeg(String[] cmd) throws IOException {
LocalDateTime beginBuildMapTime = LocalDateTime.now();
BufferedReader reader = null;
try {
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process process = pb.start();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 等待 FFmpeg 命令执行完毕
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
LocalDateTime endBuildMapTime = LocalDateTime.now();
System.out.println("FFmpeg耗时:"+Duration.between(beginBuildMapTime,endBuildMapTime).toMillis()+ "毫秒");
}
}
|