国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
JAVA+ffmpeg+mencoder轉(zhuǎn)換視頻

FFmpeg是一個(gè)開源免費(fèi)跨平臺(tái)的視頻和音頻流方案,屬于自由軟件,采用LGPL或GPL許可證(依據(jù)你選擇的組件)。它提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案。它包含了非常先進(jìn)的音頻/視頻編解碼庫libavcodec,為了保證高可移植性和編解碼質(zhì)量,libavcodec里很多codec都是從頭開發(fā)的。FFmpeg在Linux平臺(tái)下開發(fā),但它同樣也可以在其它操作系統(tǒng)環(huán)境中編譯運(yùn)行。

mencoder 是一款命令行方式的視頻處理軟件,是Mplayer自帶的編碼工具(Mplayer是Linux下的播放器,開源,支持幾乎所有視頻格式的播放,現(xiàn)在有windows和Mac版本)。

這種方式來實(shí)現(xiàn)在web頁面播放視頻的思路如下:可以將各種視頻格式轉(zhuǎn)為flv的格式,然后在頁面上只需要有一個(gè)flash播放器即可播放flv文件了,這樣的話就不用考慮使用不同的播放器來播放的情況,而且頁面代碼簡(jiǎn)潔。

以下為轉(zhuǎn)換代碼:

