Java调用FFmpeg进行视频处理及Builder设计模式的应用
1、FFmpeg是什么


2、Java调用FFmpeg

C:\workspace\project\greejoy\picManager\web\tools\ffmpeg\bin\ffmpeg.exe -i C:\Users\Dulk\Desktop\ukulele\01\《小星星》.mp4 -f image2 -ss 10 -t 0.001 -s 320*240 C:\Users\Dulk\Desktop\ukulele\01\littleStar.jpg
C:\workspace\project\greejoy\picManager\web\tools\ffmpeg\bin\ffmpeg.exe -i C:\Users\Dulk\Desktop\ukulele\01\《小星星》.mp4 -f image2 -ss 10 -t 0.001 -s 320*240 C:\Users\Dulk\Desktop\ukulele\01\littleStar.jpg
- C:\workspace\project\greejoy\picManager\web\tools\ffmpeg\bin\ffmpeg.exe 指 ffmpeg.exe 的路径
- C:\Users\Dulk\Desktop\ukulele\01\《小星星》.mp4 指源视频路径
- C:\Users\Dulk\Desktop\ukulele\01\littleStar.jpg 指截图的输出路径
- -i 表示输入文件的命令
- -f 表示输出格式,image2表示输出为图片
- -ss 表示指定的起始位置,这里设置为10,即10s处
- -t 表示设置录制/转码时长,既然截图就0.001就足够了
- -s 表示size,设置帧大小

public class FFmpegTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJava.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("10");
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();
}
}
}
public class FFmpegTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJava.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("10");
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();
}
}
}


