ffmpeg+mencoder 视频转码

            因项目需要实现上传视频和视频在线观看的功能,html5中的video标签可以直接播放视频,不需要任何的插件,但是所支持的视频格式只有三种。因此上传后需要在后台更改视频格式。这里用的是ffmpeg+mencoder进行转码的。

----------------------------------------------------------------------------------------------------------------------------------------------------

ffmpeg视频采集功能非常强大,不仅可以采集视频采集卡或USB摄像头的图像,还可以进行屏幕录制,同时还支持以RTP方式将视频流传送给支持RTSP的流媒体服务器,支持直播应用。

1.能支持的格式

ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

2.不能支持的格式

对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.

实例是将上传视频转码为flv格式,该格式ffmpeg支持,所以我们实例中需要ffmpeg视频处理工具.

这里需要下载的工具有:ffmpeg.exe,mencoder.exe,Pncrt.dll,pthreadGC2.dll,drv43260.dll,本例中存放在bin目录中



package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.youlian.dao.VideoInfoDaoImpl;

public class FileUploadServlet  extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private static String path;
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		DiskFileItemFactory factory =new DiskFileItemFactory(); 
	    //将文件保存在硬盘中,同时将tomcat下的upload文件夹与该地址下的temp文件映射
		String basePath ="d:\\temp";
	    //创建上传文件的对象; 
	    ServletFileUpload upload=new ServletFileUpload(factory); 
	  //得到所有文件
	    try {
			List<FileItem> items = upload.parseRequest(request);
			Iterator it = items.iterator();
			while( it.hasNext() ){
				FileItem item=(FileItem)it.next();
		         String fileName=item.getName();//获得文件名
		         if(fileName!=null){
		             File savedFile=new File(basePath,fileName);
		             System.out.println(savedFile.getAbsolutePath());
		             item.write(savedFile);
		             
		             path = basePath+"\\"+fileName;//转换前文件路径
		   
		           //将视频转码		            
		             if (!checkFile(path)) {//如果文件不存在退出
		     			System.out.println("Error");
		     			System.exit(1);
		     		}
		             if (process()) {
		     			System.out.println("OK");
		     		}
		             
		         }
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}       
	} 
	
	private boolean process() {
		int type = checkContentType();
		boolean status = false;
		if (type == 0){  //ffmpeg转码
			status = processMp4(path);
		}else if (type == 1) {//mencoder转码
			String aviFilePath = processAVI(type);
			if (aviFilePath == null)
				return false;
			status = processMp4(aviFilePath);
		}
		return status;
	}

	//判断文件类型
	public int checkContentType(){ 
			String type=path.substring(path.lastIndexOf(".")+1,path.length()).toLowerCase(); 

			if(type.equals("avi")||type.equals("mpg")||type.equals("wmv")||type.equals("3gp")
					||type.equals("mov")||type.equals("mp4")||type.equals("asf")||type.equals("asx")||type.equals("flv")){
							return 0; 
			}else if(type.equals("wmv9")||type.equals("rm")||type.equals("rmvb")) {
						return 1; 
			}else {
				return -1; 
			}
	}	
	
	//判断文件是否存在
	public boolean checkFile(String path) {
		File file = new File(path);
		if (!file.isFile())
			return false;
		return true;
	}

	//转成avi格式
	private String processAVI(int type){ 
		String mencoderPath="d:\\temp\\bin\\mencoder"; 
		String aviFilePath="d:\\temp\\newtemp.avi"; 
		String commend="-oac mp3lame -lameopts preset=64 -ovc xvid -xvidencopts bitrate=600 -of avi -o"; 
		String cmd="cmd /c start "+mencoderPath+" "+path+" "+commend+" "+aviFilePath;

	  try{ 
		  		Process pr=Runtime.getRuntime().exec(cmd); 
				   try{ 
				    BufferedReader buf1=new BufferedReader(new InputStreamReader(pr.getInputStream())); 
				    while(buf1.readLine()!=null); 
				   }catch(Exception e){ 
				    e.printStackTrace(); 
				   } 
				   return aviFilePath; 
		  }catch(Exception e){ 
			  e.printStackTrace(); 
			  return null; 
		  } 
	}
	
	private boolean processMp4(String oldFilePath) { 
			String ffmpegPath="d:\\temp\\bin\\ffmpeg"; 
			String Mp4Path="d:\\temp\\lkq.mp4"; 
			String commend="-y -ab 56 -ar 11025 -b 400000 -s 320*240"; 
			String cmd="cmd /c start "+ffmpegPath+" -i "+oldFilePath+" "+commend+" "+Mp4Path;

		  if(!checkFile(oldFilePath)){ 
			  	System.out.println(oldFilePath+" is not a file."); 
			  	return false; 
		  } 
		  		try{ 
		  				Process pro=Runtime.getRuntime().exec(cmd); 
		  				try{ 
		  				BufferedReader buf=new BufferedReader(new InputStreamReader(pro.getInputStream())); 
		  				while(buf.readLine()!=null); 
		  				}catch(Exception e){ 
		  					e.printStackTrace(); 
		  				} 
		  				return true; 
		  		}catch(Exception e){ 
		  				e.printStackTrace(); 
		  				return false; 
		  		} 
		}	
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值