JSP上传视频后自动转成flv的核心JAVA方法

来源:互联网 发布:html css js 教程视频 编辑:程序博客网 时间:2024/05/17 09:21

作者:古刹飞鹰

BLOG:www.v246.com

QQ:28095553

Email:aquaqu(圈.a)gmail.com

本文属古刹飞鹰原创,转载请注明出处!

本方法以经经过实战验证,正在项目中使用,稍后您可以查看:http://www.0431net.com,网站的视频模快的核心功能就是用这个方法实现的:)

以下是源码:

Cnvert 类

package com.v246.convertFLV;


import java.util.ArrayList;
import java.util.List;


import com.v246.utils.Aqu;

//懒汉模式
public class Convert {

 private static Convert instance = null;

 // flv截图工具的绝对地址
 private String aquInterceptPicToolsPath ="c:\\FLVTools\\ffmpeg.exe";

 // flv修复工具的绝对地址
 private String fixFLVToolsPath ="c:\\FLVTools\\flvmdi.exe";

 // flv转换工具的绝对地址
 private String aquCoverFLVToolsPath ="c:\\FLVTools\\mencoder\\mencoder.exe";

 private Convert() {
  //init();
 }

 public static Convert getInstance() {
  if (instance == null) {
   instance =new Convert();
  }
  return instance;
 }
 public void reset(){
  //init();
 }
 private void init() {
  

 }

 public synchronized boolean convert(StringaquInputPath, String outPath) {
  boolean re = false;
  // 通一路径定位符
  aquInputPath =aquInputPath.replace("/", "\");
  // 通一路径定位符
  outPath = outPath.replace("/","\");
  // 分析不包含文件名的输出路径
  String interceptPicSavePath =outPath.substring(0, outPath
    .lastIndexOf("\")+ 1);
  // 分析输出的文件名
  String outFileName =outPath.substring(outPath.lastIndexOf("\") + 1,
    outPath.length());
  // 继续分析得出不包含扩展名的文件名
  String outFileNameNoExt =outFileName.substring(0, outFileName
    .lastIndexOf("."));

  // 工作目录,随便设置
  // String workdirectory ="c:\\windows\\temp";
  // 修复命令
  List<String>parameterForFix = newArrayList<String>(100);
  // 转换命令
  List<String>aquParameterForConvert = newArrayList<String>(100);
  // 截图命令
  List<String>aquParameterForIntercept = newArrayList<String>(100);
  // 构建转换命令
  aquParameterForConvert.add(aquCoverFLVToolsPath);
  aquParameterForConvert.add("-vf");
  aquParameterForConvert.add("scale=320:240");
  aquParameterForConvert.add("-ffourcc");
  aquParameterForConvert.add("FLV1");
  aquParameterForConvert.add("-of");
  aquParameterForConvert.add("lavf");
  aquParameterForConvert.add("-lavfopts");
  aquParameterForConvert
    .add("i_certify_that_my_video_stream_does_not_use_b_frames");
  aquParameterForConvert.add("-ovc");
  aquParameterForConvert.add("lavc");
  aquParameterForConvert.add("-lavcopts");
  aquParameterForConvert.add("vcodec=flv:vbitrate=200");
  aquParameterForConvert.add("-srate");
  aquParameterForConvert.add("22050");
  aquParameterForConvert.add("-oac");
  aquParameterForConvert.add("lavc");
  aquParameterForConvert.add("-lavcopts");
  aquParameterForConvert.add("acodec=mp3:abitrate=56");
  aquParameterForConvert.add(aquInputPath);
  aquParameterForConvert.add("-o");
  aquParameterForConvert.add(outPath);

  // 构建修复命令
  parameterForFix.add(fixFLVToolsPath);
  parameterForFix.add(outPath);
  // 构建截图命令
  aquParameterForIntercept.add(aquInterceptPicToolsPath);
  aquParameterForIntercept.add("-i");
  aquParameterForIntercept.add(outPath);
  aquParameterForIntercept.add("-y");
  aquParameterForIntercept.add("-f");
  aquParameterForIntercept.add("image2");
  aquParameterForIntercept.add("-ss");
  aquParameterForIntercept.add("8");
  aquParameterForIntercept.add("-t");
  aquParameterForIntercept.add("0.001");
  aquParameterForIntercept.add("-s");
  aquParameterForIntercept.add("320x240");
  aquParameterForIntercept.add(interceptPicSavePath+ outFileNameNoExt
    +".jpg");
  //转换
  String tmp1 =Aqu.exec(aquParameterForConvert);
  //截图
  String tmp2 =Aqu.exec(aquParameterForIntercept);
  return re;
 }

 public static void main(String[] args) {
  getInstance().convert("h:\\QQ28095553\\古刹飞鹰.wmv","h:\\aquaqu(quana)gmail.com\\古刹飞鹰.flv");
 }
}

ConvertThread 类

package com.v246.convertFLV;

public class ConvertThread extends Thread{
 
 private String fromPath = null;
 private String toPath = null;
 @Override
 public void run(){
  Convert.getInstance().convert(fromPath,toPath);
 }
 public void setFromPath(String fromPath) {
  this.fromPath = fromPath;
 }
 public void setToPath(String toPath) {
  this.toPath = toPath;
 }
}

 

ConvertThreadProxy类:

package com.v246.convertFLV;

public class ConvertThreadProxy {
 public static void convert(String fromPath,String toPath) {
  ConvertThread ct = newConvertThread();
  ct.setFromPath(fromPath);
  ct.setToPath(toPath);
  ct.start();
 }
}

 

 

使用的时候只要通过ConvertThreadProxy类的静态方法将源视频绝对地址(包括文件名+括展名)和要生成的FLV文件的绝对地址(包括文件名+括展名)以字符串的方式传进去即可!因为用的是多线程,所以转换过程不会占用当前线程!

核心转换类是线程同步的,所以您不用担心并法问题,因为一次只能转换一个文件!

0 0
原创粉丝点击