- 操作执行信息的读取是分为正常信息和错误信息,分别需要使用 getInputStream() 和 getErrorStream() 读取,上例使用了 builder.redirectErrorStream(true); 将两者合并输出
- 如果要使用 Process.waitFor() 则需要小心阻塞问题,此处不展开(FFmpeg在JAVA中的使用以及Process.waitFor()引发的阻塞问题)
3、Builder设计模式的应用
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("10");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("320*240");
command.add(outputFilePath);
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("10");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("320*240");
command.add(outputFilePath);
/**
* FFmpeg命令的封装类
*/
public class FFmpegCommand {
private List<String> command;
public FFmpegCommand(List<String> command) {
this.command = command == null ? new ArrayList<String>() : command;
}
public List<String> getCommand() {
return command;
}
public void setCommand(List<String> command) {
this.command = command;
}
/**
* 开始执行命令
*
* @param callback 回调
* @return 命令的信息输出
* @throws FFmpegCommandException
*/
public String start(FFmpegCallback callback) throws FFmpegCommandException {
BufferedReader br = null;
StringBuffer sbf = new StringBuffer();
String resultInfo = null;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
//正常信息和错误信息合并输出
builder.redirectErrorStream(true);
//开启执行子线程
Process process = builder.start();
String line = null;
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
resultInfo = sbf.toString();
//等待命令子线程执行完成
int exitValue = process.waitFor();
//完成后执行回调
if (exitValue == 0 && callback != null) {
callback.complete(resultInfo);
}
//销毁子线程
process.destroy();
} catch (IOException e) {
e.printStackTrace();
throw new FFmpegCommandException(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
throw new FFmpegCommandException("线程阻塞异常:" + e.getMessage());
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultInfo;
}
}
/**
* FFmpeg命令的封装类
*/
public class FFmpegCommand {
private List<String> command;
public FFmpegCommand(List<String> command) {
this.command = command == null ? new ArrayList<String>() : command;
}
public List<String> getCommand() {
return command;
}
public void setCommand(List<String> command) {
this.command = command;
}
/**
* 开始执行命令
*
* @param callback 回调
* @return 命令的信息输出
* @throws FFmpegCommandException
*/
public String start(FFmpegCallback callback) throws FFmpegCommandException {
BufferedReader br = null;
StringBuffer sbf = new StringBuffer();
String resultInfo = null;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
//正常信息和错误信息合并输出
builder.redirectErrorStream(true);
//开启执行子线程
Process process = builder.start();
String line = null;
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
resultInfo = sbf.toString();
//等待命令子线程执行完成
int exitValue = process.waitFor();
//完成后执行回调
if (exitValue == 0 && callback != null) {
callback.complete(resultInfo);
}
//销毁子线程
process.destroy();
} catch (IOException e) {
e.printStackTrace();
throw new FFmpegCommandException(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
throw new FFmpegCommandException("线程阻塞异常:" + e.getMessage());
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultInfo;
}
}
public class FFmpegCommandBuilder {
List<String> command = new ArrayList<String>();
public FFmpegCommandBuilder(String exePath) {
if (exePath == null) {
throw new FFmpegCommandRuntimeException("ffmpeg.exe 路径不得为空");
}
//添加命令的exe执行文件位置
command.add(exePath);
}
/**
* 添加输入文件的路径
*
* @param inputFilePath
*/
public FFmpegCommandBuilder input(String inputFilePath) {
if (inputFilePath != null) {
command.add("-i");
command.add(inputFilePath);
}
return this;
}
/**
* 添加输出文件的路径
*
* @param outputFilePath
*/
public FFmpegCommandBuilder output(String outputFilePath) {
if (outputFilePath != null) {
command.add(outputFilePath);
}
return this;
}
/**
* 覆盖输出文件
*/
public FFmpegCommandBuilder override() {
command.add("-y");
return this;
}
/**
* 强制输出格式
*
* @param format 输出格式
*/
public FFmpegCommandBuilder format(FFmpegCommandFormatEnum format) {
if (format != null) {
command.add("-f");
command.add(format.getValue());
}
return this;
}
/**
* 设置录制/转码的时长
*
* @param duration 形如 0.001 表示0.001秒,hh:mm:ss[.xxx]格式的记录时间也支持
*/
public FFmpegCommandBuilder duration(String duration) {
if (duration != null) {
command.add("-t");
command.add(duration);
}
return this;
}
/**
* 搜索到指定的起始时间
*
* @param position 形如 17 表示17秒,[-]hh:mm:ss[.xxx]的格式也支持
*/
public FFmpegCommandBuilder position(String position) {
if (position != null) {
command.add("-ss");
command.add(position);
}
return this;
}
/**
* 设置帧大小
*
* @param size 形如 xxx*xxx
* @return
*/
public FFmpegCommandBuilder size(String size) {
if (size != null) {
command.add("-s");
command.add(size);
}
return this;
}
/**
* 创建FFmpegCommand命令封装类
*
* @return FFmpegCommand
*/
public FFmpegCommand build() {
return new FFmpegCommand(command);
}
}
public class FFmpegCommandBuilder {
List<String> command = new ArrayList<String>();
public FFmpegCommandBuilder(String exePath) {
if (exePath == null) {
throw new FFmpegCommandRuntimeException("ffmpeg.exe 路径不得为空");
}
//添加命令的exe执行文件位置
command.add(exePath);
}
/**
* 添加输入文件的路径
*
* @param inputFilePath
*/
public FFmpegCommandBuilder input(String inputFilePath) {
if (inputFilePath != null) {
command.add("-i");
command.add(inputFilePath);
}
return this;
}
/**
* 添加输出文件的路径
*
* @param outputFilePath
*/
public FFmpegCommandBuilder output(String outputFilePath) {
if (outputFilePath != null) {
command.add(outputFilePath);
}
return this;
}
/**
* 覆盖输出文件
*/
public FFmpegCommandBuilder override() {
command.add("-y");
return this;
}
/**
* 强制输出格式
*
* @param format 输出格式
*/
public FFmpegCommandBuilder format(FFmpegCommandFormatEnum format) {
if (format != null) {
command.add("-f");
command.add(format.getValue());
}
return this;
}
/**
* 设置录制/转码的时长
*
* @param duration 形如 0.001 表示0.001秒,hh:mm:ss[.xxx]格式的记录时间也支持
*/
public FFmpegCommandBuilder duration(String duration) {
if (duration != null) {
command.add("-t");
command.add(duration);
}
return this;
}
/**
* 搜索到指定的起始时间
*
* @param position 形如 17 表示17秒,[-]hh:mm:ss[.xxx]的格式也支持
*/
public FFmpegCommandBuilder position(String position) {
if (position != null) {
command.add("-ss");
command.add(position);
}
return this;
}
/**
* 设置帧大小
*
* @param size 形如 xxx*xxx
* @return
*/
public FFmpegCommandBuilder size(String size) {
if (size != null) {
command.add("-s");
command.add(size);
}
return this;
}
/**
* 创建FFmpegCommand命令封装类
*
* @return FFmpegCommand
*/
public FFmpegCommand build() {
return new FFmpegCommand(command);
}
}
public class FFmpegBuilderTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJavaBuilder.jpg";
FFmpegCommandBuilder builder = new FFmpegCommandBuilder(ffmpegExePath);
builder.input(inputFilePath).format(FFmpegCommandFormatEnum.IMAGE)
.position("10").duration("0.001").size("320*240").output(outputFilePath);
FFmpegCommand command = builder.build();
try {
String result = command.start(null);
System.out.println(result);
} catch (FFmpegCommandException e) {
e.printStackTrace();
}
}
}
public class FFmpegBuilderTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJavaBuilder.jpg";
FFmpegCommandBuilder builder = new FFmpegCommandBuilder(ffmpegExePath);
builder.input(inputFilePath).format(FFmpegCommandFormatEnum.IMAGE)
.position("10").duration("0.001").size("320*240").output(outputFilePath);
FFmpegCommand command = builder.build();
try {
String result = command.start(null);
System.out.println(result);
} catch (FFmpegCommandException e) {
e.printStackTrace();
}
}
}


