前一段時間朱哥一直在搞視頻轉(zhuǎn)換這個東東,我也一直很有興趣,就嘗試了一下。
首先是文件上傳功能的完成,做得很粗糙,沒有驗(yàn)證上傳文件是否為視頻文件。利用前一段時間看視頻學(xué)來的部分代碼很輕松搞定。
接下來,就是視頻轉(zhuǎn)換了,我的ffmpeg和mencoder放在d:\ffmpeg目錄中,代碼如下

Code
package com.lc.test;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class CodeConven {
private static String OldPath;
private static String NewPath;
public static boolean process() {
int type = checkContentType();
boolean status = false;
if (type == 0) {
status = processFLV(OldPath);// 直接將文件轉(zhuǎn)為flv文件
} else if (type == 1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// avi文件沒有得到
status = processFLV(avifilepath);// 將avi轉(zhuǎn)為flv
}
return status;
}
private static int checkContentType() {
String type = OldPath.substring(OldPath.lastIndexOf(".") + 1,
OldPath.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)轉(zhuǎn)換為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;
}
private static boolean checkfile(String OldPath) {
File file = new File(OldPath);
if (!file.isFile()) {
return false;
}
return true;
}
// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend = new java.util.ArrayList<String>();
commend.add("d:\\ffmpeg\\mencoder");
commend.add(OldPath);
commend.add("-oac");
commend.add("lavc");
commend.add("-lavcopts");
commend.add("acodec=mp3:abitrate=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(NewPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return NewPath;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String oldfilepath) {
if (!checkfile(OldPath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add("d:\\ffmpeg\\ffmpeg");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-y");
commend.add("-ab");
commend.add("32");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("800000");
commend.add("-s");
commend.add("640*480");
commend.add(NewPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String getOldPath() {
return OldPath;
}
public void setOldPath(String oldPath) {
OldPath = oldPath;
}
public String getNewPath() {
return NewPath;
}
public void setNewPath(String newPath) {
NewPath = newPath;
}
}
寫個測試類試了一下,挺好使的,轉(zhuǎn)換的速度也還行。
接下來就是要在上傳類上略作修改,把轉(zhuǎn)換的步驟加進(jìn)去就ok了。
以下是添加視頻轉(zhuǎn)換功能后的上傳類代碼:

Code
package com.lc.test;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 2431664260164700200L;
private ServletContext sc;
private String savePath;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
System.out.println("表單的參數(shù)名稱:" + item.getFieldName()
+ ",對應(yīng)的參數(shù)值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上傳文件的大?。?/span>" + item.getSize());
System.out.println("上傳文件的類型:" + item.getContentType());
System.out.println("上傳文件的名稱:" + item.getName());
File tempFile = new File(item.getName());
File file = new File(sc.getRealPath("/") + savePath,
tempFile.getName());
item.write(file);
// System.out.println(sc.getRealPath("/") + savePath);
// request.setAttribute("upload.message", "上傳文件成功!");
CodeConven cc= new CodeConven();
String oldfilename=item.getName().substring(item.getName().lastIndexOf("\\")+1,item.getName().length()).toLowerCase();
String newfilename=oldfilename.substring(0,oldfilename.lastIndexOf("."))+".flv";
String oldPath=sc.getRealPath("/") + savePath+"\\"+oldfilename;
String newPath=sc.getRealPath("/") +"output\\"+newfilename;
// System.out.println("文件名:"+item.getName());
// System.out.println("原路徑:"+oldPath);
// System.out.println("轉(zhuǎn)換后路徑:"+newPath);
cc.setOldPath(oldPath);
cc.setNewPath(newPath);
if(cc.process()){
System.out.println("conven ok");
}
} else {
request.setAttribute("upload.message", "沒有選擇上傳文件!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上傳文件不成功!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
}
public void init(ServletConfig config) {
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
}
ok了,寫這個主要是為了實(shí)現(xiàn),具體有其他不到位的地方還請大家指正啊。
源碼下載
文章來源:
http://www.cnblogs.com/xiaoao808/archive/2008/07/31/1257414.html
posted on 2008-07-31 16:41
破名超難起 閱讀(144)
評論(0) 編輯 收藏