应用ffmpeg进行视频文件格式转换

  1.  //视频格式转换函数
  2. public static boolean process(String resourcePath) 
  3.         int type = checkContentType(resourcePath); //先检测源文件类型是不是ffmpeg能解析的格式
  4.         boolean status = false
  5.         if (type == 0) 
  6.             status = processFLV(resourcePath);// 直接将文件转为flv文件 
  7.         } 
  8.         return status; 
  9.  
  10.  //判断参数的视频格式是不是
  11. private static int checkContentType(String resourcePath) 
  12.         String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1, 
  13.         resourcePath.length()).toLowerCase(); 
  14.         
  15. // cuplayer.com  ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) 
  16.         if (type.equals("avi")) 
  17.             return 0; 
  18.         } 
  19. else if (type.equals("mpg")) 
  20.             return 0; 
  21.         } 
  22. else if (type.equals("wmv")) 
  23.             return 0; 
  24.         } 
  25. else if (type.equals("3gp")) 
  26.             return 0; 
  27.         } 
  28. else if (type.equals("mov"))
  29.             return 0; 
  30.         } 
  31. else if (type.equals("mp4")) 
  32.             return 0; 
  33.         } 
  34. else if (type.equals("asf")) 
  35.             return 0; 
  36.         } 
  37. else if (type.equals("asx")) 
  38.             return 0; 
  39.         } 
  40. else if (type.equals("flv")) 
  41.             return 0; 
  42.         } 
  43. else if (type.equals("mpeg")) 
  44.             return 0; 
  45.         } 
  46. else if (type.equals("mpe")) 
  47.             return 0; 
  48.         } 
  49.         // cuplayer.com 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. 
  50.         else if (type.equals("wmv9")) 
  51.             return 1; 
  52.         } 
  53. else if (type.equals("rm")) 
  54.             return 1; 
  55.         } 
  56. else if (type.equals("rmvb")) 
  57.             return 1; 
  58.         } 
  59.         return 9; 
  60.     } 
  61.  
  62.     private static boolean checkfile(String path) 
  63.         File file = new File(path); 
  64.         if (!file.isFile()) 
  65.             return false; 
  66.         } 
  67.         return true; 
  68.     } 
  69.  
  70.     
  71. // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) 
  72.     private static boolean processFLV(String resourcePath) 
  73.  
  74.         if (!checkfile(resourcePath)) 
  75.             System.out.println(resourcePath + "   is   not   file"); 
  76.             return false; 
  77.         } 
  78.         // 文件命名 
  79.         Calendar c = Calendar.getInstance(); 
  80.         String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000); 
  81.         List commend = new ArrayList(); 
  82.         commend.add("e:\\ffmpeg"); 
  83.         commend.add("-i"); 
  84.         commend.add(resourcePath); 
  85. //      commend.add("-ab"); //音频流码率:(默认是同源文件码率) 
  86. //      commend.add("56"); 
  87.         commend.add("-ar"); //视频流采样率:(大多数情况下使用44100和48000,分别对用PAL和NTSC制式,根据需要选择) 
  88.         commend.add("22050"); 
  89.         commend.add("-qscale"); //视频量化指标 
  90.         commend.add("8"); 
  91.         commend.add("-r"); //视频流帧数(一般来书PAL制式同常用25,ntsc制式通常用29) 
  92.         commend.add("15"); 
  93.         commend.add("-s"); //视频解析度:(分辨率)可以自己定义所需要的大小: 改变视频流的解析式很耗cpu的 
  94.         commend.add("600x500"); 
  95.         commend.add("e:\\" + savename + ".flv"); 
  96.         try { 
  97.              Runtime runtime = Runtime.getRuntime(); 
  98.              Process proce = null
  99.              String cmd = ""
  100.              String cut = "     e://ffmpeg.exe   -i   " 
  101.                     + resourcePath 
  102.                     + "   -y   -f   image2   -ss   8   -t   0.001   -s   600x500   e:\\" 
  103.                     + savename + ".jpg"; 
  104.              String cutCmd = cmd + cut; 
  105.              proce = runtime.exec(cutCmd); 
  106.              ProcessBuilder builder = new ProcessBuilder(commend); 
  107.              // builder.command(commend); 
  108.              builder.start(); 
  109.   return true; 
  110.          } 
  111. catch (Exception e) 
  112.              e.printStackTrace(); 
  113.              return false; 
  114.          } 
  115.     } 
  116.  
  117.     public static void main(String[] args) 
  118.         if (!checkfile("e:\\1-android_sdk.flv")) 
  119.             System.out.println("" + "   is   not   file"); 
  120.             return; 
  121.         } 
  122.         if (process("e:\\1-android_sdk.flv")) 
  123.             System.out.println("ok"); 
  124.         } 
  125.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值