Java调用ffmepg+mencoder视频格式转换(*)
PS: 建议大家在官网下载最新的资源
其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现
ffmepg:
D:\ffmpeg\bin\ffmpeg.exe -i E:\1.mp4 -ab 64 -acodec mp3 -ac 2 -ar 22050 -b 230 -r 24 -y E:\111.flv
Mencoder:
D:\WisMencoder\mencoder.exe E:\1.rmvb -oac mp3lame -lameopts preset=64 -ovc xvid -xvidencopts bitrate=600 -of avi -o E:\1.avi
ps:网易视频 比较清晰 50min -》170mb 的FLV格式
/*** 功能:将任意格式的视频转化为flv格式,有利于在线视频播放* ps: ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等* mencoder 解析剩下的格式:wmv9,rm,rmvb * time:2013.12.11*/ package softflag.core.util; import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.List; import softflag.index.IndexService; public class VideoConverter { public synchronized void convert(String inputFile, String outputFile) { process(inputFile, outputFile); } private static String getAviFilePath(String filePath) { String path = filePath.substring(0, filePath.lastIndexOf(".")) + ".avi"; return path; } /** * 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI * * @param inputFile * @param outputFile * @return */ private void process(String inputFile, String outputFile) { int type = checkContentType(inputFile); if (type == 0) { processFLV(inputFile, outputFile);// 直接将文件转为flv文件 } else if (type == 1) { processAVI(type, inputFile,outputFile); } } /** * 检查视频类型 * * @param inputFile * @return ffmpeg 能解析返回0,不能解析返回1 */ private static int checkContentType(String inputFile) { String type = inputFile.substring(inputFile.lastIndexOf(".") + 1, inputFile.length()).toLowerCase(); // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) if (type.equals("avi")) { return 0; } else if (type.equals("mpg")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("3gp")) { return 0; } else if (type.equals("mov")) { return 0; } else if (type.equals("mp4")) { return 0; } else if (type.equals("asf")) { return 0; } else if (type.equals("asx")) { return 0; } else if (type.equals("flv")) { return 0; } // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. else if (type.equals("wmv9")) { return 1; } else if (type.equals("rm")) { return 1; } else if (type.equals("rmvb")) { return 1; } return 9; } /** * ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) * * @param inputFile * @param outputFile * @return */ private void processFLV(String inputFile, String outputFile) { try { try { List<String> commend = new java.util.ArrayList<String>(); // 低精度 commend.add(IndexService.getConfigValue("check_multi_ffmpegPath")); commend.add("-i"); commend.add(inputFile); commend.add("-ab"); commend.add("64"); commend.add("-acodec"); commend.add("mp3"); commend.add("-ac"); commend.add("2"); commend.add("-ar"); commend.add("22050"); commend.add("-b"); commend.add("230"); commend.add("-r"); commend.add("24"); commend.add("-y"); commend.add(outputFile); StringBuffer test = new StringBuffer(); for (int i = 0; i < commend.size(); i++) test.append(commend.get(i) + " ");// System.out.println(test); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); Process p = builder.start(); doWaitFor(p); p.destroy(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Mencoder: 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), * 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. * * @param type * @param inputFile * @return */ private void processAVI(int type, String inputFile,String outputFile) { String aviPath = getAviFilePath(inputFile); String outputFilePath = outputFile; File file = new File(aviPath); if (file.exists()) file.delete(); List<String> commend = new java.util.ArrayList<String>(); commend.add(IndexService.getConfigValue("check_multi_mencoderPath")); commend.add(inputFile); commend.add("-oac"); commend.add("mp3lame"); commend.add("-lameopts"); commend.add("preset=64"); commend.add("-ovc"); commend.add("xvid"); commend.add("-xvidencopts"); commend.add("bitrate=600"); commend.add("-of"); commend.add("avi"); commend.add("-o"); commend.add(aviPath); StringBuffer test = new StringBuffer(); for (int i = 0; i < commend.size(); i++) test.append(commend.get(i) + " ");// System.out.println(test); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); Process p = builder.start(); /** * 清空Mencoder进程 的输出流和错误流 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小, * 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。 */ final InputStream is1 = p.getInputStream(); final InputStream is2 = p.getErrorStream(); new Thread() { public void run() { BufferedReader br = new BufferedReader( new InputStreamReader(is1)); try { String lineB = null; while ((lineB = br.readLine()) != null) { if (lineB != null) System.out.println(lineB); } is1.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { BufferedReader br2 = new BufferedReader( new InputStreamReader(is2)); try { String lineC = null; while ((lineC = br2.readLine()) != null) { if (lineC != null) System.out.println(lineC); } br2.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); doWaitFor(p); p.destroy(); processFLV(aviPath,outputFilePath); } catch (Exception e) { System.err.println(e); } } public int doWaitFor(Process p) { InputStream in = null; InputStream err = null; int exitValue = -1; // returned to caller when p is finished try {// System.out.println("comeing"); in = p.getInputStream(); err = p.getErrorStream(); boolean finished = false; // Set to true when p is finished while (!finished) { try { while (in.available() > 0) { Character c = new Character((char) in.read()); System.out.print(c); } while (err.available() > 0) { Character c = new Character((char) err.read()); System.out.print(c); } exitValue = p.exitValue(); finished = true; } catch (IllegalThreadStateException e) { Thread.currentThread().sleep(500); } } } catch (Exception e) { System.err.println("doWaitFor();: unexpected exception - " + e.getMessage()); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { System.out.println(e.getMessage()); } if (err != null) { try { err.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } return exitValue; }}
转载于:https://www.cnblogs.com/coprince/p/3461664.html
Java调用ffmepg+mencoder视频格式转换(*)相关推荐
- Java调用ffmepg+mencoder视频格式转换
PS: ffmpeg.rev12665.7z版本相当老了,有很多命令可能出现错误 其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现 ffmepg版本:ffmpeg.rev12 ...
- wmv格式+java_Java调用ffmepg+mencoder视频格式转换(*)
/*** 功能:将任意格式的视频转化为flv格式,有利于在线视频播放 * ps: ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等 * mencod ...
- JAVA调用GDAL实现影像格式转换,以tif to jpg为例
前言 本文简单实现java调用GDAL实现影像格式转换. package Marcus.com;import org.gdal.gdal.Dataset; import org.gdal.gdal.D ...
- Ffmpeg,mencoder视频格式转换
一. Ffmpeg,mencoder视频格式转换 Mencoder 转 flv mencoder -ffourcc FLV1 -lavfopts i_certify_that_my_video_st ...
- Java使用ffmpeg进行视频格式转换、音视频合并、播放、截图
封装类: ffplay.ffmpeg.ffprobe是安装的ffmpeg路径. import java.io.BufferedReader; import java.io.File; import j ...
- 【JAVA】JAVA使用ffmpeg进行视频格式转换以及截图
JAVA 处理视频文件,需要用到 ffmpeg 这个工具.不需要依赖jar包.无论是在windows服务器还是linux服务器,想要使用 ffmpeg 都需要先安装. 附上Linux下 *.tar ...
- java调用print2flash_文档格式转换工具Print2Flash使用教程:使用皮肤更改按钮图像...
Print2Flash是一款强大的文件格式转换及文档发布工具.可以轻松实现可打印文档(如Word.PDF文档.PPT或是Excel电子表格)转换到 Adobe ® Flash ®文件(swf).htm ...
- ffmpeg 视频格式转换和宽高转换 制作自己想要的数据格式
ffmpeg 视频格式转换和宽高转换 制作自己想要的数据格式 命令如下: ffmpeg -y -i Titanic.mkv -s 640*480 out.h264 运行效果: 一般的 使用 ffmpe ...
- 使用FFmepg进行视频转码、视频格式转换、图片提取等!
一.什么是FFmpeg? FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常 ...
- Java视频格式转换---avi转MP4(h264编码格式)
Java视频格式转换---avi转MP4(h264编码格式的MP4视频在网页播放兼容性更好--试过其它的编码格式,没找到能在网页上直接播放的) 资源借鉴处 需要添加到pom.xml的包 java代码 ...
最新文章
- 用Gogs在Windows上搭建Git服务
- 计算机视觉系统中图像究竟经历了哪些“折磨”
- 自反ACL访问控制列表的应用
- php ci cookie使用,CI框架实现cookie登陆的方法详解
- oracle导入时 ora39166,impdp ORA-39002,ORA-39166,ORA-39164的问题及解决
- 如何解决tomcat提示文件被锁定
- JSZip的简单使用
- 网络交换机怎么连接硬盘录像机、网络交换机怎么连接摄像头
- SQL Server 2008 远程过程调用失败的问题解决方法
- vscode怎样新建项目和文件
- python代码画乌龟_python画乌龟
- 读Mybatis源码
- 云计算 码率适配限速_一种基于云计算的应用于用户终端的测速方法
- 基于AT91RM9200与LINUX2.6.26内核的嵌入式平台开发全过程
- 电子书翻页效果(转)
- JavaScript:替换原段落中的文字并将其变成红色
- Java数据类型转换02--强制类型转换
- 0-1背包-poj-1948-Triangular Pastures
- SSR 应用与原 CSR 应用变更同步问题实践
- 《我的世界》Python编程入门(0) 给家长的建议