• 线程池多线程视频转码(完整版)


    Threadpool 代码 

     收藏代码

    1. package yong.method;  
    2.   
    3. import java.util.concurrent.ExecutorService;   
    4. import java.util.concurrent.Executors;   
    5.   
    6. /**  
    7.  *   
    8.  * @author 赵永恩  
    9.  *  
    10.  */  
    11. public class ThreadPool {   
    12.       
    13.   public static ExecutorService exec = Executors.newFixedThreadPool(3);   
    14.   public static synchronized void trans(String filePath, String outPath,String outImgPath,String pathAddress){   
    15.     ThreadTransCode trans=new ThreadTransCode(filePath,outPath,outImgPath,pathAddress);   
    16.     exec.execute(trans);   
    17.   }   
    18. }    


     

    Threadtranscode代码 

     收藏代码

    1. package yong.method;  
    2.   
    3. /**  
    4.  *   
    5.  * @author 赵永恩  
    6.  *  
    7.  */  
    8. public class ThreadTransCode implements Runnable {   
    9.   
    10.   //原始文件   
    11.   private String filePath;   
    12.   //目标文件   
    13.   private String outPath;   
    14.   private String outImgPath;   
    15.     
    16.   //工具地址  
    17.   private String pathAddress;   
    18.   
    19.   
    20.   public ThreadTransCode(String filePath, String outPath,String outImgPath,String pathAddress) {  
    21.         this.filePath = filePath;  
    22.         this.outPath = outPath;  
    23.         this.outImgPath = outImgPath;  
    24.         this.pathAddress = pathAddress;  
    25.     }  
    26.   
    27.   
    28.   public void run() {   
    29.     synchronized (this) {   
    30.       //System.out.println("转码开始..............");   
    31.       ConvertVideo cv = new ConvertVideo(filePath, outPath,outImgPath,pathAddress);   
    32.       cv.process();   
    33.     }   
    34.   }   
    35. }   


     

    Convertvideo 代码 

     收藏代码

    1. package yong.method;  
    2.   
    3. import java.io.*;  
    4. import java.util.List;  
    5.   
    6.   
    7. /**  
    8.  *   
    9.  * @author 赵永恩  
    10.  *  
    11.  */  
    12. public class ConvertVideo {  
    13.       
    14.       
    15.       
    16.     // 原始文件  
    17.     private String filePath;  
    18.     // 目标文件  
    19.     private String outPath;  
    20.     private String outImgPath;  
    21.       
    22.     private String pathAddress;   
    23.       
    24.     /**  
    25.      * 工具[tool] 地址[address]  
    26.      */  
    27.     private String mencoderAdd;  
    28.     private String ffmpegAdd;  
    29.       
    30.       
    31.     public ConvertVideo(String filePath, String outPath, String outImgPath,String pathAddress) {  
    32.         this.filePath = filePath;  
    33.         this.outPath = outPath;  
    34.         this.outImgPath = outImgPath;  
    35.         this.pathAddress = pathAddress;  
    36.           
    37.         this.mencoderAdd=pathAddress+"tool\ffmpeg\mencoder.exe";  
    38.         this.ffmpegAdd=pathAddress+"tool\ffmpeg\ffmpeg.exe";  
    39.           
    40.         //System.out.println(ffmpegAdd);  
    41.     }  
    42.   
    43.     public synchronized void process() {  
    44.         int type = checkContentType();  
    45.         if (type == 0) {  
    46.             this.ffmpegTransVideo();  
    47.             this.ffmpegTransImage();  
    48.         } else if (type == 1) {  
    49.             this.mencoderTransVideo();  
    50.         }  
    51.     }  
    52.   
    53.     public synchronized int checkContentType() {  
    54.         String type = filePath.substring(filePath.lastIndexOf(".") + 1,  
    55.                 filePath.length()).toLowerCase();  
    56.         // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
    57.         if (type.equals("avi")) {  
    58.             return 0;  
    59.         } else if (type.equals("mpg")) {  
    60.             return 0;  
    61.         } else if (type.equals("wmv")) {  
    62.             return 0;  
    63.         } else if (type.equals("3gp")) {  
    64.             return 0;  
    65.         } else if (type.equals("mov")) {  
    66.             return 0;  
    67.         } else if (type.equals("mp4")) {  
    68.             return 0;  
    69.         } else if (type.equals("asf")) {  
    70.             return 0;  
    71.         } else if (type.equals("asx")) {  
    72.             return 0;  
    73.         } else if (type.equals("flv")) {  
    74.             return 0;  
    75.         }  
    76.         // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),  
    77.         // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.  
    78.         else if (type.equals("wmv9")) {  
    79.             return 1;  
    80.         } else if (type.equals("rm")) {  
    81.             return 1;  
    82.         } else if (type.equals("rmvb")) {  
    83.             return 1;  
    84.         }  
    85.         return 9;  
    86.     }  
    87.   
    88.     public synchronized static boolean checkfile(String path) {  
    89.         File file = new File(path);  
    90.         if (!file.isFile()) {  
    91.             return false;  
    92.         }  
    93.         return true;  
    94.     }  
    95.   
    96.     /**  
    97.      * 使用mencoder转码  
    98.      *   
    99.      * @param videoPath  
    100.      *            源路径 -- 要转换的视频文件  
    101.      * @param targetPath  
    102.      *            目标路径 -- 转换后的视频flv  
    103.      * @return 返回目标路径  
    104.      */  
    105.     public synchronized String mencoderTransVideo() {  
    106.         List<String> commend = new java.util.ArrayList<String>();  
    107.         // commend.add("d:\flv\MediaCoder\codecs\mencoder.exe");  
    108.         //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\mencoder.exe");  
    109.         commend.add(mencoderAdd);  
    110.         commend.add(filePath);  
    111.         // 音频采用mp3编码  
    112.         commend.add("-oac");  
    113.         commend.add("mp3lame");  
    114.         // 采用高质DivX视频编码,视频码率为112kbps  
    115.         commend.add("-ovc");  
    116.         commend.add("lavc");  
    117.         commend.add("-lavcopts");  
    118.         commend  
    119.                 .add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");  
    120.         commend.add("-lameopts");  
    121.         commend.add("abr:br=56");  
    122.         // 声音采样频率设置,现为22K  
    123.         commend.add("-srate");  
    124.         commend.add("22050");  
    125.         // -sws就是用来设置品质的,默认值为2  
    126.         commend.add("-sws");  
    127.         commend.add("3");  
    128.         // 宽度为208,高度自动调整保持比例;  
    129.         // -vf scale=-3:176宽度自动调整保持比例,高度为176;如果想保持原来的大小可以不要这个参数  
    130.         commend.add("-vf");  
    131.         commend.add("scale=512:-3");  
    132.         // 帧速率设置  
    133.         commend.add("-ofps");  
    134.         commend.add("18");  
    135.         /*  
    136.          * mode=3:cbr:br=24单声道 音频码率为24kbps;-lameopts  
    137.          * mode=0:cbr:br=24立体声,音频码率为24kbps; 还可设置音量,-lameopts  
    138.          * mode=3:cbr:br=32:vol=1,设置范置为1~10,但不宜设得太高  
    139.          */  
    140.         commend.add("-lameopts");  
    141.         commend.add("vbr=3:br=128");  
    142.         commend.add("-o");  
    143.         commend.add(outPath);  
    144.         // 控制台显示执行的命令  
    145.         System.out.println(commend);  
    146.         try {  
    147.             ProcessBuilder builder = new ProcessBuilder();  
    148.             builder.command(commend);  
    149.             builder.start();  
    150.             return outPath;  
    151.         } catch (Exception e) {  
    152.             e.printStackTrace();  
    153.             return null;  
    154.         }  
    155.     }  
    156.   
    157.     /**  
    158.      * 使用ffmpeg转码  
    159.      *   
    160.      * @param videoPath  
    161.      *            源路径 -- 要转换的视频文件  
    162.      * @param targetPath  
    163.      *            目标路径 -- 转换后的视频flv  
    164.      * @return 返回目标路径  
    165.      */  
    166.     public synchronized  String ffmpegTransVideo() {   
    167.         // if (!checkfile(videoPath)) {   
    168.         // System.out.println(videoPath + " is not file aaa");   
    169.         // return false;   
    170.         // }   
    171.         List<String> commend = new java.util.ArrayList<String>();   
    172.         //commend.add("d:\flv\MediaCoder\codecs\ffmpeg.exe");   
    173.         //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\ffmpeg.exe");   
    174.         commend.add(ffmpegAdd);   
    175.         commend.add("-i");   
    176.         commend.add(filePath);   
    177.         commend.add("-ab");   
    178.         commend.add("64");   
    179.         // commend.add(" -acodec ");   
    180.         // commend.add("codec");   
    181.         commend.add("-ac");   
    182.         commend.add("2");   
    183.         commend.add("-ar");   
    184.         commend.add("22050");   
    185.         // 清晰度 -qscale 4 为最好可是文件大, -qscale 6就可以了   
    186.         commend.add("-qscale");   
    187.         commend.add("6");   
    188.         // commend.add("-b");   
    189.         // commend.add("768");   
    190.         // commend.add("230");   
    191.         // commend.add("-s");   
    192.         // commend.add("352x240");   
    193.         // commend.add("-r");   
    194.         // commend.add("29.97");   
    195.         commend.add("-y");   
    196.         commend.add(outPath);   
    197.         //System.out.println(commend);   
    198.         try {   
    199.           ProcessBuilder builder = new ProcessBuilder();   
    200.           builder.command(commend);   
    201.           Process process = builder.start();   
    202.           InputStream is = process.getErrorStream();   
    203.           InputStreamReader inputStreamReader = new InputStreamReader(is);   
    204.           BufferedReader inputBufferedReader = new BufferedReader(   
    205.               inputStreamReader);   
    206.           String line = null;   
    207.           StringBuilder stringBuilder = new StringBuilder();   
    208.           while ((line = inputBufferedReader.readLine()) != null) {   
    209.             stringBuilder.append(line);   
    210.           }   
    211.           inputBufferedReader.close();   
    212.           inputBufferedReader = null;   
    213.           inputStreamReader.close();   
    214.           inputStreamReader = null;   
    215.           is.close();   
    216.           is = null;   
    217.           return outPath;   
    218.         } catch (Exception e) {   
    219.           e.printStackTrace();   
    220.           return null;   
    221.         }   
    222.       }  
    223.   
    224.   
    225.     // 生成图片 参数String newfilename, String newimg  
    226.     public synchronized boolean ffmpegTransImage() {  
    227.         List<String> commend = new java.util.ArrayList<String>();  
    228.         // commend.add("d:\flv\MediaCoder\codecs\ffmpeg.exe");  
    229.         //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\ffmpeg.exe");   
    230.         commend.add(ffmpegAdd);  
    231.         commend.add("-i");  
    232.         commend.add(filePath);  
    233.         commend.add("-y");  
    234.         commend.add("-f");  
    235.         commend.add("image2");  
    236.         commend.add("-ss");  
    237.         commend.add("38");  
    238.         commend.add("-t");  
    239.         commend.add("0.001");  
    240.         commend.add("-s");  
    241.         commend.add("320x240");  
    242.         commend.add(outImgPath);  
    243.         try {  
    244.             ProcessBuilder builder = new ProcessBuilder();  
    245.             builder.command(commend);  
    246.             builder.start();  
    247.             return true;  
    248.         } catch (Exception e) {  
    249.             e.printStackTrace();  
    250.             return false;  
    251.         }  
    252.     }  
    253.   
    254. }  


     

    测试类代码 

     收藏代码

    1. package yong.method;  
    2.   
    3.   
    4. public class A {  
    5.   
    6.     /**  
    7.      * @param args  
    8.      */  
    9.     public static void main(String[] args) {  
    10.         // TODO Auto-generated method stub  
    11.         String filePath = "D:\test\a.MP4";  
    12.         String outPath = "d:\test\2011q.flv";  
    13.         String outImgPath = "d:\test\20111.jpg";  
    14.         String pathAddress="D:\Tomcat-6.0.29\webapps\zyk\";  
    15.         ThreadPool.trans(filePath,outPath,outImgPath,pathAddress);  
    16.   
    17.           
    18.     }  
    19. }  
  • 相关阅读:
    最短路之浇水
    agc031_d A Sequence of Permutations
    loj 3236 [POI2019 R1] Układ scalony
    CodeForces 1237H Balanced Reversals
    CodeForces 1320F Blocks and Sensors
    CodeForces 1340D Nastya and Time Machine
    agc037_f Counting of Subarrays
    nikkei2019_2_qual_e Non-triangular Triplets
    CodeForces 603E Pastoral Oddities
    Vue router / ElementUI,Ant Design Vue 重复点击导航路由报错解决方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317483.html
Copyright © 2020-2023  润新知