天天看點

線程池多線程視訊轉碼(完整版)

Threadpool 代碼 

線程池多線程視訊轉碼(完整版)

package yong.method;  

import java.util.concurrent.ExecutorService;   

import java.util.concurrent.Executors;   

/**  

 *   

 * @author 趙永恩  

 *  

 */  

public class ThreadPool {   

  public static ExecutorService exec = Executors.newFixedThreadPool(3);   

  public static synchronized void trans(String filePath, String outPath,String outImgPath,String pathAddress){   

    ThreadTransCode trans=new ThreadTransCode(filePath,outPath,outImgPath,pathAddress);   

    exec.execute(trans);   

  }   

}    

Threadtranscode代碼 

線程池多線程視訊轉碼(完整版)

public class ThreadTransCode implements Runnable {   

  //原始檔案   

  private String filePath;   

  //目标檔案   

  private String outPath;   

  private String outImgPath;   

  //工具位址  

  private String pathAddress;   

  public ThreadTransCode(String filePath, String outPath,String outImgPath,String pathAddress) {  

        this.filePath = filePath;  

        this.outPath = outPath;  

        this.outImgPath = outImgPath;  

        this.pathAddress = pathAddress;  

    }  

  public void run() {   

    synchronized (this) {   

      //System.out.println("轉碼開始..............");   

      ConvertVideo cv = new ConvertVideo(filePath, outPath,outImgPath,pathAddress);   

      cv.process();   

    }   

}   

Convertvideo 代碼 

線程池多線程視訊轉碼(完整版)

import java.io.*;  

import java.util.List;  

public class ConvertVideo {  

    // 原始檔案  

    private String filePath;  

    // 目标檔案  

    private String outPath;  

    private String outImgPath;  

    private String pathAddress;   

    /**  

     * 工具[tool] 位址[address]  

     */  

    private String mencoderAdd;  

    private String ffmpegAdd;  