Java调用FFmpeg进行视频处理及Builder设计模式的应用的更多相关文章
- java调用ffmpeg获取视频文件信息的一些参数
一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...
- Java调用ffmepg+mencoder视频格式转换(*)
PS: 建议大家在官网下载最新的资源 其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现 ffmepg: D:\ffmpeg\bin\ffmpeg.exe -i E:\1.mp ...
- Java使用FFmpeg处理视频文件指南
Java使用FFmpeg处理视频文件指南 本文主要讲述如何使用Java + FFmpeg实现对视频文件的信息提取.码率压缩.分辨率转换等功能: 之前在网上浏览了一大圈Java使用FFmpeg处理音视频 ...
- Java使用FFmpeg处理视频文件的方法教程
这篇文章主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 前言 本文主要 ...
- Java+Windows+ffmpeg实现视频转换
最近由于项目需要,研究了一下如何用Java实现视频转换,“着实”废了点心思,整理整理,写出给自己备忘下. 思路 由于之前没有没法过相关功能的经验,一开始来真不知道从哪里入手.当然,这个解决,googl ...
- java调用ffmpeg命令行推流遇到的问题
1.Java调用命令行,如果没有额外环境变量,不指定工作路径,Runtime有两个方法 public Process exec(String command) public Process exec( ...
- NET 2.0(C#)调用ffmpeg处理视频的方法
另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...
- .Net调用ffmpeg对视频截图
2019/10/27, .Net c#代码片段 摘要:借助ffmpeg对视频/图片截图.生成缩略图,使用命令行调用ffmpeg工具,支持Linux和Windows 网上很多版本都是需要等待4s的做法, ...
- java调用FFmpeg及mencoder转换视频为FLV并截图
Conver.java package com.ll19.flv; public class Conver { public void run() { try { // 转换并截图 String fi ...
随机推荐
- 基于Aspectj 注解实现 spring AOP
AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...
- The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global- results?,global-exception-mappings?,action*)".
报错 The content of element type "package" must match "(result-types?,interceptors?,def ...
- 谈谈装xp官方纯净系统屡次失败的深刻体会
有木有,小硬盘小内存的电脑竟然装不了五六百m大小的xp却能装win7之类的而感到痛失,如果去装win7电脑果断卡死, 用了最流行的制作u盘启动的软件都不行, 任何直接点击安装也不行,点不了安装的那个选 ...
- mssql 系统函数-字符串函数专题--字符串函数大全
mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...
- js获取地址栏中的数据
window.location.href:设置或获取整个 URL 为字符串window.location.pathname:设置或获取对象指定的文件名或路径window.location.search ...
- 自动化测试基础篇--Selenium判断元素是够存在
摘自https://www.cnblogs.com/sanzangTst/p/8376101.html selenium+python处于学习阶段,功能实现之后开始整理之前写的代码,突然发现一个功能没 ...
- Windows Server 2016-Active Directory复制概念(一)
停更十余天后,从今天开始继续为大家带来Windows Server 2016 Active Directory系列更新,本章为大家介绍有关Active Directory复制相关概念内容,有关Acti ...
- 二、selenium 安装
selenium的安装所需要的环境: 1.浏览器的安装Firefox 2.JDK的安装(Java开发基础类库)eclipse 一个开发源代码的工具 3.selenium sever 下载.网络状况监视 ...
- Java断言(Assertion)
断言(Assertion)是Java中一条语句,包含一个布尔表达式,当该布尔值为真,程序则被认为是正确的:当布尔值为假,则系统会抛出错误. 断言默认禁用的,在开发时候可开启功能,有利于纠正错误,增加可 ...
- 获取WebApplicationContext的几种方式
加载WebApplicationContext的方式 WebApplicationContext是ApplicationContext的子接口,纵观Spring框架的几种容器,BeanFactory作 ...