1 Star 0 Fork 0

ws/javm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
FFMPEGExecutor.java 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
YCKJ2817 提交于 4年前 . video encode tool
package cn.angs;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* ClassName: FFMPEGExecutor.java
* Description:
*
* @author wangshun@cloudwalk.cn
* @version 1.0.0
* @date 2021/07/22 13:39
*/
@SuppressWarnings("all")
public class FFMPEGExecutor {
private String ffmpegExecutablePath;
private ArrayList args = new ArrayList();
private Process ffmpeg = null;
private ProcessKiller ffmpegKiller = null;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private InputStream errorStream = null;
public FFMPEGExecutor(String ffmpegExecutablePath) {
this.ffmpegExecutablePath = ffmpegExecutablePath;
}
public void addArgument(String arg) {
args.add(arg);
}
public void execute() throws IOException{
int argsSize = args.size();
List<String> commnd=new ArrayList<>();
commnd.add(0,ffmpegExecutablePath);
for (int i = 0; i < argsSize; i++) {
commnd.add(i + 1,(String) args.get(i));
}
Runtime runtime = Runtime.getRuntime();
ffmpeg= new ProcessBuilder(commnd).start();
try {
ffmpeg.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
ffmpegKiller = new ProcessKiller(ffmpeg);
runtime.addShutdownHook(ffmpegKiller);
inputStream = ffmpeg.getInputStream();
outputStream = ffmpeg.getOutputStream();
errorStream = ffmpeg.getErrorStream();
}
public void executeWithPrint() throws IOException{
int argsSize = args.size();
List<String> commnd=new ArrayList<>();
commnd.add(0,ffmpegExecutablePath);
for (int i = 0; i < argsSize; i++) {
commnd.add(i + 1,(String) args.get(i));
}
Runtime runtime = Runtime.getRuntime();
ffmpeg= new ProcessBuilder(commnd).start();
new PrintStream(ffmpeg.getErrorStream()).start();
new PrintStream(ffmpeg.getInputStream()).start();
try {
ffmpeg.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
ffmpegKiller = new ProcessKiller(ffmpeg);
runtime.addShutdownHook(ffmpegKiller);
}
public String getFfmpegExecutablePath() {
return ffmpegExecutablePath;
}
public InputStream getInputStream() {
return inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public InputStream getErrorStream() {
return errorStream;
}
public void destroy() {
if (inputStream != null) {
try {
inputStream.close();
} catch (Throwable t) {
;
}
inputStream = null;
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Throwable t) {
}
outputStream = null;
}
if (errorStream != null) {
try {
errorStream.close();
} catch (Throwable t) {
;
}
errorStream = null;
}
if (ffmpeg != null) {
ffmpeg.destroy();
ffmpeg = null;
}
if (ffmpegKiller != null) {
Runtime runtime = Runtime.getRuntime();
runtime.removeShutdownHook(ffmpegKiller);
ffmpegKiller = null;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ws410/javm.git
git@gitee.com:ws410/javm.git
ws410
javm
javm
master

搜索帮助