介绍
如何在java中使用ffmpeg对视频进行转换?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
java可以用来干什么
java主要应用于:1。网页开发;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 并不知道; , 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(类型、ffmpegPath, inputPath,, outputPath); if (null !=, command ,, command.size(),祝辞,0),{ return 过程(命令); , } return 假; } , private static  int checkContentType (StringinputPath), { String type =inputPath.substring (inputPath.lastIndexOf (“!”), +, - 1, inputPath.length ()) .toLowerCase ();//ffmpeg能解析的格式:(asx asf,英里/加仑,对wmv, 3 gp、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 (“3 gp")) { return 1; },else if (type.equals (“mov")) { return 1; },else if (type.equals (“mp4")) { return 1; },else 如果(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 文件(路径); if (! file.isFile ()), { return 假; } return 真实; } , private static  boolean 过程(Listcommand), throws FFmpegException { , try { , if (null ==, command | |, command.size (),==0) return 假; Process videoProcess =, newProcessBuilder(命令).redirectErrorStream(真正的).start (); , newPrintStream (videoProcess.getErrorStream ()) .start (); , newPrintStream (videoProcess.getInputStream ()) .start (); , int exitcode =videoProcess.waitFor (); , if (exitcode ==, 1), { return 假; } return 真实; },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(“我); 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 (“256 k"); command.add (“-vsync"); command.add (“cfr"); command.add (“-f"); command.add (“mpegts"); command.add (outputPath); },else 如果(type==0) { command.add (ffmpegPath +“\ \ ffmpeg"); command.add(“我); 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=异步=1000“); command.add (“c: a"); command.add (“libmp3lame"); command.add (“- b: a"); command.add (“192 k"); command.add (“-pix_fmt"); command.add (“yuv420p"); command.add (“-f"); command.add (“mpegts"); command.add (outputPath); 其他}{ throw newFFmpegException(“不支持当前上传的文件格式“); } return 命令; } } , class PrintStream  extends 螺纹{ java.io.InputStream __is =零; , public PrintStream (java.io.InputStream ) { 时间=__is ; } , public void  run (), { try { while (却;能够!=,null), { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在java中使用ffmpeg对视频进行转换