JSP 做视频流媒体服务器在线播放AVI,MP4,WMV格式,急求!!!
小弟不才,第一次使用JSP做在线视频播放,需要播放格式有AVI,MP4,WMV等(服务器里已经存储了这几种格式了,后续还有人通过CS结构的项目上传更多的视频文件,CS结构的项目我没办法修改,为了达到能网页播放这些视频只是假设了TOMCAT)
问题: 使用 MEDIA PLAYER 应该是可以播放WMV的,可以边缓冲边播放,但是AVI的格式不行。
思路:以为是解码器的问题和AVI不支持流媒体。也使用过百度影音等工具播放,但是连WMV 的格式也需要全部下载完成后才可以播了。
提问: 网上很多AVI等格式 百度影音和快播等 都可以边缓冲边播放,他们是需要假设 流媒体服务器?
我现在的环境就是TOMCAT 将路径映射到 视频文件夹下,然后JSP页面调用视频URL。
请各位大神帮忙分析以及提供一下思路。 在线万分感谢! 视频流?JSP? avi 视频 tomcat 流媒体服务器
[解决办法]
使用流媒体。使用ffmpeg转码。
播放器用这个,我做过:http://flowplayer.org/tools/demos/
[解决办法]
不是播放器问题!你要把你所有的后台视频格式转换成flv格式的。现在基本上大型在线视频网站都是flv格式的。我上面说了用ffmpeg可以转成flv格式的。api或者exe都可以做到。你自己研究下,转成flv之后基本上用一个flash播放器都可以变下载变放,而且随意拖动。
我直线写的一个Converter,你可以参考下
package com.cemso.util;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class VideoConverter {
private final static String BASE_PATH = FileOperationTool.DEFAULT_VIDEO_DES_PATH;
private final static String FLV_PATH = FileOperationTool.DEFAULT_FLV_PATH;
private static String PATH = "";
private static String FILE_NAME = "";
private static final Log log = LogFactory.getLog(com.cemso.util.VideoConverter.class);
public static boolean convert(String fileName) {
PATH = BASE_PATH + fileName;
FILE_NAME = fileName;
if(log.isDebugEnabled()){
log.debug("start to convert video to flv format...");
log.debug("the file name is : " + FILE_NAME);
log.debug("the file path is : " + PATH);
}
if (!checkfile(PATH)) {
return false;
} else {
if (process()) {
if(log.isDebugEnabled()){
log.debug("process() ok");
}
return true;
} else {
return false;
}
}
}
private static boolean process() {
int type = checkContentType();
boolean status = false;
if (type == 0) {
if(log.isDebugEnabled()){
log.debug("Start to convert to flv file");
}
status = processFLV(PATH);// 直接将文件转为flv文件
} else if (type == 1) {
String avifilepath = processAVI(type);
if (avifilepath == null){
return false;// avi文件没有得到
}
status = processFLV(avifilepath);// 将avi转为flv
} else if(type == 9){
if(log.isDebugEnabled()){
log.debug("this file is no need to convert.");
}
return false;
}
return status;
}
private static int checkContentType() {
String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.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)转换为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;
}
// check file
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend = new ArrayList<String>();
commend.add(FLV_PATH + "/mencoder");
commend.add(PATH);
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(FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".avi");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".avi";
} 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(PATH)) {
return false;
}
List<String> commend = new ArrayList<String>();
commend.add(FLV_PATH + "ffmpeg");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("600x500");
commend.add(FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".flv");
try {
Runtime runtime = Runtime.getRuntime();
String cmd = "";
String cut = FLV_PATH + "ffmpeg.exe -i "
+ oldfilepath
+ " -y -f image2 -ss 8 -t 0.001 -s 600x500 "
+ FLV_PATH
+ FILE_NAME.substring(0,FILE_NAME.lastIndexOf("."))
+ ".jpg";
String cutCmd = cmd + cut;
runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process process = builder.start();
int i = doWaitFor(process);
if(i == 0){
if(log.isDebugEnabled()){
log.debug("ffmpeg has finished.");
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static int doWaitFor(Process p) {
int exitValue = -1; // returned to caller when p is finished
try {
InputStream in = p.getInputStream();
InputStream err = p.getErrorStream();
boolean finished = false; // Set to true when p is finished
while(!finished){
try {
while( in.available() > 0) {
// Print the output of our system call
Character c = new Character( (char) in.read());
if(log.isDebugEnabled()){
log.debug( c);
}
}
while( err.available() > 0) {
// Print the output of our system call
Character c = new Character( (char) err.read());
if(log.isDebugEnabled()){
log.debug( c);
}
}
// Ask the process for its exitValue. If the process
// is not finished, an IllegalThreadStateException
// is thrown. If it is finished, we fall through and
// the variable finished is set to true.
exitValue = p.exitValue();
finished = true;
}
catch (IllegalThreadStateException e) {
Thread.currentThread();
// Process is not finished yet;
// Sleep a little to save on CPU cycles
Thread.sleep(500);
}
}
}
catch (Exception e) {
// unexpected exception! print it out for debugging...
if(log.isErrorEnabled()){
log.error("doWaitFor(): unexpected exception - " +
e.getMessage());
}
if(log.isErrorEnabled()){
log.error(e.getMessage());
}
}
// return completion status to caller
return exitValue;
}
}