(1)將視頻轉(zhuǎn)換過程做成一個(gè)幫助類:

  1. import java.io.BufferedReader;    
  2. import java.io.File;    
  3. import java.io.IOException;    
  4. import java.io.InputStream;    
  5. import java.io.InputStreamReader;    
  6. import java.util.List;    
  7. public class ConvertSingleVideo {  
  8.       
  9.     private static String mencoder_home = "D:\\javaserve\\mencoder\\mencoder.exe";//mencoder.exe所放的路徑  
  10.     private static String ffmpeg_home = "D:\\javaserve\\ffmpeg\\ffmpeg.exe";//ffmpeg.exe所放的路徑  
  11.       
  12.     public static String inputFile_home = "F:\\java\\work\\jingpinkecheng\\WebRoot\\upload\\input\\";//需轉(zhuǎn)換的文件的位置  
  13.     public static String outputFile_home = "D:\\javaserve\\tomcat\\apache-tomcat-7.0.32\\webapps\\jingpinkecheng\\upload\\output\\";//轉(zhuǎn)換后的flv文件所放的文件夾位置  
  14.     private String tempFile_home;//存放rm,rmvb等無法使用ffmpeg直接轉(zhuǎn)換為flv文件先轉(zhuǎn)成的avi文件  
  15.        
  16.      public ConvertSingleVideo(String tempFilePath){  
  17.          this.tempFile_home = tempFilePath;  
  18.      }  
  19.        
  20.         /**  
  21.          *  功能函數(shù)  
  22.          * @param inputFile 待處理視頻,需帶路徑  
  23.          * @param outputFile 處理后視頻,需帶路徑  
  24.          * @return  
  25.          */    
  26.         public  boolean convert(String inputFile, String outputFile)    
  27.         {    
  28.             if (!checkfile(inputFile)) {    
  29.                 System.out.println(inputFile + " is not file");    
  30.                 return false;    
  31.             }    
  32.             if (process(inputFile,outputFile)) {    
  33.                 System.out.println("ok");    
  34.                 return true;    
  35.             }    
  36.             return false;    
  37.         }    
  38.         //檢查文件是否存在    
  39.         private  boolean checkfile(String path) {    
  40.             File file = new File(path);    
  41.             if (!file.isFile()) {    
  42.                 return false;    
  43.             }    
  44.             return true;    
  45.         }    
  46.         /**  
  47.          * 轉(zhuǎn)換過程 :先檢查文件類型,在決定調(diào)用 processFlv還是processAVI  
  48.          * @param inputFile  
  49.          * @param outputFile  
  50.          * @return  
  51.          */    
  52.         private  boolean process(String inputFile,String outputFile) {    
  53.             int type = checkContentType( inputFile);    
  54.             boolean status = false;    
  55.             if (type == 0) {    
  56.                 status = processFLV(inputFile,outputFile);// 直接將文件轉(zhuǎn)為flv文件    
  57.             } else if (type == 1) {    
  58.                 String avifilepath = processAVI(type,inputFile);    
  59.                 if (avifilepath == null)    
  60.                     return false;// avi文件沒有得到    
  61.                 status = processFLV(avifilepath,outputFile);// 將avi轉(zhuǎn)為flv    
  62.             }    
  63.             return status;    
  64.         }    
  65.         /**  
  66.          * 檢查視頻類型  
  67.          * @param inputFile  
  68.          * @return ffmpeg 能解析返回0,不能解析返回1  
  69.          */    
  70.         private  int checkContentType(String inputFile) {    
  71.             String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,inputFile.length()).toLowerCase();    
  72.             // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)    
  73.             if (type.equals("avi")) {    
  74.                 return 0;    
  75.             } else if (type.equals("mpg")) {    
  76.                 return 0;    
  77.             } else if (type.equals("wmv")) {    
  78.                 return 0;    
  79.             } else if (type.equals("3gp")) {    
  80.                 return 0;    
  81.             } else if (type.equals("mov")) {    
  82.                 return 0;    
  83.             } else if (type.equals("mp4")) {    
  84.                 return 0;    
  85.             } else if (type.equals("asf")) {    
  86.                 return 0;    
  87.             } else if (type.equals("asx")) {    
  88.                 return 0;    
  89.             } else if (type.equals("flv")) {    
  90.                 return 0;    
  91.             }    
  92.             // 對(duì)ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),    
  93.             // 可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.    
  94.             else if (type.equals("wmv9")) {    
  95.                 return 1;    
  96.             } else if (type.equals("rm")) {    
  97.                 return 1;    
  98.             } else if (type.equals("rmvb")) {    
  99.                 return 1;    
  100.             }    
  101.             return 9;    
  102.         }    
  103.         /**  
  104.          *  ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
  105.          * @param inputFile  
  106.          * @param outputFile  
  107.          * @return  
  108.          */    
  109.         private  boolean processFLV(String inputFile,String outputFile) {    
  110.             if (!checkfile(inputFile)) {    
  111.                 System.out.println(inputFile + " is not file");    
  112.                 return false;    
  113.             }   
  114.             File file = new File(outputFile);  
  115.             if(file.exists()){  
  116.                 System.out.println("flv文件已經(jīng)存在!無需轉(zhuǎn)換");  
  117.                 return true;  
  118.             } else {  
  119.                 System.out.println("正在轉(zhuǎn)換成flv文件……");  
  120.                   
  121.                 List<String> commend = new java.util.ArrayList<String>();    
  122.                 //低精度    
  123.                 commend.add(ffmpeg_home);  
  124.                 commend.add("-i");    
  125.                 commend.add(inputFile);    
  126.                 commend.add("-ab");    
  127.                 commend.add("128");    
  128.                 commend.add("-acodec");    
  129.                 commend.add("libmp3lame");    
  130.                 commend.add("-ac");    
  131.                 commend.add("1");    
  132.                 commend.add("-ar");    
  133.                 commend.add("22050");    
  134.                 commend.add("-r");    
  135.                 commend.add("29.97");   
  136.                 // 清晰度 -qscale 4 為最好但文件大, -qscale 6就可以了  
  137.                 commend.add("-qscale");    
  138.                 commend.add("4");    
  139.                 commend.add("-y");    
  140.                 commend.add(outputFile);    
  141.                 StringBuffer test=new StringBuffer();    
  142.                 for(int i=0;i<commend.size();i++)    
  143.                     test.append(commend.get(i)+" ");    
  144.                 System.out.println(test);    
  145.                 try {    
  146.                     ProcessBuilder builder = new ProcessBuilder();    
  147.                     builder.command(commend);    
  148.                     builder.start();   
  149.                     return true;    
  150.                 } catch (Exception e) {    
  151.                     e.printStackTrace();    
  152.                     return false;    
  153.                 }    
  154.                  
  155.             }  
  156.              
  157.         }    
  158.         /**  
  159.          * Mencoder:  
  160.          * 對(duì)ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.  
  161.          * @param type  
  162.          * @param inputFile  
  163.          * @return  
  164.          */    
  165.         private  String processAVI(int type,String inputFile) {    
  166.             File file =new File(tempFile_home);    
  167.             if(file.exists()){  
  168.                 System.out.println("avi文件已經(jīng)存在!無需轉(zhuǎn)換");  
  169.                 return tempFile_home;  
  170.             }    
  171.             List<String> commend = new java.util.ArrayList<String>();    
  172.             commend.add(mencoder_home);    
  173.             commend.add(inputFile);    
  174.             commend.add("-oac");    
  175.             commend.add("mp3lame");    
  176.             commend.add("-lameopts");    
  177.             commend.add("preset=64");    
  178.             commend.add("-ovc");    
  179.             commend.add("xvid");    
  180.             commend.add("-xvidencopts");    
  181.             commend.add("bitrate=600");    
  182.             commend.add("-of");    
  183.             commend.add("avi");    
  184.             commend.add("-o");    
  185.             commend.add(tempFile_home);    
  186.             StringBuffer test=new StringBuffer();    
  187.             for(int i=0;i<commend.size();i++)    
  188.                 test.append(commend.get(i)+" ");    
  189.             System.out.println(test);    
  190.             try     
  191.             {    
  192.                 ProcessBuilder builder = new ProcessBuilder();    
  193.                 builder.command(commend);    
  194.                 Process p=builder.start();    
  195.                 /**  
  196.                  * 清空Mencoder進(jìn)程 的輸出流和錯(cuò)誤流  
  197.                  * 因?yàn)橛行┍緳C(jī)平臺(tái)僅針對(duì)標(biāo)準(zhǔn)輸入和輸出流提供有限的緩沖區(qū)大小,  
  198.                  * 如果讀寫子進(jìn)程的輸出流或輸入流迅速出現(xiàn)失敗,則可能導(dǎo)致子進(jìn)程阻塞,甚至產(chǎn)生死鎖。   
  199.                  */    
  200.                 final InputStream is1 = p.getInputStream();    
  201.                 final InputStream is2 = p.getErrorStream();    
  202.                 new Thread() {    
  203.                     public void run() {    
  204.                         BufferedReader br = new BufferedReader(new InputStreamReader(is1));    
  205.                         try {    
  206.                             String lineB = null;    
  207.                             while ((lineB = br.readLine()) != null ){    
  208.                                 if(lineB != null)System.out.println(lineB);    
  209.                             }    
  210.                         } catch (IOException e) {    
  211.                             e.printStackTrace();    
  212.                         }    
  213.                     }    
  214.                 }.start();     
  215.                 new Thread() {    
  216.                     public void run() {    
  217.                         BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));    
  218.                         try {    
  219.                             String lineC = null;    
  220.                             while ( (lineC = br2.readLine()) != null){    
  221.                                 if(lineC != null)System.out.println(lineC);    
  222.                             }    
  223.                         } catch (IOException e) {    
  224.                             e.printStackTrace();    
  225.                         }    
  226.                     }    
  227.                 }.start();     
  228.                     
  229.                 //等Mencoder進(jìn)程轉(zhuǎn)換結(jié)束,再調(diào)用ffmpeg進(jìn)程    
  230.                 p.waitFor();    
  231.                  System.out.println("who cares");    
  232.                 return tempFile_home;    
  233.             }catch (Exception e){     
  234.                 System.err.println(e);     
  235.                 return null;    
  236.             }     
  237.         }    
  238.     }    