    public ConvertVideo(String filePath, String outPath, String outImgPath,String pathAddress) {  

        this.mencoderAdd=pathAddress+"tool\\ffmpeg\\mencoder.exe";  

        this.ffmpegAdd=pathAddress+"tool\\ffmpeg\\ffmpeg.exe";  

        //System.out.println(ffmpegAdd);  

    public synchronized void process() {  

        int type = checkContentType();  

        if (type == 0) {  

            this.ffmpegTransVideo();  

            this.ffmpegTransImage();  

        } else if (type == 1) {  

            this.mencoderTransVideo();  

        }  

    public synchronized int checkContentType() {  

        String type = filePath.substring(filePath.lastIndexOf(".") + 1,  

                filePath.length()).toLowerCase();  

        // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  

        if (type.equals("avi")) {  

            return 0;  

        } else if (type.equals("mpg")) {  

        } else if (type.equals("wmv")) {  

        } else if (type.equals("3gp")) {  

        } else if (type.equals("mov")) {  

        } else if (type.equals("mp4")) {  

        } else if (type.equals("asf")) {  

        } else if (type.equals("asx")) {  

        } else if (type.equals("flv")) {  

        // 對ffmpeg無法解析的檔案格式(wmv9,rm,rmvb等),  

        // 可以先用别的工具(mencoder)轉換為avi(ffmpeg能解析的)格式.  

        else if (type.equals("wmv9")) {  

            return 1;  

        } else if (type.equals("rm")) {  

        } else if (type.equals("rmvb")) {  

        return 9;  

    public synchronized static boolean checkfile(String path) {  

        File file = new File(path);  

        if (!file.isFile()) {  

            return false;  

        return true;  

     * 使用mencoder轉碼  

     *   

     * @param videoPath  

     *            源路徑 -- 要轉換的視訊檔案  

     * @param targetPath  

     *            目标路徑 -- 轉換後的視訊flv  

     * @return 傳回目标路徑  

    public synchronized String mencoderTransVideo() {  

        List<String> commend = new java.util.ArrayList<String>();  

        // commend.add("d:\\flv\\MediaCoder\\codecs\\mencoder.exe");  

        //commend.add("D:\\Tomcat-6.0.29\\webapps\\zyk\\tool\\ffmpeg\\mencoder.exe");  

        commend.add(mencoderAdd);  

        commend.add(filePath);  

        // 音頻采用mp3編碼  

        commend.add("-oac");  

        commend.add("mp3lame");  

        // 采用高質DivX視訊編碼,視訊碼率為112kbps  

        commend.add("-ovc");  

        commend.add("lavc");  

        commend.add("-lavcopts");  

        commend  

                .add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");  

        commend.add("-lameopts");  

        commend.add("abr:br=56");  

        // 聲音采樣頻率設定,現為22K  

        commend.add("-srate");  

        commend.add("22050");  

        // -sws就是用來設定品質的,預設值為2  

        commend.add("-sws");  

        commend.add("3");  

        // 寬度為208,高度自動調整保持比例;  

        // -vf scale=-3:176寬度自動調整保持比例,高度為176;如果想保持原來的大小可以不要這個參數  

        commend.add("-vf");  

        commend.add("scale=512:-3");  

        // 幀速率設定  

        commend.add("-ofps");  

        commend.add("18");  

        /*  

         * mode=3:cbr:br=24單聲道 音頻碼率為24kbps;-lameopts  

         * mode=0:cbr:br=24立體聲,音頻碼率為24kbps; 還可設定音量,-lameopts  

         * mode=3:cbr:br=32:vol=1,設定範置為1~10,但不宜設得太高  

         */  

        commend.add("vbr=3:br=128");  

        commend.add("-o");  

        commend.add(outPath);  

        // 控制台顯示執行的指令  

        System.out.println(commend);  

        try {  

            ProcessBuilder builder = new ProcessBuilder();  

            builder.command(commend);  

            builder.start();  

            return outPath;  

        } catch (Exception e) {  

            e.printStackTrace();  

            return null;  

     * 使用ffmpeg轉碼  

    public synchronized  String ffmpegTransVideo() {   

        // if (!checkfile(videoPath)) {   

        // System.out.println(videoPath + " is not file aaa");   

        // return false;   

        // }   

        List<String> commend = new java.util.ArrayList<String>();   

        //commend.add("d:\\flv\\MediaCoder\\codecs\\ffmpeg.exe");   

        //commend.add("D:\\Tomcat-6.0.29\\webapps\\zyk\\tool\\ffmpeg\\ffmpeg.exe");   

        commend.add(ffmpegAdd);   

        commend.add("-i");   

        commend.add(filePath);   

        commend.add("-ab");   

        commend.add("64");   

        // commend.add(" -acodec ");   

        // commend.add("codec");   

        commend.add("-ac");   

        commend.add("2");   

        commend.add("-ar");   

        commend.add("22050");   

        // 清晰度 -qscale 4 為最好可是檔案大, -qscale 6就可以了   

        commend.add("-qscale");   

        commend.add("6");   

        // commend.add("-b");   

        // commend.add("768");   

        // commend.add("230");   

        // commend.add("-s");   

        // commend.add("352x240");   

        // commend.add("-r");   

        // commend.add("29.97");   

        commend.add("-y");   

        commend.add(outPath);   

        //System.out.println(commend);   

        try {   

          ProcessBuilder builder = new ProcessBuilder();   

          builder.command(commend);   

          Process process = builder.start();   

          InputStream is = process.getErrorStream();   

          InputStreamReader inputStreamReader = new InputStreamReader(is);   

          BufferedReader inputBufferedReader = new BufferedReader(   

              inputStreamReader);   

          String line = null;   

          StringBuilder stringBuilder = new StringBuilder();   

          while ((line = inputBufferedReader.readLine()) != null) {   

            stringBuilder.append(line);   

          }   

          inputBufferedReader.close();   

          inputBufferedReader = null;   

          inputStreamReader.close();   

          inputStreamReader = null;   

          is.close();   

          is = null;   

          return outPath;   

        } catch (Exception e) {   

          e.printStackTrace();   

          return null;   

        }   

      }  

    // 生成圖檔 參數String newfilename, String newimg  

    public synchronized boolean ffmpegTransImage() {  

        // commend.add("d:\\flv\\MediaCoder\\codecs\\ffmpeg.exe");  

        commend.add(ffmpegAdd);  

        commend.add("-i");  

        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(outImgPath);  

            return true;  

}  

測試類代碼 

線程池多線程視訊轉碼(完整版)

public class A {  

     * @param args  

    public static void main(String[] args) {  

        // TODO Auto-generated method stub  

        String filePath = "D:\\test\\a.MP4";  

        String outPath = "d:\\test\\2011q.flv";  

        String outImgPath = "d:\\test\\20111.jpg";  

        String pathAddress="D:\\Tomcat-6.0.29\\webapps\\zyk\\";  

        ThreadPool.trans(filePath,outPath,outImgPath,pathAddress);