1、安装包网盘下载地址:
链接:https://pan.baidu.com/s/1iKUn7ul1JZWbutGr7AOKhg
提取码:d0nj
2、解压安装包到自定义目录,例如:
copy安装目录下的bin,例如:D:\FFmpeg\ffmpeg-4.4\bin
3、配置环境变量
打开“电脑” --> 右键“属性” --> 点击"高级系统设置" --> 点击“环境变量”窗口 --> 在用户变量的“Path”中添加 FFmpeg安装目录,例如:
4、java相关代码
public class FFMpegUtil {
//Windows下 ffmpeg.exe的路径
private static String ffmpegEXE = "D:\\FFmpeg\\ffmpeg-4.4\\bin\\ffmpeg.exe";
public static void main(String[] args) {
String videoInputPath = "C:\\Users\\asus\\Desktop\\02a128c2dd70fde15804122c53babd62.flv";
String jpgName= UUID.randomUUID().toString();
String coverOutputPath = "C:\\Users\\asus\\Desktop\\"+jpgName+".mp4";
try {
convetor(videoInputPath,coverOutputPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param videoInputPath 视频的输入路径
* @param videoOutPath 视频的输出路径
* @throws Exception
*/
// 拷贝视频,并指定新的视频的名字以及格式
// ffmpeg.exe -i old.mp4 new.avi
public static void convetor(String videoInputPath, String videoOutPath) throws Exception {
List<String> command = new ArrayList<String>();
command.add(ffmpegEXE);
command.add("-i");
command.add(videoInputPath);
command.add(videoOutPath);
ProcessBuilder builder = new ProcessBuilder(command);
Process process = null;
try {
process = builder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ((line = br.readLine()) != null) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
}