Java使用ffmpeg和mencoder实现视频转码
本文为大家分享了Java使用ffmpeg和mencoder实现视频转码的具体代码,供大家参考,具体内容如下
准备:
需要下载ffmpeg和mencoder,百度一搜就有了,请自行下载。
不墨迹,上代码:
1)首先需要定义几个量:Contants.java
publicclassContants{
publicstaticfinalStringffmpegpath="D:\\DevTools\\ffmpeg\\bin\\ffmpeg.exe";//ffmpeg的安装位置
publicstaticfinalStringmencoderpath="D:\\DevTools\\mencoder\\";//mencoder的目录
publicstaticfinalStringvideofolder="D:\\tools\\video\\";//需要被转换格式的视频目录
publicstaticfinalStringtargetfolder="D:\\tools\\target\\";//转换后视频的目录
publicstaticfinalStringvideoRealPath="D:\\tools\\target\\";//需要被截图的视频目录;
publicstaticfinalStringimageRealPath="D:\\tools\\imgs\\";//截图的存放目录
}
2)其次,就是Utils类了,具体里面有注释:ConverVideoUtils.java
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Date;
importjava.util.List;
importcom.wdy.common.Contants;
publicclassConverVideoUtils{
privateDatedt;
privatelongbegintime;
privateStringsourceVideoPath;//源视频路径
privateStringfilerealname;//文件名不包括扩展名
privateStringfilename;//包括扩展名
privateStringvideofolder=Contants.videofolder;//别的格式视频的目录
privateStringtargetfolder=Contants.targetfolder;//flv视频的目录
privateStringffmpegpath=Contants.ffmpegpath;//ffmpeg.exe的目录
privateStringmencoderpath=Contants.mencoderpath;//mencoder的目录
privateStringimageRealPath=Contants.imageRealPath;//截图的存放目录
publicConverVideoUtils(){
}
publicConverVideoUtils(Stringpath){
sourceVideoPath=path;
}
publicStringgetPATH(){
returnsourceVideoPath;
}
publicvoidsetPATH(Stringpath){
sourceVideoPath=path;
}
/**
*转换视频格式
*@paramStringtargetExtension目标视频扩展名.xxx
*@paramisDelSourseFile转换完成后是否删除源文件
*@return
*/
publicbooleanbeginConver(StringtargetExtension,booleanisDelSourseFile){
Filefi=newFile(sourceVideoPath);
filename=fi.getName();
filerealname=filename.substring(0,filename.lastIndexOf(".")).toLowerCase();
System.out.println("----接收到文件("+sourceVideoPath+")需要转换--------------------------");
if(!checkfile(sourceVideoPath)){
System.out.println(sourceVideoPath+"文件不存在"+"");
returnfalse;
}
dt=newDate();
begintime=dt.getTime();
System.out.println("----开始转文件("+sourceVideoPath+")--------------------------");
if(process(targetExtension,isDelSourseFile)){
Datedt2=newDate();
System.out.println("转换成功");
longendtime=dt2.getTime();
longtimecha=(endtime-begintime);
Stringtotaltime=sumTime(timecha);
System.out.println("转换视频格式共用了:"+totaltime+"");
if(processImg(sourceVideoPath)){
System.out.println("截图成功了!");
}else{
System.out.println("截图失败了!");
}
if(isDelSourseFile){
deleteFile(sourceVideoPath);
}
sourceVideoPath=null;
returntrue;
}else{
sourceVideoPath=null;
returnfalse;
}
}
/**
*对视频进行截图
*@paramsourceVideoPath需要被截图的视频路径(包含文件名和扩展名)
*@return
*/
publicbooleanprocessImg(StringsourceVideoPath){
if(!checkfile(sourceVideoPath)){
System.out.println(sourceVideoPath+"isnotfile");
returnfalse;
}
Filefi=newFile(sourceVideoPath);
filename=fi.getName();
filerealname=filename.substring(0,filename.lastIndexOf(".")).toLowerCase();
Listcommend=newjava.util.ArrayList();
//第一帧:00:00:01
//timeffmpeg-ss00:00:01-itest1.flv-fimage2-ytest1.jpg
commend.add(ffmpegpath);
//commend.add("-i");
//commend.add(videoRealPath+filerealname+".flv");
//commend.add("-y");
//commend.add("-f");
//commend.add("image2");
//commend.add("-ss");
//commend.add("38");
//commend.add("-t");
//commend.add("0.001");
//commend.add("-s");
//commend.add("320x240");
commend.add("-ss");
commend.add("00:00:01");
commend.add("-i");
commend.add(sourceVideoPath);
commend.add("-f");
commend.add("image2");
commend.add("-y");
commend.add(imageRealPath+filerealname+".jpg");
try{
ProcessBuilderbuilder=newProcessBuilder();
builder.command(commend);
builder.start();
returntrue;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
/**
*实际转换视频格式的方法
*@paramtargetExtension目标视频扩展名
*@paramisDelSourseFile转换完成后是否删除源文件
*@return
*/
privatebooleanprocess(StringtargetExtension,booleanisDelSourseFile){
inttype=checkContentType();
booleanstatus=false;
if(type==0){
//如果type为0用ffmpeg直接转换
status=processVideoFormat(sourceVideoPath,targetExtension,isDelSourseFile);
}elseif(type==1){
//如果type为1,将其他文件先转换为avi,然后在用ffmpeg转换为指定格式
Stringavifilepath=processAVI(type);
if(avifilepath==null){
//avi文件没有得到
returnfalse;
}else{
System.out.println("开始转换:");
status=processVideoFormat(avifilepath,targetExtension,isDelSourseFile);
}
}
returnstatus;
}
/**
*检查文件类型
*@return
*/
privateintcheckContentType(){
Stringtype=sourceVideoPath.substring(sourceVideoPath.lastIndexOf(".")+1,sourceVideoPath.length()).toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if(type.equals("avi")){
return0;
}elseif(type.equals("mpg")){
return0;
}elseif(type.equals("wmv")){
return0;
}elseif(type.equals("3gp")){
return0;
}elseif(type.equals("mov")){
return0;
}elseif(type.equals("mp4")){
return0;
}elseif(type.equals("asf")){
return0;
}elseif(type.equals("asx")){
return0;
}elseif(type.equals("flv")){
return0;
}
//对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
//可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
elseif(type.equals("wmv9")){
return1;
}elseif(type.equals("rm")){
return1;
}elseif(type.equals("rmvb")){
return1;
}
return9;
}
/**
*检查文件是否存在
*@parampath
*@return
*/
privatebooleancheckfile(Stringpath){
Filefile=newFile(path);
if(!file.isFile()){
returnfalse;
}else{
returntrue;
}
}
/**
*对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
*@paramtype
*@return
*/
privateStringprocessAVI(inttype){
Listcommend=newjava.util.ArrayList();
commend.add(mencoderpath);
commend.add(sourceVideoPath);
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(videofolder+filerealname+".avi");
//命令类型:mencoder1.rmvb-oacmp3lame-lameoptspreset=64-ovcxvid
//-xvidencoptsbitrate=600-ofavi-ormvb.avi
try{
ProcessBuilderbuilder=newProcessBuilder();
builder.command(commend);
Processp=builder.start();
doWaitFor(p);
returnvideofolder+filerealname+".avi";
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
/**
*转换为指定格式
*ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
*@paramoldfilepath
*@paramtargetExtension目标格式扩展名.xxx
*@paramisDelSourseFile转换完成后是否删除源文件
*@return
*/
privatebooleanprocessVideoFormat(Stringoldfilepath,StringtargetExtension,booleanisDelSourceFile){
if(!checkfile(sourceVideoPath)){
System.out.println(oldfilepath+"isnotfile");
returnfalse;
}
//ffmpeg-iFILE_NAME.flv-ar22050NEW_FILE_NAME.mp4
Listcommend=newjava.util.ArrayList<>();
commend.add(ffmpegpath);
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ar");
commend.add("22050");
commend.add(targetfolder+filerealname+targetExtension);
try{
ProcessBuilderbuilder=newProcessBuilder();
Stringcmd=commend.toString();
builder.command(commend);
Processp=builder.start();
doWaitFor(p);
p.destroy();
//转换完成后删除源文件
//if(isDelSourceFile){
//deleteFile(sourceVideoPath);
//}
returntrue;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
/**
*ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
*@paramoldfilepath
*@return
*/
privatebooleanprocessFLV(Stringoldfilepath){
if(!checkfile(sourceVideoPath)){
System.out.println(oldfilepath+"isnotfile");
returnfalse;
}
Listcommend=newjava.util.ArrayList<>();
commend.add(ffmpegpath);
commend.add("-i");
commend.add(oldfilepath);
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(targetfolder+filerealname+".flv");
try{
ProcessBuilderbuilder=newProcessBuilder();
Stringcmd=commend.toString();
builder.command(commend);
Processp=builder.start();
doWaitFor(p);
p.destroy();
deleteFile(oldfilepath);
returntrue;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
publicintdoWaitFor(Processp){
InputStreamin=null;
InputStreamerr=null;
intexitValue=-1;//returnedtocallerwhenpisfinished
try{
System.out.println("comeing");
in=p.getInputStream();
err=p.getErrorStream();
booleanfinished=false;//Settotruewhenpisfinished
while(!finished){
try{
while(in.available()>0){
Characterc=newCharacter((char)in.read());
System.out.print(c);
}
while(err.available()>0){
Characterc=newCharacter((char)err.read());
System.out.print(c);
}
exitValue=p.exitValue();
finished=true;
}catch(IllegalThreadStateExceptione){
Thread.currentThread().sleep(500);
}
}
}catch(Exceptione){
System.err.println("doWaitFor();:unexpectedexception-"+e.getMessage());
}finally{
try{
if(in!=null){
in.close();
}
}catch(IOExceptione){
System.out.println(e.getMessage());
}
if(err!=null){
try{
err.close();
}catch(IOExceptione){
System.out.println(e.getMessage());
}
}
}
returnexitValue;
}
3)最后,编写测试类:ConverVideoTest.java
publicclassConverVideoTest{
publicvoidrun(){
try{
//转换并截图
StringfilePath="C:\\Users\\wangdy\\Desktop\\pics\\05.尚硅谷_SVN_启动服务器.wmv";
ConverVideoUtilscv=newConverVideoUtils(filePath);
StringtargetExtension=".mp4";
booleanisDelSourseFile=true;
booleanbeginConver=cv.beginConver(targetExtension,isDelSourseFile);
System.out.println(beginConver);
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
ConverVideoTestc=newConverVideoTest();
c.run();
}
}
这样就完成了整个视频格式的转换,在定义的目标视频文件夹和截图存放文件夹就可以看到转换后的视频和视频第一帧的截图了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。