视频网站,将上传视频转换为flv。在页面上播放

哈哈 我的博客标题之所以写的这么白话。主要是为了以后方便自己查找资源。

前几天接手到一个上传视频并播放的需求。 可以简单的理解为小型的视频网站。 并具有基本的增删改查功能。

最初实现:
保持原有的上传视频格式,并通过不同的软件,RealPlay Windows media play 来在jsp上实现播放。
后来于哥说.这种需要用户安装软件的形式 很不友好。需要将上传视频的格式统一转换成flv,这样在网页上可以直接播放。


从昨天下午开始着手完成这部分内容。

主要思想是将上传后的视频格式。
通过java 调用服务器端的两个可执行性文件:1.ffmpeg 2. mencoder。
java的ProcessBuilder可以直接调用cmd命令来执行程序自动转化视频。

public class ConvertVideo{
public void convertFlv(String ffmpegPath,String path1,String path2){
List<String> commend = new java.util.ArrayList<String>();
//String flvPath = "e:\\a.flv";
commend.add(ffmpegPath);
commend.add("-i");
commend.add(path1);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("500");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("800x600");
commend.add(path2);
int flag = doProcess(commend);
if(flag!=-1){
deleteFile(path1);
}
}
public int doProcess(List<String> list){
int exitValue = -1;
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command(list);
Process p = builder.start();
exitValue = doWaitFor(p);
if(exitValue!=-1){
System.out.println(">>>>>>>>> convert finished >>>>>>>>>>>>>");
}
return exitValue;
}catch(Exception e){
e.printStackTrace();
}
return exitValue;
}
public int doWaitFor(Process p){
InputStream in = null;
InputStream err = null;
int exitValue = -1; // returned to caller when p is finished
try {
System.out.println("comeing");
in = p.getInputStream();
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
BufferedInputStream bi = new BufferedInputStream(in);
Character c = new Character((char) bi.read());
System.out.print(c);
}
while (err.available() > 0) {
// Print the output of our system call
BufferedInputStream bi = new BufferedInputStream(err);
Character c = new Character((char) bi.read());
System.out.print(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) {
// Process is not finished yet;
// Sleep a little to save on CPU cycles
Thread.currentThread().sleep(500);
}
}
} catch (Exception e) {
// unexpected exception! print it out for debugging...
System.err.println("doWaitFor();: unexpected exception - "
+ e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}

} catch (IOException e) {
System.out.println(e.getMessage());
}
if (err != null) {
try {
err.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
// return completion status to caller
return exitValue;
}
public void deleteFile(String str) {
File file = new File(str);
file.delete();
}

}

本来是想支持rmvb格式视频的上传了,但是rmvb不能转换为flv,需要先转化成avi,再由avi转换成flv。但是这样一来。转换的时间太长了。还不如索性就直接提示用户上传指定的视频文件格式了。
这样一来我也省事了,也不用去操心rmvb的转换了。

---------------------------------------------------------------------------------
有了上传后转换好保存的视频。 那我们下一步就是在网上显示了。
上午找了一会。发现如果在网页上实现flv的播放。那么服务器端需要 有swf。
又找了一个别人已经写好的swf。扔在服务端上。就可以播放flv了。
这样在修修改改一些边边角角。 这个需求就算是完成了。

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="380" width="500">
<param name="movie"
value="<%=path1%>vcastr22.swf?vcastr_file=<%=path1%><%=info[0]%>.flv">
<param name="quality" value="high">
<param name="allowFullScreen" value="true" />
<embed
src="<%=path1%>vcastr22.swf?vcastr_file=<%=path1%><%=info[0]%>.flv"
quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="800" height="600">
</embed>
</object>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值