学习JAVA,需用到FFMPEG,在此记录
1.点击打开FFmpeg官网:Download FFmpeghttps://ffmpeg.org/download.html
2.选择Windows版本FFmpeg下载
解压文件:
\bin文件夹下有3个文件ffmpeg.exe, ffplay, ffprobe
将bin文件路径加入到环境变量的Path中
CMD使用 ffmpeg -version 查看是否安装成功
安装完毕后需要重新启动,否则JAVA调用不正常(下面是测试压缩)
package com.hvle.util;
import java.io.File;
public class FFmpegUtil {
/**
* 压缩视频
* @param convertFile 待转换的文件
* @param targetFile 转换后的目标文件
*/
private static void toCompressFile(String convertFile,String targetFile){
try{
Runtime runtime = Runtime.getRuntime();
/**将视频压缩为 每秒15帧 平均码率600k 画面的宽与高 为1280*720*/
String cutCmd="ffmpeg -i " + convertFile + " -r 15 -b:v 600k -s 1280x720 "+ targetFile;
runtime.exec(cutCmd);
System.out.println("文件:"+convertFile+" 正在转换中。。。");
}catch(Exception e){
e.printStackTrace();
System.out.println("压缩文件出现异常:"+e.getMessage());
}
}
public static void cov(){
final String convertFile="D:\\media\\";
File f=new File(convertFile);
File[] fs=f.listFiles();
for(File ff:fs){
if(ff.toPath().toString().toLowerCase().endsWith(".mp4")){
String f1=convertFile+ff.getName();
String f2=convertFile+"压缩后_"+ff.getName();
toCompressFile(f1,f2);
}
}
}
}