详解java调用ffmpeg转换视频格式为flv
程序员文章站
2023-10-30 19:38:52
详解java调用ffmpeg转换视频格式为flv
注意:下面的程序是在linux下运行的,如果在windows下rmvb转换成avi会出现问题,想成功需要下载下个drv4...
详解java调用ffmpeg转换视频格式为flv
注意:下面的程序是在linux下运行的,如果在windows下rmvb转换成avi会出现问题,想成功需要下载下个drv43260.dll东西放到c:windowssystem32下面
这几天在写一个视频管理系统,遇到一个很大的问题就是如果把不同格式转换为flv,格式!经过网上的一番搜索,自己在总结,整理,整理,终于整出来了!实现了视频进行转换的同时还能够进行视频截图和删除原文件的功能!
格式转换主要原理就是先用java调用ffmpeg的exe文件!
但是有些格式是ffmpeg不能处理的比如rmvb,这样的可以先调用mencoder先把格式转换为avi再进行转换为flv!
我主要写3个类:分别为conver.java
converbegin.java convervideo.java
conver.java 代码如下:
package org.conver; import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; @suppresswarnings("serial") public class conver extends jframe{ public static jtextarea outshowlog; public conver() { settitle("flv转换"); setsize(500, 400); setdefaultcloseoperation(jframe.exit_on_close); outshowlog = new jtextarea(); jscrollpane outpane = newjscrollpane(outshowlog); add(outpane,borderlayout.center); setvisible(true); } public static void main(string args[]) { new conver(); converbegin cb = new converbegin(); cb.start(); } }
converbegin.java 代码如下:
package org.conver; import java.io.file; public class converbegin extends thread{ string[] ff; public void run(){ while (true) { string folderpath ="other/"; //string path = null; file f = newfile(folderpath); if (f.isdirectory()) { ff = f.list(); int i =0; while (i< ff.length) { new convervideo(folderpath+ff[i]).beginconver(); i++; } conver.outshowlog.append("---------------总共转换了"+ff.length+"-----------视频------------"); ff =null; } f = null; try { thread.sleep(10000); } catch (interruptedexceptione) { //如果失败重启线程 this.start(); } } } }
converbegin.java 代码如下
package org.conver; import java.io.file; import java.io.ioexception; import java.io.inputstream; import java.util.date; import java.util.list; public class convervideo { private date dt; private long begintime; private string path; private string filerealname; // 文件名 不包括扩展名 private stringfilename; // 包括扩展名 private string videofolder = "other/"; //别的格式视频的目录 private string flvfolder ="flv/"; //flv视频的目录 privatestringffmpegpath="ffmpeg/ffmpeg.exe"; //ffmpeg.exe的目录 privatestringmencoderpath="ffmpeg/mencoder"; //mencoder的目录 privatestringvideorealpath="flv/"; //截图的视频目录; privatestring imagerealpath ="img/"; //截图的存放目录 //privatestring batrealpath="ffmpeg/ffmpeg.bat"; //bat目录 publicconvervideo(){} public convervideo(string path) { path = path; } publicstring getpath() { return path; } public void setpath(string path) { path = path; } public boolean beginconver(){ file fi = new file(path); filename = fi.getname(); filerealname = filename.substring(0,filename.lastindexof(".")) .tolowercase(); conver.outshowlog.append("----接收到文件("+path+")需要转换--------------------------"); if (!checkfile(path)) { conver.outshowlog.append(path+ "文件不存在"+" "); return false; } dt = new date(); begintime = dt.gettime(); conver.outshowlog.append("----开始转文件("+path+")--------------------------"); if (process()) { date dt2 = new date(); conver.outshowlog.append("转换成功"); long endtime =dt2.gettime(); long timecha = (endtime -begintime); string totaltime =sumtime(timecha); conver.outshowlog.append("共用了:" + totaltime+" "); if(processimg()) { conver.outshowlog.append("截图成功了 "); }else { conver.outshowlog.append("截图不成功了 "); } path = null; return true; }else { path = null; return false; } } public boolean processimg() { // system.out.println(newfilename + "->" +newimg); list commend = new java.util.arraylist(); commend.add(ffmpegpath); commend.add("-i"); commend.add(videorealpath+filerealname+".flv"); commend.add("-y"); commend.add("-f"); commend.add("image2"); commend.add("-ss"); commend.add("38"); commend.add("-t"); commend.add("0.001"); commend.add("-s"); commend.add("320x240"); commend.add(imagerealpath+filerealname+".jpg"); try { processbuilder builder = new processbuilder(); builder.command(commend); builder.start(); return true; } catch (exception e) { e.printstacktrace(); return false; } } private boolean process() { int type = checkcontenttype(); boolean status = false; if (type == 0) { // status =processflv(path);// 直接将文件转为flv文件 status =processflv(path); } else if (type == 1) { string avifilepath =processavi(type); if (avifilepath == null) returnfalse; // avi文件没有得到 else { system.out.println("kaishizhuang"); status =processflv(avifilepath);// 将avi转为flv } } return status; } private int checkcontenttype() { string type =path.substring(path.lastindexof(".") + 1, path.length()) .tolowercase(); //ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) if (type.equals("avi")) { return 0; } else if (type.equals("mpg")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("3gp")) { return 0; } else if (type.equals("mov")) { return 0; } else if (type.equals("mp4")) { return 0; } else if (type.equals("asf")) { return 0; } else if (type.equals("asx")) { return 0; } else if (type.equals("flv")) { return 0; } // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. else if (type.equals("wmv9")) { return 1; } else if (type.equals("rm")) { return 1; } else if (type.equals("rmvb")) { return 1; } return 9; } private boolean checkfile(string path) { file file = new file(path); if (!file.isfile()) { return false; }else { return true; } } // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. private string processavi(int type) { list commend = new java.util.arraylist(); commend.add(mencoderpath); commend.add(path); commend.add("-oac"); commend.add("mp3lame"); commend.add("-lameopts"); commend.add("preset=64"); commend.add("-ovc"); commend.add("xvid"); commend.add("-xvidencopts"); commend.add("bitrate=600"); commend.add("-of"); commend.add("avi"); commend.add("-o"); commend.add(videofolder + filerealname +".avi"); // 命令类型:mencoder 1.rmvb -oac mp3lame -lameoptspreset=64 -ovc xvid // -xvidencopts bitrate=600 -of avi -ormvb.avi try { processbuilder builder = newprocessbuilder(); builder.command(commend); process p =builder.start(); dowaitfor(p); return videofolder +filerealname + ".avi"; } catch (exception e) { e.printstacktrace(); return null; } } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private boolean processflv(string oldfilepath) { if (!checkfile(path)) { system.out.println(oldfilepath+ " is not file"); return false; } list commend = new java.util.arraylist(); commend.add(ffmpegpath); commend.add("-i"); commend.add(oldfilepath); commend.add("-ab"); commend.add("64"); commend.add("-acodec"); commend.add("mp3"); commend.add("-ac"); commend.add("2"); commend.add("-ar"); commend.add("22050"); commend.add("-b"); commend.add("230"); commend.add("-r"); commend.add("24"); commend.add("-y"); commend.add(flvfolder + filerealname +".flv"); try { processbuilder builder = newprocessbuilder(); string cmd =commend.tostring(); builder.command(commend); //builder.redirecterrorstream(true); process p =builder.start(); dowaitfor(p); p.destroy(); deletefile(oldfilepath); return true; } catch (exception e) { e.printstacktrace(); return false; } } public int dowaitfor(process p) { inputstream in = null; inputstream err=null; int exitvalue = -1; // returned to caller when pis finished try { system.out.println("comeing"); in = p.getinputstream(); err =p.geterrorstream(); boolean finished = false; //set to true when p is finished while (!finished) { try { while (in.available() > 0) { // print the output of our system call character c = new character((char) in.read()); system.out.print(c); } while (err.available() > 0) { // print the output of our system call character c = new character((char) err.read()); system.out.print(c); } // ask the process for its exitvalue. if the process // is not finished, an illegalthreadstateexception // is thrown. if it is finished, we fall through and // the variable finished is set to true. exitvalue = p.exitvalue(); finished = true; } catch(illegalthreadstateexception e) { // process is not finished yet; // sleep a little to save on cpu cycles thread.currentthread().sleep(500); } } } catch (exception e) { // unexpected exception! printit out for debugging... system.err.println("dowaitfor();: unexpected exception - " + e.getmessage()); } finally { try { if(in!=null) { in.close(); } } catch (ioexception e){ system.out.println(e.getmessage()); } if(err!=null) { try { err.close(); } catch(ioexception e) { system.out.println(e.getmessage()); } } } // return completion status to caller return exitvalue; } public void deletefile(string filepath) { file file = new file(filepath); if (path.equals(filepath)) { if (file.delete()) { system.out.println("文件" + filepath + "已删除"); } } else { if (file.delete()) { system.out.println("文件" + filepath + "已删除 "); } file filedelete2 = newfile(path); if (filedelete2.delete()){ system.out.println("文件" + path + "已删除"); } } } public string sumtime(long ms) { int ss = 1000; long mi = ss * 60; long hh = mi * 60; long dd = hh * 24; long day = ms / dd; long hour = (ms - day * dd) / hh; long minute = (ms - day * dd - hour * hh) /mi; long second = (ms - day * dd - hour * hh -minute * mi) / ss; long millisecond = ms - day * dd - hour * hh -minute * mi - second * ss; string strday = day < 10 ? "0" +day + "天" : "" + day + "天"; string strhour = hour < 10 ? "0"+ hour + "小时" : "" + hour + "小时"; string strminute = minute < 10 ?"0" + minute + "分" : "" + minute + "分"; string strsecond = second < 10 ?"0" + second + "秒" : "" + second + "秒"; string strmillisecond = millisecond< 10 ? "0" + millisecond : "" +millisecond; strmillisecond = millisecond <100 ? "0" + strmillisecond + "毫秒" : "" +strmillisecond + " 毫秒"; return strday + " " + strhour + ":" + strminute+ ":" + strsecond + " " +strmillisecond; } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!