DDR爱好者之家 Design By 杰米

详解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;

}
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。