zoukankan      html  css  js  c++  java
  • 如何使用java代码进行视频格式的转换(FLV)

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

    一,前言

        在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放。

        公司一直在使用中,配合使用,感觉不错,每天转换的文件超过上千个。

    二,准备

        工具包  : drv43260.dll,ffmpeg.exe,mencoder.exe,pncrt.dll,pthreadGC2.dll

        原理其实是使用java代码在CMD下调用MediaCode,完成视频格式的转换。

        完整项目下载地址: ConverVedio

        播放器我用的是ckplayer

    三,部分代码

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
     
     
    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. InputStreamReader(is1));  
    180.                     try {  
    181.                         String lineB = null;  
    182.                         while ((lineB = br.readLine()) != null ){  
    183.                             if(lineB != null)System.out.println(lineB);  
    184.                         }  
    185.                     } catch (IOException e) {  
    186.                         e.printStackTrace();  
    187.                     }  
    188.                 }  
    189.             }.start();  
    190.             new Thread() {  
    191.                 public void run() {  
    192.                     BufferedReader br2 = new BufferedReader( new  
    193. InputStreamReader(is2));  
    194.                     try {  
    195.                         String lineC = null;  
    196.                         while ( (lineC = br2.readLine()) != null){  
    197.                             if(lineC != null)System.out.println(lineC);  
    198.                         }  
    199.                     } catch (IOException e) {  
    200.                         e.printStackTrace();  
    201.                     }  
    202.                 }  
    203.             }.start();  
    204.                
    205.             p.waitFor();  
    206.              System.out.println("who cares");  
    207.             return "";  
    208.         }catch (Exception e){  
    209.             System.err.println(e);  
    210.             return null;  
    211.         }  
    212.     }  
    213. }  
  • 相关阅读:
    javascript获取当前日期、年份和月份等
    程序员也可以懂一点期望值管理
    数据类型,隐式转换以及json,对象,引用类型,预解析 视频教程
    两个值交互位置的几种方法
    通过Class获取标签,兼容的几种思路
    前端开发流程
    元素多层嵌套,JS获取问题
    原生JS实现分页效果2.0(新增了上一页和下一页,添加当前元素样式)
    原生JS实现分页效果1.0
    学习方法,以及时间的安排。
  • 原文地址:https://www.cnblogs.com/Firesun/p/9998940.html
Copyright © 2011-2022 走看看