Java 实现视频转FLV,支持完成进度百分比

jopen 10年前

视频转换主要采用ffmpeg,下载去官网即可

转换主程序:

注:以下标粗的 ffmpeg 是 绝对路径如:D:/conver/ffmpeg-win.exe

/**        *         * @Title: processFLV         * @Description: 转FLV格式        *  ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)        * @param           * @return boolean         * @throws        */        private static boolean coverToFLV(String srcVideoPath,String tarVideoPath) {          if (!checkfile(srcVideoPath)) {               logger.error("【" + srcVideoPath + "】  不存在 !");             return false;            }           List<String> commend = new java.util.ArrayList<String>();          commend.add(</span><strong><span style="font-size:18px;">ffmpegPath</span></strong><span style="font-size:14px;">);          commend.add( "-y");            commend.add( "-i");           commend.add(srcVideoPath);          commend.add("-ab");          commend.add("64");          //commend.add("-sameq");          commend.add("-acodec");          commend.add("mp3");    //      commend.add("-vcodec");    //      commend.add("xvid");          commend.add("-ac");          commend.add("2");          commend.add("-ar");          commend.add("22050");          commend.add("-qscale");          commend.add("6"); //压缩大小    //      commend.add("-b");    //      commend.add("1500");          commend.add("-r");          commend.add("24");          commend.add(tarVideoPath);          try {               ProcessBuilder builder = new ProcessBuilder();               //String cmd = commend.toString();               builder.command(commend);               Process p = builder.start();               doWaitPro(p);               //doWaitFor(p);               p.destroy();               return true;          } catch (Exception e) {              e.printStackTrace();              return false;          }        }
等待线程完成:
    //等待线程处理完成            public static void doWaitPro(Process p){                try {                    String errorMsg = readInputStream(p.getErrorStream(), "error");                    String outputMsg = readInputStream(p.getInputStream(), "out");                        int c = p.waitFor();                         if (c != 0) {// 如果处理进程在等待                             System.out.println("处理失败:" + errorMsg);                         } else {                             System.out.println(COMPLETE + outputMsg);                         }                  } catch (IOException e) {                    // tanghui Auto-generated catch block                    e.printStackTrace();                } catch (InterruptedException e) {                    // tanghui Auto-generated catch block                    e.printStackTrace();                }                                 }  
完成进度百分比:
    /**              *               * @Title: readInputStream               * @Description: 完成进度百分比              * @param                 * @return String               * @throws              */               private static String readInputStream(InputStream is, String f) throws IOException {                      // 将进程的输出流封装成缓冲读者对象                      BufferedReader br = new BufferedReader(new InputStreamReader(is));                      StringBuffer lines = new StringBuffer();// 构造一个可变字符串                      long totalTime = 0;                                          // 对缓冲读者对象进行每行循环                      for (String line = br.readLine(); line != null; line = br.readLine()) {                       lines.append(line);// 将每行信息字符串添加到可变字符串中                       int positionDuration = line.indexOf("Duration:");// 在当前行中找到第一个"Duration:"的位置                       int positionTime = line.indexOf("time=");                       if (positionDuration > 0) {// 如果当前行中有"Duration:"                        String dur = line.replace("Duration:", "");// 将当前行中"Duration:"替换为""                        dur = dur.trim().substring(0, 8);// 将替换后的字符串去掉首尾空格后截取前8个字符                        int h = Integer.parseInt(dur.substring(0, 2));// 封装成小时                        int m = Integer.parseInt(dur.substring(3, 5));// 封装成分钟                        int s = Integer.parseInt(dur.substring(6, 8));// 封装成秒                        totalTime = h * 3600 + m * 60 + s;// 得到总共的时间秒数                       }                       if (positionTime > 0) {// 如果所用时间字符串存在                        // 截取包含time=的当前所用时间字符串                        String time = line.substring(positionTime, line                          .indexOf("bitrate") - 1);                        time = time.substring(time.indexOf("=") + 1, time.indexOf("."));// 截取当前所用时间字符串                        int h = Integer.parseInt(time.substring(0, 2));// 封装成小时                        int m = Integer.parseInt(time.substring(3, 5));// 封装成分钟                        int s = Integer.parseInt(time.substring(6, 8));// 封装成秒                        long hasTime = h * 3600 + m * 60 + s;// 得到总共的时间秒数                       float t = (float) hasTime / (float) totalTime;// 计算所用时间与总共需要时间的比例                        COMPLETE = (int) Math.ceil(t * 100);// 计算完成进度百分比                       }                          System.out.println("完成:" + COMPLETE + "%");                      }                      br.close();// 关闭进程的输出流                      return lines.toString();                 }    
来自:http://blog.csdn.net/tanghui2qinghong/article/details/24304859