add ffmpeg

This commit is contained in:
pancm 2023-06-08 11:24:10 +08:00
parent f02d2cc6f4
commit b6783b7025
3 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package com.pancm.ffmpeg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* @author pancm
* @Title: FFmpegTest
* @Description: FFmpeg获取视频图片
* 调用FFmpeg的命令获取ts视频的图片
* @Version:1.0.0
* @Since:jdk1.8
* @Date 2021/4/14
**/
public class FFmpegTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "D:\\video\\ts\\25-16_940.ts";
String outputFilePath = "D:\\video\\ts\\t2.jpg";
List<String> command = new ArrayList<String>();
command.add(ffmpegExePath);
command.add("-i");
command.add(inputFilePath);
command.add("-f");
command.add("image2");
command.add("-ss");
command.add("1");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("320*240");
command.add(outputFilePath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
//正常信息和错误信息合并输出
builder.redirectErrorStream(true);
try {
//开始执行命令
Process process = builder.start();
//如果你想获取到执行完后的信息那么下面的代码也是需要的
StringBuffer sbf = new StringBuffer();
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
String resultInfo = sbf.toString();
System.out.println(resultInfo);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,69 @@
package com.pancm.ffmpeg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* @author pancm
* @Title: gb28181_platform
* @Description: FFmpeg相关的工具类
* @Version:1.0.0
* @Since:jdk1.8
* @date 2021/4/15
*/
public class FFmpegUtil {
/**
* 执行ffmpeg的项目命令
* @param cmdList
* @throws IOException
*/
public static void exec(List<String> cmdList) throws IOException {
BufferedReader br = null;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmdList);
//正常信息和错误信息合并输出
builder.redirectErrorStream(true);
//开始执行命令
Process process = builder.start();
StringBuffer sbf = new StringBuffer();
String line = null;
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
String resultInfo = sbf.toString();
System.out.println(resultInfo);
} finally {
if (br != null) {
br.close();
}
}
}
public static void main(String[] args) throws IOException {
String ffmpegExePath = "C:\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "D:\\video\\ts\\25-16_940.ts";
String outputFilePath = "D:\\video\\ts\\t3.jpg";
List<String> command = new ArrayList<String>();
command.add(ffmpegExePath);
command.add("-i");
command.add(inputFilePath);
command.add("-f");
command.add("image2");
command.add("-ss");
command.add("1");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("640*480");
command.add(outputFilePath);
exec(command);
}
}

View File

@ -0,0 +1,9 @@
/**
* @Title: pancm_project
* @Description: ffmpeg的工具类
* @Version:1.0.0
* @Since:jdk1.8
* @author pancm
* @date 2021/1/26
*/
package com.pancm.ffmpeg;