如何在java中使用ffmpeg对视频进行转换

发布时间:2021-02-22 15:56:58 作者:Leah
来源:亿速云 阅读:368

如何在java中使用ffmpeg对视频进行转换?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Java可以用来干什么

Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。

步骤:

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());
    }
   }
 
 }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 如何在Python中对字节单位进行转换
  2. 怎么在python中使用ffmpeg对视频文件进行批量转换

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ffmpeg

上一篇:html强制清除缓存的方法

下一篇:使用java怎么将pdf按页转换为图片

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》