当前位置: 移动技术网 > IT编程>开发语言>Java > Java视频格式转化的实现方法

Java视频格式转化的实现方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

替情敌养儿子,李继耐的儿子,谢晋元后代

本文实例为大家分享了java视频格式转化的具体代码,供大家参考,具体内容如下

核心是利用ffmpeg进行视频转换,我们自己并不写转换视频的代码,只是调用ffmpeg,它会帮我们完成视频的转换。ffmpeg支持的类型有:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等,这些类型,可以利用ffmpeg进行直接转换。ffmpeg不支持的类型有:wmv9,rm,rmvb等,这些类型需要先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式。

废话不大多说了,首先要把相关的库和要转化的视频准备好,如下图

 

下面就是代码部分了   

package com.sino.test; 
 
import java.io.bufferedreader; 
import java.io.file; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
import java.util.arraylist; 
import java.util.list; 
 
/** 
 * java实现视频格式的转化 
 * @author liuyazhuang 
 * 
 */ 
public class changevideo { 
  public static void main(string[] args) { 
    changevideo.convert("d:\\myeclipse\\aa.avi", "d:\\myeclipse\\bb.mp4"); 
  } 
 
  /** 
   * @param inputfile:需要转换的视频 
   * @param outputfile:转换后的视频w 
   * @return 
   */ 
  public static boolean convert(string inputfile, string outputfile) { 
    if (!checkfile(inputfile)) { 
      system.out.println(inputfile + " is nokt file"); 
      return false; 
    } 
    if (process(inputfile, outputfile)) { 
      system.out.println("ok"); 
      return true; 
    } 
    return false; 
  } 
 
  // 检查文件是否存在 
  private static boolean checkfile(string path) { 
    file file = new file(path); 
    if (!file.isfile()) { 
      return false; 
    } 
    return true; 
  } 
 
  /** 
   * @param inputfile 
   * @param outputfile 
   * @return 
   * 转换视频文件 
   */ 
  private static boolean process(string inputfile, string outputfile) { 
    int type = checkcontenttype(inputfile); 
    boolean status = false; 
    if (type == 0) { 
      status = processflv(inputfile, outputfile);// 直接将文件转为flv文件 
    } else if (type == 1) { 
      string avifilepath = processavi(type, inputfile); 
      if (avifilepath == null) 
        return false;// avi文件没有得到 
      status = processflv(avifilepath, outputfile);// 将avi转为flv 
    } 
    return status; 
  } 
 
  private static int checkcontenttype(string inputfile) { 
    string type = inputfile.substring(inputfile.lastindexof(".") + 1, 
        inputfile.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; 
  } 
  // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)直接转换为目标视频 
  private static boolean processflv(string inputfile, string outputfile) { 
    if (!checkfile(inputfile)) { 
      system.out.println(inputfile + " is not file"); 
      return false; 
    } 
    list<string> commend = new arraylist<string>(); 
     
    commend.add(constants.ffmpegpath); 
    commend.add("-i"); 
    commend.add(inputfile); 
    commend.add("-ab"); 
    commend.add("128"); 
    commend.add("-acodec"); 
    commend.add("libmp3lame"); 
    commend.add("-ac"); 
    commend.add("1"); 
    commend.add("-ar"); 
    commend.add("22050"); 
    commend.add("-r"); 
    commend.add("29.97"); 
    //高品质  
    commend.add("-qscale"); 
    commend.add("6"); 
    //低品质 
//   commend.add("-b"); 
//   commend.add("512"); 
    commend.add("-y"); 
     
    commend.add(outputfile); 
    stringbuffer test = new stringbuffer(); 
    for (int i = 0; i < commend.size(); i++) { 
      test.append(commend.get(i) + " "); 
    } 
 
    system.out.println(test); 
 
    try { 
      processbuilder builder = new processbuilder(); 
      builder.command(commend); 
      builder.start(); 
      return true; 
    } catch (exception e) { 
      e.printstacktrace(); 
      return false; 
    } 
  } 
  // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 
  // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. 
  private static string processavi(int type, string inputfile) { 
    file file = new file(constants.avifilepath); 
    if (file.exists()) 
      file.delete(); 
    list<string> commend = new arraylist<string>(); 
    commend.add(constants.mencoderpath); 
    commend.add(inputfile); 
    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(constants.avifilepath); 
    stringbuffer test = new stringbuffer(); 
    for (int i = 0; i < commend.size(); i++) { 
      test.append(commend.get(i) + " "); 
    } 
 
    system.out.println(test); 
    try { 
      processbuilder builder = new processbuilder(); 
      builder.command(commend); 
      process p = builder.start(); 
 
      final inputstream is1 = p.getinputstream(); 
      final inputstream is2 = p.geterrorstream(); 
      new thread() { 
        public void run() { 
          bufferedreader br = new bufferedreader( 
              new inputstreamreader(is1)); 
          try { 
            string lineb = null; 
            while ((lineb = br.readline()) != null) { 
              if (lineb != null) 
                system.out.println(lineb); 
            } 
          } catch (ioexception e) { 
            e.printstacktrace(); 
          } 
        } 
      }.start(); 
      new thread() { 
        public void run() { 
          bufferedreader br2 = new bufferedreader( 
              new inputstreamreader(is2)); 
          try { 
            string linec = null; 
            while ((linec = br2.readline()) != null) { 
              if (linec != null) 
                system.out.println(linec); 
            } 
          } catch (ioexception e) { 
            e.printstacktrace(); 
          } 
        } 
      }.start(); 
 
      // 等mencoder进程转换结束,再调用ffmepg进程 
      p.waitfor(); 
      system.out.println("who cares"); 
      return constants.avifilepath; 
    } catch (exception e) { 
      system.err.println(e); 
      return null; 
    } 
  } 
} 

 类changevideo主要进行视频格式的转化

package com.sino.test; 
 
/** 
 * 常量类,主要设置可执行程序和动态链接库以及转化过程中生成的临时视频文件的位置 
 * @author liuyazhuang 
 * 
 */ 
public class constants { 
  //ffmpeg存放的路径 
  public static final string ffmpegpath = "d:\\myeclipse\\ffmpeg.exe"; 
  //mencoder存放的路径 
  public static final string mencoderpath = "d:\\myeclipse\\mencoder.exe"; 
  //通过mencoder转换成的avi存放路径 
  public static final string avifilepath = "d:\\myeclipse\\temp.avi"; 
} 

常量类constants ,主要设置可执行程序和动态链接库以及转化过程中生成的临时视频文件的位置。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网