(2)action中調(diào)用該類的方法:

  1. public String showResourceJiaoxueshipin() throws IOException{  
  2.         resourcejiaoxueshipin = itemService.findById(resourcejiaoxueshipinid);  
  3.           
  4.         String fileName = resourcejiaoxueshipin.getFirst_img();  
  5.         ConvertSingleVideo conver = new ConvertSingleVideo("F:\\java\\work\\jingpinkecheng\\WebRoot\\upload\\temp\\" + fileName.substring(0,fileName.lastIndexOf("."))+".avi");  
  6.           
  7.         conver.convert(ConvertSingleVideo.inputFile_home + fileName, ConvertSingleVideo.outputFile_home + fileName.substring(0,fileName.lastIndexOf("."))+".flv");  
  8.           
  9.         HttpSession session = request.getSession();  
  10.         fileName = new String(fileName.getBytes("UTF-8"),"GBK");//存到session后在jsp頁面取出的值是GBK("亂碼"),所以這里先變成亂碼,傳輸過去之后即可消除  
  11.         session.setAttribute("jiaoxueshipinName",fileName.substring(0,fileName.lastIndexOf("."))+".flv" );  
  12.         System.out.println("我是測(cè)試:"+session.getAttribute("jiaoxueshipinName"));  
  13.          return "success";  
  14.           
  15.           
  16.           
  17.     }  

3)jsp代碼:

只需要在頁面嵌入一個(gè)flash播放器,我這里使用vcastr22.swf,并且將flv文件的路徑填寫正確即可。嵌入代碼如下:

  1. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="120" width="190">  
  2.     <param name="movie" value="../upload/vcastr22.swf?vcastr_file=../upload/output/${sessionScope.jiaoxueshipinName }">  
  3.     <param name="quality" value="high">  
  4.     <param name="allowFullScreen" value="true" />  
  5.     <!-- src里就是播放器的路徑以及需要顯示的flv文件的路徑,路徑一定要正確! -->  
  6.     <embed  
  7.         src="../upload/vcastr22.swf?vcastr_file=../upload/output/${sessionScope.jiaoxueshipinName }"  
  8.         quality="high"  
  9.         pluginspage="http://www.macromedia.com/go/getflashplayer"  
  10.         type="application/x-shockwave-flash" width="500" height="350">  
  11.     </embed>  
  12. </object>   


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java實(shí)現(xiàn)視頻上傳和播放SpringMVC + Mybatis + ckplayer+ffmpeg+mencoder
ffmpeg.exe與mencoder.exe實(shí)例轉(zhuǎn)換操作
web/java實(shí)現(xiàn)多種格式視頻上傳、轉(zhuǎn)碼、截圖、播放、下載等功能附源碼(詳細(xì))
FFmpeg:視頻轉(zhuǎn)碼、剪切、合并、播放速調(diào)整
Java+Windows+ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)換
java實(shí)現(xiàn)視頻文件轉(zhuǎn)換為flv(帶文件縮略圖)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服