[Java] 如何使用java代码进行视频格式的转换(FLV) →→→→→进入此内容的聊天室

来自 , 2021-04-13, 写在 Java, 查看 161 次.
URL http://www.code666.cn/view/22ac3c5a
  1. package yctc.cp.converter;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.List;
  8. public class ConvertFlv {
  9.     public static void main(String[] args) {
  10.         ConvertFlv.convert("D:\\2.avi", "D:\\3.flv");
  11.     }
  12.      
  13.     public static boolean convert(String inputFile, String outputFile)
  14.     {
  15.         if (!checkfile(inputFile)) {
  16.             System.out.println(inputFile + " is not file");
  17.             return false;
  18.         }
  19.         if (process(inputFile,outputFile)) {
  20.             System.out.println("ok");
  21.             return true;
  22.         }
  23.         return false;
  24.     }
  25.     //检查文件是否存在
  26.     private static boolean checkfile(String path) {
  27.         File file = new File(path);
  28.         if (!file.isFile()) {
  29.             return false;
  30.         }
  31.         return true;
  32.     }
  33.     private static boolean process(String inputFile,String outputFile) {
  34.         int type = checkContentType( inputFile);
  35.         boolean status = false;
  36.         if (type == 0) {
  37.             status = processFLV(inputFile,outputFile);// 直接将文件转为flv文件
  38.         } else if (type == 1) {
  39.             String avifilepath = processAVI(type,inputFile);
  40.             if (avifilepath == null)
  41.                 return false;// avi文件没有得到
  42.             status = processFLV(avifilepath,outputFile);// 将avi转为flv
  43.         }
  44.         return status;
  45.     }
  46.     /**
  47.      * 检查视频类型
  48.      * @param inputFile
  49.      * @return ffmpeg 能解析返回0,不能解析返回1
  50.      */
  51.     private static int checkContentType(String inputFile) {
  52.         String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
  53. inputFile.length())
  54.         .toLowerCase();
  55.         // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
  56.         if (type.equals("avi")) {
  57.             return 0;
  58.         } else if (type.equals("mpg")) {
  59.             return 0;
  60.         } else if (type.equals("wmv")) {
  61.             return 0;
  62.         } else if (type.equals("3gp")) {
  63.             return 0;
  64.         } else if (type.equals("mov")) {
  65.             return 0;
  66.         } else if (type.equals("mp4")) {
  67.             return 0;
  68.         } else if (type.equals("asf")) {
  69.             return 0;
  70.         } else if (type.equals("asx")) {
  71.             return 0;
  72.         } else if (type.equals("flv")) {
  73.             return 0;
  74.         }
  75.         // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  76.         // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  77.         else if (type.equals("wmv9")) {
  78.             return 1;
  79.         } else if (type.equals("rm")) {
  80.             return 1;
  81.         } else if (type.equals("rmvb")) {
  82.             return 1;
  83.         }
  84.         return 9;
  85.     }
  86.     /**
  87.      *  ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
  88.      * @param inputFile
  89.      * @param outputFile
  90.      * @return
  91.      */
  92.     private static boolean processFLV(String inputFile,String outputFile) {
  93.         if (!checkfile(inputFile)) {
  94.             System.out.println(inputFile + " is not file");
  95.             return false;
  96.         }
  97.         List<String> commend = new java.util.ArrayList<String>();
  98.         //低精度
  99.         commend.add("");
  100.         commend.add("-i");
  101.         commend.add(inputFile);
  102.         commend.add("-ab");
  103.         commend.add("128");
  104.         commend.add("-acodec");
  105.         commend.add("libmp3lame");
  106.         commend.add("-ac");
  107.         commend.add("1");
  108.         commend.add("-ar");
  109.         commend.add("22050");
  110.         commend.add("-qscale");
  111.         commend.add("4");
  112.         commend.add("-s");
  113.         commend.add("350x240");
  114.         commend.add("-r");
  115.         commend.add("29.97");
  116.         commend.add("-b");
  117.         commend.add("512");
  118.         commend.add("-y");
  119.         commend.add(outputFile);
  120.         StringBuffer test=new StringBuffer();
  121.         for(int i=0;i<commend.size();i++)
  122.             test.append(commend.get(i)+" ");
  123.         System.out.println(test);
  124.         try {
  125.             ProcessBuilder builder = new ProcessBuilder();
  126.             builder.command(commend);
  127.             builder.start();
  128.             return true;
  129.         } catch (Exception e) {
  130.             e.printStackTrace();
  131.             return false;
  132.         }
  133.     }
  134.     /**
  135.      * Mencoder:
  136.      *  对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  137. 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  138.      * @param type
  139.      * @param inputFile
  140.      * @return
  141.      */
  142.     private static String processAVI(int type,String inputFile) {
  143.         File file =new File("");
  144.         if(file.exists())   file.delete();
  145.         List<String> commend = new java.util.ArrayList<String>();
  146.         commend.add("");
  147.         commend.add(inputFile);
  148.         commend.add("-oac");
  149.         commend.add("mp3lame");
  150.         commend.add("-lameopts");
  151.         commend.add("preset=64");
  152.         commend.add("-ovc");
  153.         commend.add("xvid");
  154.         commend.add("-xvidencopts");
  155.         commend.add("bitrate=600");
  156.         commend.add("-of");
  157.         commend.add("avi");
  158.         commend.add("-o");
  159.         commend.add("");
  160.         StringBuffer test=new StringBuffer();
  161.         for(int i=0;i<commend.size();i++)
  162.             test.append(commend.get(i)+" ");
  163.         System.out.println(test);
  164.         try
  165.         {
  166.             ProcessBuilder builder = new ProcessBuilder();
  167.             builder.command(commend);
  168.             Process p=builder.start();
  169.             /**
  170.              * 清空Mencoder进程 的输出流和错误流
  171.              * 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
  172.              * 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
  173.              */
  174.             final InputStream is1 = p.getInputStream();
  175.             final InputStream is2 = p.getErrorStream();
  176.             new Thread() {
  177.                 public void run() {
  178.                     BufferedReader br = new BufferedReader( new
  179.                     try {
  180.                         String lineB = null;
  181.                         while ((lineB = br.readLine()) != null ){
  182.                             if(lineB != null)System.out.println(lineB);
  183.                         }
  184.                     } catch (IOException e) {
  185.                         e.printStackTrace();
  186.                     }
  187.                 }
  188.             }.start();
  189.             new Thread() {
  190.                 public void run() {
  191.                     BufferedReader br2 = new BufferedReader( new
  192.                     try {
  193.                         String lineC = null;
  194.                         while ( (lineC = br2.readLine()) != null){
  195.                             if(lineC != null)System.out.println(lineC);
  196.                         }
  197.                     } catch (IOException e) {
  198.                         e.printStackTrace();
  199.                     }
  200.                 }
  201.             }.start();
  202.              
  203.             p.waitFor();
  204.              System.out.println("who cares");
  205.             return "";
  206.         }catch (Exception e){
  207.             System.err.println(e);
  208.             return null;
  209.         }
  210.     }
  211. }

回复 "如何使用java代码进行视频格式的转换(FLV)"

这儿你可以回复上面这条便签

captcha