如何使用java代码进行视频格式的转换(FLV)
来源:互联网 发布:编程达人 vip教程 编辑:程序博客网 时间:2024/06/05 18:24
一,前言
在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放。
二,准备
工具包 : drv43260.dll,ffmpeg.exe,mencoder.exe,pncrt.dll,pthreadGC2.dll
原理其实是使用java代码在CMD下调用MediaCode,完成视频格式的转换。
完整项目下载地址: ConverVedio
播放器我用的是ckplayer
三,部分代码
- package yctc.cp.converter;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.List;
- public class ConvertFlv {
- public static void main(String[] args) {
- ConvertFlv.convert("D:\\2.avi", "D:\\3.flv");
- }
- public static boolean convert(String inputFile, String outputFile)
- {
- if (!checkfile(inputFile)) {
- System.out.println(inputFile + " is not file");
- return false;
- }
- if (process(inputFile,outputFile)) {
- System.out.println("ok");
- return true;
- }
- return false;
- }
- //检查文件是否存在
- private static boolean checkfile(String path) {
- File file = new File(path);
- if (!file.isFile()) {
- return false;
- }
- return true;
- }
- private static boolean process(String inputFile,String outputFile) {
- int type = checkContentType( inputFile);
- boolean status = false;
- if (type == 0) {
- status = processFLV(inputFile,outputFile);// 直接将文件转为flv文件
- } else if (type == 1) {
- String avifilepath = processAVI(type,inputFile);
- if (avifilepath == null)
- return false;// avi文件没有得到
- status = processFLV(avifilepath,outputFile);// 将avi转为flv
- }
- return status;
- }
- /**
- * 检查视频类型
- * @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 static boolean processFLV(String inputFile,String outputFile) {
- if (!checkfile(inputFile)) {
- System.out.println(inputFile + " is not file");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- //低精度
- commend.add("");
- commend.add("-i");
- commend.add(inputFile);
- commend.add("-ab");
- commend.add("128");
- commend.add("-acodec");
- commend.add("libmp3lame");
- commend.add("-ac");
- commend.add("1");
- commend.add("-ar");
- commend.add("22050");
- commend.add("-qscale");
- commend.add("4");
- commend.add("-s");
- commend.add("350x240");
- commend.add("-r");
- commend.add("29.97");
- commend.add("-b");
- commend.add("512");
- 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);
- builder.start();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * Mencoder:
- * 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
- 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- * @param type
- * @param inputFile
- * @return
- */
- private static String processAVI(int type,String inputFile) {
- File file =new File("");
- if(file.exists()) file.delete();
- List<String> commend = new java.util.ArrayList<String>();
- commend.add("");
- 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("");
- 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);
- }
- } 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);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- p.waitFor();
- System.out.println("who cares");
- return "";
- }catch (Exception e){
- System.err.println(e);
- return null;
- }
- }
- }
0 0
- 如何使用java代码进行视频格式的转换(FLV)
- 如何使用java代码进行视频格式的转换(FLV)
- java调用ffmpeg转换视频格式为flv
- (浏览器兼容问题)使用ffmpeg进行视频格式转换的用法
- 用Mencoder进行任意视频格式转换成flv
- 用Mencoder进行任意视频格式转换成flv
- 【JAVA】JAVA使用ffmpeg进行视频格式转换以及截图
- 所有视频格式转换为flv
- FLV视频格式详解(转)
- 视频格式转换之ffmpeg的使用
- Java 调用FFMPEG命令进行视频格式转换 (windows环境)
- 使用Java自带的API进行JAVA代码与XML文件的转换(JAXB)
- ffmpeg视频格式转换(Java)
- java 实现 视频格式转换。
- 关于FLV视频格式的知识整理!
- 用于 Flash 的 FLV 和 F4V 视频格式
- IIS 支持flv视频格式的方法
- 如何使用免费工具进行音/视频格式转化
- 如何自定义button的点击有效区域
- Eclipse项目无法自动发布问题解决
- 测试用例
- FlexPaper+SWFTools 实现仿百度文库及一些小问题
- mail 曾经最牛B的方法;
- 如何使用java代码进行视频格式的转换(FLV)
- Learning C++ by Creating Games With UE4(15.05.21)(Chapter 11-2)Monster
- mysql索引失效的五种情况分析
- iOS获取股票行情
- java 视频处理 mencoder
- JAVA常见面试题之Forward和Redirect的区别
- 前端中文输入法对剩余字数计算的影响
- iOS 判读是否登录
- eclipse启动优化(一)