java调用ffmpeg实现转换视频
作者:zhengdesheng19930211 发布时间:2022-08-25 15:21:04
标签:java,ffmpeg,转换视频
最近由于项目需要把不同格式的视频转换为ts流,故研究了一下ffmpeg。在网上找了很多资料,主要参考了Java+Windows+ffmpeg实现视频转换功能。
期间也加了几个qq群,咨询了各大高手,其中在代码中关于ffmpeg的命令就是来自其中一个qq群里面的大神。
下载相关文件
ffmpeg地址,我下载是windows 64位static版本。
xuggler下载地址
下面的代码我上传到了github,需要的可以下载下来看看。
步骤:
1.研究java如何调用外部程序
2.研究ffmpeg转换视频格式的命令
3.利用xuggle获取ffmpeg解析的ts流的时长、分辨率以及文件大小。
下面直接上代码:
1.ffmpeg转换实现
package vedio.ffmpeg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FfmpegUtil {
public static Boolean ffmpeg(StringffmpegPath, String inputPath, String outputPath) throwsFFmpegException{
if (!checkfile(inputPath)) {
throw newFFmpegException("文件格式不合法");
}
int type =checkContentType(inputPath);
List command = getFfmpegCommand(type,ffmpegPath, inputPath, outputPath);
if (null != command &&command.size() > 0) {
return process(command);
}
return false;
}
private static int checkContentType(StringinputPath) {
String type =inputPath.substring(inputPath.lastIndexOf(".") + 1,inputPath.length()).toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 1;
} else if (type.equals("mpg")){
return 1;
} else if (type.equals("wmv")){
return 1;
} else if (type.equals("3gp")){
return 1;
} else if (type.equals("mov")){
return 1;
} else if (type.equals("mp4")){
return 1;
} else if(type.equals("mkv")){
return 1;
}else if (type.equals("asf")){
return 0;
} else if (type.equals("flv")){
return 0;
}else if (type.equals("rm")){
return 0;
} else if (type.equals("rmvb")){
return 1;
}
return 9;
}
private static boolean checkfile(Stringpath) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
private static boolean process(Listcommand) throws FFmpegException{
try {
if (null == command || command.size() ==0)
return false;
Process videoProcess = newProcessBuilder(command).redirectErrorStream(true).start();
newPrintStream(videoProcess.getErrorStream()).start();
newPrintStream(videoProcess.getInputStream()).start();
int exitcode =videoProcess.waitFor();
if (exitcode == 1) {
return false;
}
return true;
} catch (Exception e) {
throw new FFmpegException("file uploadfailed",e);
}
}
private static List getFfmpegCommand(inttype, String ffmpegPath, String oldfilepath, String outputPath)throws FFmpegException {
List command = newArrayList();
if (type == 1) {
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-c:a");
command.add("mp2");
command.add("-b:a");
command.add("256k");
command.add("-vsync");
command.add("cfr");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
} else if(type==0){
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-vsync");
command.add("cfr");
command.add("-vf");
command.add("idet,yadif=deint=interlaced");
command.add("-filter_complex");
command.add("aresample=async=1000");
command.add("-c:a");
command.add("libmp3lame");
command.add("-b:a");
command.add("192k");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
}else{
throw newFFmpegException("不支持当前上传的文件格式");
}
return command;
}
}
class PrintStream extends Thread{
java.io.InputStream __is =null;
public PrintStream(java.io.InputStream is){
__is = is;
}
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch == -1) {
break;
} else {
System.out.print((char) _ch);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.调用测试类
package vedio.ffmpeg;
public class ConvertVedio {
public static void convertVedio(StringinputPath){
String ffmpegPath =getFfmpegPath();
String outputPath =getOutputPath(inputPath);
try {
FfmpegUtil.ffmpeg(ffmpegPath, inputPath,outputPath);
} catch (FFmpegException e) {
e.printStackTrace();
}
}
private static String getFfmpegPath(){
return "ffmpeg";
}
private static String getOutputPath(StringinputPath) {
return inputPath.substring(0,inputPath.lastIndexOf(".")).toLowerCase() + ".ts";
}
}
3.自定义的异常类
package vedio.ffmpeg;
public class FFmpegException extendsException {
private static final long serialVersionUID= 1L;
public FFmpegException() {
super();
}
public FFmpegException(String message){
super(message);
}
public FFmpegException(Throwable cause){
super(cause);
}
public FFmpegException(String message,Throwable cause) {
super(message, cause);
}
}
4.获取ts流的时长、大小以及分辨率(用到了Xuggle,需要下载对应jar包)
importcom.xuggle.xuggler.ICodec;
importcom.xuggle.xuggler.IContainer;
importcom.xuggle.xuggler.IStream;
importcom.xuggle.xuggler.IStreamCoder;
*/
public static void getVedioInfo(String filename){
// first we create a Xuggler containerobject
IContainer container =IContainer.make();
// we attempt to open up thecontainer
int result = container.open(filename,IContainer.Type.READ, null);
// check if the operation wassuccessful
if (result<0)
return;
// query how many streams the call to openfound
int numStreams =container.getNumStreams();
// query for the total duration
long duration =container.getDuration();
// query for the file size
long fileSize =container.getFileSize();
long secondDuration =duration/1000000;
System.out.println("时长:"+secondDuration+"秒");
System.out.println("文件大小:"+fileSize+"M");
for (int i=0; i
IStreamstream = container.getStream(i);
IStreamCoder coder = stream.getStreamCoder();
if(coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
System.out.println("视频宽度:"+coder.getWidth());
System.out.println("视频高度:"+coder.getHeight());
}
}
}
来源:https://blog.csdn.net/zhengdesheng19930211/article/details/64443620


猜你喜欢
- 运用Java编写代码将一个大文件切割成指定大小的小文件思路:对已知文件进行切割操作 –> 得到多个碎片文件使用:1、 1个字节输入流
- /// <summary> /// interface &
- 目录前言应用定义基本Enum特性Enum的静态导入Enum中添加新方法Switch语句中的EnumEnum的继承EnumSet的使用Enum
- 1、profiles是什么?有什么作用在maven构建的项目都存在一个pom.xml的项目对象模型配置文件,用于约束项目(如:jar包管理、
- 前置知识Kotlin协程不是什么空中阁楼,Kotlin源代码会被编译成class字节码文件,最终会运行到虚拟机中。所以从本质上讲,Kotli
- 最近在通讯录新建联系人=中,一进入一个页面, EditText默认就会自动获取焦点,很是郁闷, 如何让EditText不自动获取焦点?那么如
- 前言在一些日常业务中,会遇到一些琐碎文件需要统一打包到一个压缩包中上传,业务方在后台接收到压缩包后自行解压,然后解析相应文件。而且可能涉及安
- 通过zookeeper实现分布式锁1、创建zookeeper的client首先通过CuratorFrameworkFactory创建一个连接
- 前言Mybatis作为一个应用广泛的优秀的ORM框架,已经成了JavaWeb世界近乎标配的部分,这个框架具有强大的灵活性,在四大组件(Exe
- 本文实例为大家分享了Android登录邮箱自动补全功能的实现方法,供大家参考,具体内容如下效果:实现原理:1、继承重写简单控件AutoCom
- Java中java.io包为我们提供了输入流和输出流,对文件的读写基本上都依赖于这些封装好的关于流的类中来实现。前段时间遇到了以下两种需求:
- Android 处理OnItemClickListener时关于焦点颜色的设置问题  
- 本文实例总结了Java中泛型的用法。分享给大家供大家参考。具体如下:1 基本使用public interface List<E>
- 学会了Paint,Canvas的基本用法之后,我们就可以动手开始实践了,先写个简单的图片加载进度条看看。按照惯例,先看效果图,再决定要不要往
- 一、错误处理原理分析使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状
- C++实现接两个链表实例代码有以ha为头结点的链表,元素个数为m;以hb为头结点的链表,元素个数为n。现在需要你把这两个链表连接
- Java 调用long的最大值和最小值今天对Java八种基本数据类型进行总结,当总结到整数类型中的long时,出现了测试long最大值和最小
- 概述本文基于示例的方式解释控制反转,再看控制反转之前,我们先看下常规控制流程,以数据库访问为例示例并没有实际访问数据,而是基于service
- 本文实例讲述了C#使用NPOI导入Excel的方法。分享给大家供大家参考,具体如下:NPOI是由国人开发的一个进行excel操作的第三方库。
- 本文实例演示了DevExpress实现GridControl单元格编辑验证的方法,比较实用的功能,具体方法如下:主要功能代码如下:/// &