如何使用java代码进行视频格式的转换(FLV)

一,前言

在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放。

公司一直在使用中,配合使用,感觉不错,每天转换的文件超过上千个。

二,准备

工具包  : drv43260.dll,ffmpeg.exe,mencoder.exe,pncrt.dll,pthreadGC2.dll

原理其实是使用java代码在CMD下调用MediaCode,完成视频格式的转换。

完整项目下载地址: ConverVedio

播放器我用的是ckplayer

三,部分代码

  1. package yctc.cp.converter;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.List;
  8. public class ConvertFlv {
  9. public static void main(String[] args) {
  10. ConvertFlv.convert("D:\\2.avi", "D:\\3.flv");
  11. }
  12. public static boolean convert(String inputFile, String outputFile)
  13. {
  14. if (!checkfile(inputFile)) {
  15. System.out.println(inputFile + " is not file");
  16. return false;
  17. }
  18. if (process(inputFile,outputFile)) {
  19. System.out.println("ok");
  20. return true;
  21. }
  22. return false;
  23. }
  24. //检查文件是否存在
  25. private static boolean checkfile(String path) {
  26. File file = new File(path);
  27. if (!file.isFile()) {
  28. return false;
  29. }
  30. return true;
  31. }
  32. private static boolean process(String inputFile,String outputFile) {
  33. int type = checkContentType( inputFile);
  34. boolean status = false;
  35. if (type == 0) {
  36. status = processFLV(inputFile,outputFile);// 直接将文件转为flv文件
  37. } else if (type == 1) {
  38. String avifilepath = processAVI(type,inputFile);
  39. if (avifilepath == null)
  40. return false;// avi文件没有得到
  41. status = processFLV(avifilepath,outputFile);// 将avi转为flv
  42. }
  43. return status;
  44. }
  45. /**
  46. * 检查视频类型
  47. * @param inputFile
  48. * @return ffmpeg 能解析返回0,不能解析返回1
  49. */
  50. private static int checkContentType(String inputFile) {
  51. String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
  52. inputFile.length())
  53. .toLowerCase();
  54. // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
  55. if (type.equals("avi")) {
  56. return 0;
  57. } else if (type.equals("mpg")) {
  58. return 0;
  59. } else if (type.equals("wmv")) {
  60. return 0;
  61. } else if (type.equals("3gp")) {
  62. return 0;
  63. } else if (type.equals("mov")) {
  64. return 0;
  65. } else if (type.equals("mp4")) {
  66. return 0;
  67. } else if (type.equals("asf")) {
  68. return 0;
  69. } else if (type.equals("asx")) {
  70. return 0;
  71. } else if (type.equals("flv")) {
  72. return 0;
  73. }
  74. // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  75. // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  76. else if (type.equals("wmv9")) {
  77. return 1;
  78. } else if (type.equals("rm")) {
  79. return 1;
  80. } else if (type.equals("rmvb")) {
  81. return 1;
  82. }
  83. return 9;
  84. }
  85. /**
  86. *  ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
  87. * @param inputFile
  88. * @param outputFile
  89. * @return
  90. */
  91. private static boolean processFLV(String inputFile,String outputFile) {
  92. if (!checkfile(inputFile)) {
  93. System.out.println(inputFile + " is not file");
  94. return false;
  95. }
  96. List<String> commend = new java.util.ArrayList<String>();
  97. //低精度
  98. commend.add("");
  99. commend.add("-i");
  100. commend.add(inputFile);
  101. commend.add("-ab");
  102. commend.add("128");
  103. commend.add("-acodec");
  104. commend.add("libmp3lame");
  105. commend.add("-ac");
  106. commend.add("1");
  107. commend.add("-ar");
  108. commend.add("22050");
  109. commend.add("-qscale");
  110. commend.add("4");
  111. commend.add("-s");
  112. commend.add("350x240");
  113. commend.add("-r");
  114. commend.add("29.97");
  115. commend.add("-b");
  116. commend.add("512");
  117. commend.add("-y");
  118. commend.add(outputFile);
  119. StringBuffer test=new StringBuffer();
  120. for(int i=0;i<commend.size();i++)
  121. test.append(commend.get(i)+" ");
  122. System.out.println(test);
  123. try {
  124. ProcessBuilder builder = new ProcessBuilder();
  125. builder.command(commend);
  126. builder.start();
  127. return true;
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. return false;
  131. }
  132. }
  133. /**
  134. * Mencoder:
  135. *  对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  136. 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  137. * @param type
  138. * @param inputFile
  139. * @return
  140. */
  141. private static String processAVI(int type,String inputFile) {
  142. File file =new File("");
  143. if(file.exists())   file.delete();
  144. List<String> commend = new java.util.ArrayList<String>();
  145. commend.add("");
  146. commend.add(inputFile);
  147. commend.add("-oac");
  148. commend.add("mp3lame");
  149. commend.add("-lameopts");
  150. commend.add("preset=64");
  151. commend.add("-ovc");
  152. commend.add("xvid");
  153. commend.add("-xvidencopts");
  154. commend.add("bitrate=600");
  155. commend.add("-of");
  156. commend.add("avi");
  157. commend.add("-o");
  158. commend.add("");
  159. StringBuffer test=new StringBuffer();
  160. for(int i=0;i<commend.size();i++)
  161. test.append(commend.get(i)+" ");
  162. System.out.println(test);
  163. try
  164. {
  165. ProcessBuilder builder = new ProcessBuilder();
  166. builder.command(commend);
  167. Process p=builder.start();
  168. /**
  169. * 清空Mencoder进程 的输出流和错误流
  170. * 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
  171. * 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
  172. */
  173. final InputStream is1 = p.getInputStream();
  174. final InputStream is2 = p.getErrorStream();
  175. new Thread() {
  176. public void run() {
  177. BufferedReader br = new BufferedReader( new
  178. InputStreamReader(is1));
  179. try {
  180. String lineB = null;
  181. while ((lineB = br.readLine()) != null ){
  182. if(lineB != null)System.out.println(lineB);
  183. }
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }.start();
  189. new Thread() {
  190. public void run() {
  191. BufferedReader br2 = new BufferedReader( new
  192. InputStreamReader(is2));
  193. try {
  194. String lineC = null;
  195. while ( (lineC = br2.readLine()) != null){
  196. if(lineC != null)System.out.println(lineC);
  197. }
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. }
  201. }
  202. }.start();
  203. p.waitFor();
  204. System.out.println("who cares");
  205. return "";
  206. }catch (Exception e){
  207. System.err.println(e);
  208. return null;
  209. }
  210. }
  211. }

如何使用java代码进行视频格式的转换(FLV)的更多相关文章

  1. java程序实现视频格式的转换

    http://blog.sina.com.cn/s/blog_96b60b0c01013mi5.html 原文地址:java程序实现视频格式的转换作者:笑看风云 flv格式转换--第一步 不定期更新. ...

  2. Flv视频格式如何转换成MP4格式

    如何将flv视频格式转换成MP4格式呢?随着现在视频格式的不断多样化,视频格式转换的问题也成了现在生活中常见的问题,那么我们应该怎样将flv视频格式转换成MP4格式呢?下面我们就一起来看一下吧. 操作 ...

  3. asp.net实现调用ffmpeg实现视频格式的转换

    视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...

  4. java 代码获取视频时长

    package test; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.MultimediaInfo; i ...

  5. java代码发送JSON格式的httpPOST请求

    package com.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOE ...

  6. (转)java代码发送JSON格式的httpPOST请求

    import Java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...

  7. java 代码判断图片格式后缀名称

    /** * 图片判断 */ private static String getFormatName(Object o) { try { // Create an image input stream ...

  8. [改善Java代码]避开基本类型数组转换列表陷阱

    开发中经常用到Arrays和Collections这两个工具类. 在数组和列表之间进行切换.非常方便.但是也会遇到一些问题. 看代码: import java.util.Arrays; import ...

  9. 使用resumable.js上传大文件(视频)兵转换flv格式

    前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Video.asp ...

随机推荐

  1. [poj2398]Toy Storage

    接替关键:和上题类似,输出不同,注意输入这道题需要排序. #include<cstdio> #include<cstring> #include<algorithm> ...

  2. ann

    转自 http://blog.csdn.net/yiluoyan/article/details/45308785 这篇文章接着之前的车牌识别,从输入的车图片中分割识别出车牌之后,将进行下一步:车牌号 ...

  3. STM32 C++编程 001 工程模板

    将 STM32的官方工程模板 修改为我们这套教材的:STM32 C++工程模板 我使用的 STM32 库的版本 : V3.5.0 注意: 想学习本套 STM32 C++编程 的专栏是有点门槛的.你需要 ...

  4. 39、count_rpkm_fpkm_TPM

    参考:https://f1000research.com/articles/4-1521/v1 https://www.biostars.org/p/171766/ http://www.rna-se ...

  5. Bulma 源码结构

    源码基于 Bulma 0.4.0 版本. 一.入口文件 bulma.sass bulma.sass 是 Bulma 使用 SASS 编译的入口文件. sass bulma.sass css/bulma ...

  6. JS 求解时间差

    function UTCTimeDemo($str){ var d, s = "当前 UTC 时间为: "; var c = ":"; d = new Date ...

  7. CodeForces 478D Red-Green Towers (DP)

    题意:给定 n 块红砖,m 块绿砖,问有多少种方式可以建造成最高的塔,每一层颜色必须一样. 析:首先要确定最高是多少层h,大约应该是用 h * (h+1) <= (m+n) * 2,然后dp[i ...

  8. AttributeError: 'module' object has no attribute 'gfile'

    While running TensorFlow's classify_image, getting AttributeError: 'module' object has no attribute ...

  9. C语言结构体--位域

    有些数据在存储时并不需要占用一个完整的字节,只需要占用一个或几个二进制位即可.比如开关只有通电和断电两种状态,用 0 和 1 表示足以,也就是用一个二进位.正是基于这种考虑,C语言又提供了一种叫做位域 ...

  10. 对bookinfo.dat的说明

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 现在论坛推出的下载工具五花八门,但是有不少都忽视了bookinfo.dat的生成,因此有必要说明一下这个 ...