Skip to content

Commit

Permalink
add ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed Jun 8, 2023
1 parent f02d2cc commit b6783b7
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/main/java/com/pancm/ffmpeg/FFmpegTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}


}
69 changes: 69 additions & 0 deletions src/main/java/com/pancm/ffmpeg/FFmpegUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/pancm/ffmpeg/package-info.java
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b6783b7

Please sign in to comment.