(转)JSP上传视频后自动转成flv的核心JAVA方法
作者:古刹飞鹰
BLOG:www.v246.com
QQ:28095553
Email:aquaqu(圈.a)gmail.com
本文属古刹飞鹰原创,转载请注明出处!
以下是源码:
【全文】
作者:古刹飞鹰
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;
//作者:古刹飞鹰 blog:www.v246.com懒汉模式
public class Convert {
private static Convert instance = null;
//作者:古刹飞鹰 blog:www.v246.com flv截图工具的绝对地址
private String aquInterceptPicToolsPath = "c:\\FLVTools\\ffmpeg.exe";
//作者:古刹飞鹰 blog:www.v246.com flv修复工具的绝对地址
private String fixFLVToolsPath = "c:\\FLVTools\\flvmdi.exe";
//作者:古刹飞鹰 blog:www.v246.com flv转换工具的绝对地址
private String aquCoverFLVToolsPath = "c:\\FLVTools\\mencoder\\mencoder.exe";
private Convert() {
//作者:古刹飞鹰 blog:www.v246.cominit();
}
public static Convert getInstance() {
if (instance == null) {
instance = new Convert();
}
return instance;
}
public void reset(){
//作者:古刹飞鹰 blog:www.v246.cominit();
}
private void init() {
/*aquInterceptPicToolsPath = Aqu.getXMLValue("convertFLV.xml",
"//作者:古刹飞鹰 blog:www.v246.com古刹飞鹰/FLV转换工具全路径")[0];
fixFLVToolsPath = Aqu
.getXMLValue("convertFLV.xml", "//作者:古刹飞鹰 blog:www.v246.com古刹飞鹰/FLV截图工具全路径")[0];
aquCoverFLVToolsPath = Aqu.getXMLValue("convertFLV.xml",
"//作者:古刹飞鹰 blog:www.v246.com古刹飞鹰/FLV修复工具全路径")[0];
aquInterceptPicToolsPath = aquInterceptPicToolsPath.replace("/", "\\");
fixFLVToolsPath = fixFLVToolsPath.replace("/", "\\");
aquCoverFLVToolsPath = aquCoverFLVToolsPath.replace("/", "\\");
System.out.println(aquInterceptPicToolsPath);
System.out.println(fixFLVToolsPath);
System.out.println(aquCoverFLVToolsPath);*/
}
public synchronized boolean convert(String aquInputPath, String outPath) {
boolean re = false;
//作者:古刹飞鹰 blog:www.v246.com 通一路径定位符
aquInputPath = aquInputPath.replace("/", "\\");
//作者:古刹飞鹰 blog:www.v246.com 通一路径定位符
outPath = outPath.replace("/", "\\");
//作者:古刹飞鹰 blog:www.v246.com 分析不包含文件名的输出路径
String interceptPicSavePath = outPath.substring(0, outPath
.lastIndexOf("\\") + 1);
//作者:古刹飞鹰 blog:www.v246.com 分析输出的文件名
String outFileName = outPath.substring(outPath.lastIndexOf("\\") + 1,
outPath.length());
//作者:古刹飞鹰 blog:www.v246.com 继续分析得出不包含扩展名的文件名
String outFileNameNoExt = outFileName.substring(0, outFileName
.lastIndexOf("."));
//作者:古刹飞鹰 blog:www.v246.com 工作目录,随便设置
//作者:古刹飞鹰 blog:www.v246.com String workdirectory = "c:\\windows\\temp";
//作者:古刹飞鹰 blog:www.v246.com 修复命令
List<String> parameterForFix = new ArrayList<String>(100);
//作者:古刹飞鹰 blog:www.v246.com 转换命令
List<String> aquParameterForConvert = new ArrayList<String>(100);
//作者:古刹飞鹰 blog:www.v246.com 截图命令
List<String> aquParameterForIntercept = new ArrayList<String>(100);
//作者:古刹飞鹰 blog:www.v246.com 构建转换命令
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);
//作者:古刹飞鹰 blog:www.v246.com 构建修复命令
parameterForFix.add(fixFLVToolsPath);
parameterForFix.add(outPath);
//作者:古刹飞鹰 blog:www.v246.com 构建截图命令
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");
//作者:古刹飞鹰 blog:www.v246.com转换
String tmp1 = Aqu.exec(aquParameterForConvert);
//作者:古刹飞鹰 blog:www.v246.com截图
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 = new ConvertThread();
ct.setFromPath(fromPath);
ct.setToPath(toPath);
ct.start();
}
}
使用的时候只要通过ConvertThreadProxy 类的静态方法将源视频绝对地址(包括文件名+括展名)和要生成的FLV文件的绝对地址(包括文件名+括展名)以字符串的方式传进去即可!因为用的是多线程,所以转换过程不会占用当前线程!
核心转换类是线程同步的,所以您不用担心并法问题,因为一次只能转换一个文件!
注:安装和使用ffmpeg转换视频为flv文件(windows和linux)
用java程序调用ffmpeg执行视频文件格式转换flv
来源:http://www.v246.com/html/182.html