-
根据项目进展需要,我以前做的视频录制的程序(asp.net1.1+Ajaxpro)要部署到内部服务器上,并且程序转化为asp.net2.0+atlas,并架在两个机器上,WEB服务器+FMS。那面临的问题是视频缩略放在哪台服务器上,当然是放在WEB上最好了。
我决定在FMS上开webservice来解决两太机器协作问题:
webservice代码
1using System;
2using System.Web;
3using System.Collections;
4using System.Web.Services;
5using System.Web.Services.Protocols;
6using System.Data;
7using System.Data.SqlClient;
8using System.IO;
9using System.Text;
10using System.Configuration;
11using System.Drawing;
12
13
14/**//// <summary>
15/// 生成图片 下载 视频上传 生成图片 格式转换
16/// </summary>
17[WebService(Namespace = "http://tempuri.org/")]
18[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
19public class WsVideo : System.Web.Services.WebService {
20
21 private string fname;
22 private string fpath;
23
24 public WsVideo()
25 {
26 //如果使用设计的组件,请取消注释以下行
27 //InitializeComponent();
28 }
29 [WebMethod]
30 public string nn()
31 {
32 return fname;
33 }
34 /**//// <summary>
35 /// 用ffmpeg.exe生成缩略图
36 /// </summary>
37 /// <param name="flvName"></param>
38 /// <returns></returns>
39 [WebMethod]
40 public string flvImage(string na, string pa)
41 {
42 fname = na;
43 fpath = pa;
44 string flvName = fpath + "\\" + fname;
45 flvName = @"C:\Program Files\Macromedia\Flash Media Server 2\applications\record\streams\_definst_\"
46 + flvName + ".flv";
47 string ffmpeg = @"ffmpeg\ffmpeg.exe";
48 ffmpeg = Server.MapPath(ffmpeg);
49 if (!File.Exists(ffmpeg))
50 {
51 return "ffmpeg not found!";
52 }
53 if (!File.Exists(flvName))
54 {
55 return "flvimg not found";
56 }
57
58 /**/////改变后缀名 和相对路径 存入数据库的路径名
59 //string flvImg = Path.ChangeExtension(flvName, ".jpg");
60 //flvImg = flvImg.Substring(flvImg.LastIndexOf('\\') + 1);
61
62 //图片绝对路径
63 string PathFlvImg = Server.MapPath(@"flvImg\" + fname + ".jpg");
64
65 //截图的尺寸大小
66 string FlvImgSize = "120*90";
67
68 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
69 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
70
71 //ffmpeg.exe文件需要的参数
72 startInfo.Arguments = " -i \"" + flvName + "\" -y -f image2 -t 1 -s " + FlvImgSize + " \"" + PathFlvImg + "\"";
73
74 try
75 {
76 System.Diagnostics.Process.Start(startInfo);
77 }
78 catch
79 {
80 return "false";
81 }
82 return "true";
83 }
84 /**//// <summary>
85 /// 查看缩略图是否已经生成
86 /// </summary>
87 /// <param name="flvName"></param>
88 /// <returns></returns>
89 [WebMethod]
90 public bool ExistsImage(string na)
91 {
92 if (File.Exists(Server.MapPath(@"flvImg\" + na + ".jpg")))
93 {
94 return true;
95 }
96 return false;
97 }
98 /**//// <summary>
99 /// 图片二进制数组
100 /// </summary>
101 /// <param name="flvName"></param>
102 /// <returns></returns>
103 [WebMethod]
104 public byte[] ImageByte(string na)
105 {
106 string file = Server.MapPath(@"flvImg\" + na + ".jpg");
107 if (File.Exists(file))
108 {
109 try
110 {
111 FileStream s = File.OpenRead(file);
112 return ConvertStreamToByteBuffer(s);
113 }
114 catch
115 {
116 return new byte[0];
117 }
118 }
119 else
120 {
121 return new byte[0];
122 }
123 }
124 /**//// <summary>
125 /// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
126 /// </summary>
127 /// <param name="theStream"></param>
128 /// <returns></returns>
129 public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
130 {
131 int b1;
132 System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
133 while ((b1 = theStream.ReadByte()) != -1)
134 {
135 tempStream.WriteByte(((byte)b1));
136 }
137 return tempStream.ToArray();
138
1using System;
2using System.Web;
3using System.Collections;
4using System.Web.Services;
5using System.Web.Services.Protocols;
6using System.Data;
7using System.Data.SqlClient;
8using System.IO;
9using System.Text;
10using System.Configuration;
11using System.Drawing;
12
13
14/**//// <summary>
15/// 生成图片 下载 视频上传 生成图片 格式转换
16/// </summary>
17[WebService(Namespace = "http://tempuri.org/")]
18[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
19public class WsVideo : System.Web.Services.WebService {
20
21 private string fname;
22 private string fpath;
23
24 public WsVideo()
25 {
26 //如果使用设计的组件,请取消注释以下行
27 //InitializeComponent();
28 }
29 [WebMethod]
30 public string nn()
31 {
32 return fname;
33 }
34 /**//// <summary>
35 /// 用ffmpeg.exe生成缩略图
36 /// </summary>
37 /// <param name="flvName"></param>
38 /// <returns></returns>
39 [WebMethod]
40 public string flvImage(string na, string pa)
41 {
42 fname = na;
43 fpath = pa;
44 string flvName = fpath + "\\" + fname;
45 flvName = @"C:\Program Files\Macromedia\Flash Media Server 2\applications\record\streams\_definst_\"
46 + flvName + ".flv";
47 string ffmpeg = @"ffmpeg\ffmpeg.exe";
48 ffmpeg = Server.MapPath(ffmpeg);
49 if (!File.Exists(ffmpeg))
50 {
51 return "ffmpeg not found!";
52 }
53 if (!File.Exists(flvName))
54 {
55 return "flvimg not found";
56 }
57
58 /**/////改变后缀名 和相对路径 存入数据库的路径名
59 //string flvImg = Path.ChangeExtension(flvName, ".jpg");
60 //flvImg = flvImg.Substring(flvImg.LastIndexOf('\\') + 1);
61
62 //图片绝对路径
63 string PathFlvImg = Server.MapPath(@"flvImg\" + fname + ".jpg");
64
65 //截图的尺寸大小
66 string FlvImgSize = "120*90";
67
68 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
69 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
70
71 //ffmpeg.exe文件需要的参数
72 startInfo.Arguments = " -i \"" + flvName + "\" -y -f image2 -t 1 -s " + FlvImgSize + " \"" + PathFlvImg + "\"";
73
74 try
75 {
76 System.Diagnostics.Process.Start(startInfo);
77 }
78 catch
79 {
80 return "false";
81 }
82 return "true";
83 }
84 /**//// <summary>
85 /// 查看缩略图是否已经生成
86 /// </summary>
87 /// <param name="flvName"></param>
88 /// <returns></returns>
89 [WebMethod]
90 public bool ExistsImage(string na)
91 {
92 if (File.Exists(Server.MapPath(@"flvImg\" + na + ".jpg")))
93 {
94 return true;
95 }
96 return false;
97 }
98 /**//// <summary>
99 /// 图片二进制数组
100 /// </summary>
101 /// <param name="flvName"></param>
102 /// <returns></returns>
103 [WebMethod]
104 public byte[] ImageByte(string na)
105 {
106 string file = Server.MapPath(@"flvImg\" + na + ".jpg");
107 if (File.Exists(file))
108 {
109 try
110 {
111 FileStream s = File.OpenRead(file);
112 return ConvertStreamToByteBuffer(s);
113 }
114 catch
115 {
116 return new byte[0];
117 }
118 }
119 else
120 {
121 return new byte[0];
122 }
123 }
124 /**//// <summary>
125 /// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
126 /// </summary>
127 /// <param name="theStream"></param>
128 /// <returns></returns>
129 public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
130 {
131 int b1;
132 System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
133 while ((b1 = theStream.ReadByte()) != -1)
134 {
135 tempStream.WriteByte(((byte)b1));
136 }
137 return tempStream.ToArray();
138
这里工具还是以前选择的ffmpeg.exe来生成缩略图 这里放在了项目根目录ffmpeg\文件夹下
继续处理web程序 是在录制成功后跳转的页面 用到了timer控件
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VideoInf.aspx.cs" Inherits="_Default" Async="true" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>第二步:填写视频相关信息</title>
7 <style type="text/css">
8 .table1
9{}{
10 BORDER-RIGHT: #62d9ff 1px solid;
11 BORDER-TOP: #62d9ff 1px solid;
12 FONT-SIZE: 14px;
13 BORDER-LEFT: #62d9ff 1px solid;
14 COLOR: #333366;
15 BORDER-BOTTOM: #62d9ff 1px solid;
16 border-top-width: 1px;
17 border-right-width: 1px;
18 border-bottom-width: 1px;
19 border-left-width: 1px;
20 TABLE-LAYOUT:fixed;
21}
22.tdbg1
23{}{
24font-weight: bold; font-size: 14px; color: #000066; background-color: #33ccff;
25}
26.input1
27{}{
28 BORDER-RIGHT: #000099 1px solid;
29 BORDER-TOP: #000099 1px solid;
30 FONT-SIZE: 14px;
31 BORDER-LEFT: #000099 1px solid;
32 WIDTH: 235px; COLOR: #330066;
33 BORDER-BOTTOM: #000099 1px solid;
34 BACKGROUND-COLOR: #fff8f0;
35 border-top-width: 1px;
36 border-right-width: 1px;
37 border-bottom-width: 1px;
38 border-left-width: 1px;
39}
40.txtArea
41{}{
42 overflow:visible;
43 BORDER-RIGHT: #000099 1px solid;
44 BORDER-TOP: #000099 1px solid;
45 FONT-SIZE: 14px;
46 BORDER-LEFT: #000099 1px solid;
47 WIDTH: 235px; COLOR: #330066;
48 BORDER-BOTTOM: #000099 1px solid;
49 HEIGHT: 25px;
50 BACKGROUND-COLOR: #fff8f0;
51 border-top-width: 1px;
52 border-right-width: 1px;
53 border-bottom-width: 1px;
54 border-left-width: 1px;
55}
56 </style>
57</head>
58<body>
59 <form id="form1" runat="server">
60 <asp:ScriptManager ID="ScriptManager1" runat="server" />
61 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
62 <ContentTemplate>
63 <asp:Timer ID="Timer1" runat="server" Interval="500" OnTick="Timer1_Tick">
64 </asp:Timer>
65 <asp:Panel ID="Panel1" runat="server">
66 <table class="table1" id="Table1" style="WIDTH: 163px; HEIGHT: 32px">
67 <tr>
68 <td id="load" align="center"><img alt="loading" src="img/load.gif">
69 <asp:Label ID="msg" runat="server" Text="正在处理,请稍候!"></asp:Label></td>
70 </tr>
71 </table>
72 </asp:Panel>
73 <asp:Panel ID="Panel2" runat="server">
74 <div class="tdbg1" style="WIDTH:561px;height:20px">
75 第二步:填写视频相关信息</div>
76 <table class="table1" style="WIDTH:550px" cellspacing="3" cellpadding="0" border="0">
77 <tr><td style="width: 100px;">
78 视频缩略图</td>
79 <td style="width: 450px;">
80 <asp:Image ID="Image1" runat="server" /></td>
81 </tr>
82 <tr><td>
83 视频标题</td>
84 <td>
85 <asp:TextBox ID="tbTitle" runat="server" Width="247px" CssClass="input1"></asp:TextBox></td>
86 </tr>
87 <tr><td>
88 tag/标签</td>
89 <td style="height: 16px">
90 <asp:TextBox ID="tbTag" runat="server" Width="247px" CssClass="input1"></asp:TextBox></td>
91 </tr>
92 <tr><td>
93 视频分类</td>
94 <td>
95 <asp:DropDownList ID="DropDownList1" runat="server" Width="63px">
96 <asp:ListItem>自拍</asp:ListItem>
97 <asp:ListItem>翻唱</asp:ListItem>
98 <asp:ListItem>生活</asp:ListItem>
99 <asp:ListItem>搞笑</asp:ListItem>
100 <asp:ListItem>娱乐</asp:ListItem>
101 <asp:ListItem>绝活</asp:ListItem>
102 <asp:ListItem>广告</asp:ListItem>
103 <asp:ListItem>舞秀</asp:ListItem>
104 <asp:ListItem>游戏</asp:ListItem>
105 <asp:ListItem>体育</asp:ListItem>
106 <asp:ListItem>艺术</asp:ListItem>
107 <asp:ListItem>综艺</asp:ListItem>
108 </asp:DropDownList></td>
109 </tr>
110 <tr><td style="vertical-align: top">
111 视频描述</td>
112 <td>
113 <asp:TextBox CssClass="txtArea" ID="tbDisc" runat="server" Width="247px" TextMode="MultiLine"></asp:TextBox></td>
114 </tr>
115 <tr>
116 <td>
117 </td>
118 <td>
119 <asp:Button ID="Button1" runat="server" Text="ok! 确定发布" OnClick="Button1_Click" /></td>
120 </tr>
121 </table>
122 </asp:Panel>
123 </ContentTemplate>
124 </asp:UpdatePanel>
125 </form>
126</body>
127</html>
cs代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.IO;
11
12public partial class _Default : System.Web.UI.Page
13{
14 private fmss1.WsVideo fmsVideo = new fmss1.WsVideo();
15 string sqlcs = ConfigurationManager.ConnectionStrings["Ds10ConnectionString"].ToString();
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 string path;
22 string fname;
23 Panel1.Visible = true;
24 Panel2.Visible = false;
25 try
26 {
27 path = Request.QueryString["pa"];
28 fname = Request.QueryString["na"];
29 }
30 catch
31 {
32 msg.Text = "非法操作!";
33 Timer1.Visible = false;
34 return;
35 }
36 if (CheckVideo(path, fname))
37 {
38 //异步操作webservice
39 fmsVideo.flvImageCompleted += new fmss1.flvImageCompletedEventHandler(flvImage_Finished);
40 fmsVideo.flvImageAsync(fname,path);
41 }
42 else
43 {
44 msg.Text = "不要重复操作!";
45 Timer1.Visible = false;
46 }
47 }
48 }
49 protected void flvImage_Finished(object sender,fmss1.flvImageCompletedEventArgs e)
50 {
51 if (e.Result == "false")
52 {
53 System.Drawing.Image m = System.Drawing.Image.FromFile(Server.MapPath("flvImg\\失败.jpg"));
54 m.Save(Server.MapPath("flvImg\\" + Request.QueryString["na"] + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
55 }
56 else
57 {
58 while (!CreateImg())
59 {
60 System.Threading.Thread.Sleep(1000);
61 }
62 }
63 }
64 protected void Button1_Click(object sender, EventArgs e)
65 {
66 int userID = 1; //@@
67 string sql = "relVideo " + userID + ",'原创','" + DropDownList1.SelectedValue + "','"
68 + tbTitle.Text + "','" +tbTag.Text + "','" + tbDisc.Text + "','"
69 + Request.QueryString["na"] + "','" + Request.QueryString["pa"] + "'";
70 MyCommonSQL Csql = new MyCommonSQL(sqlcs);
71 if (Csql.Return_exec_If(sql))
72 {
73 string script = "alert('录制成功,页面跳转!');window.location.replace('partyaddif.aspx?if=')";
74 Microsoft.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "click", script, true);
75 }
76 }
77 /**//// <summary>
78 /// timer的触发器
79 /// </summary>
80 /// <param name="sender"></param>
81 /// <param name="e"></param>
82 protected void Timer1_Tick(object sender, EventArgs e)
83 {
84 if (File.Exists(Server.MapPath("flvImg\\" + Request.QueryString["na"] + ".jpg")))
85 {
86 Timer1.Visible = false;
87 Panel1.Visible = false;
88 Panel2.Visible = true;
89 Image1.ImageUrl = "flvImg/" + Request.QueryString["na"] + ".jpg";
90 }
91 }
92 /**//// <summary>
93 /// 查看数据库中是否存在
94 /// </summary>
95 /// <param name="pa"></param>
96 /// <param name="na"></param>
97 /// <returns></returns>
98 private bool CheckVideo(string pa,string na)
99 {
100 MyCommonSQL Csql = new MyCommonSQL(sqlcs);
101 string sql = "ExistsVideo '" + na + "','" + pa + "'";
102 sql = Csql.Return_s(sql);
103 if (sql != "0")
104 {
105 return false;
106 }
107 return true;
108 }
109 /**//// <summary>
110 /// 接收图片并生成
111 /// </summary>
112 /// <returns></returns>
113 private bool CreateImg()
114 {
115 if (fmsVideo.ExistsImage(Request.QueryString["na"]))//检测图片在FMS上是否生成
116 {
117 try
118 {
119 /**////定义并初始化文件对象;
120 ///得到二进制文件字节数组;
121 byte[] image = fmsVideo.ImageByte(Request.QueryString["na"]);
122 /**////转换为支持存储区为内存的流
123 System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
124 System.Drawing.Image m = System.Drawing.Image.FromStream(memStream);
125 /**////保存
126 m.Save(Server.MapPath("flvImg\\" + Request.QueryString["na"] + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
127 return true;
128 }
129 catch
130 {
131 System.Drawing.Image m = System.Drawing.Image.FromFile(Server.MapPath("flvImg\\失败.jpg"));
132 m.Save(Server.MapPath("flvImg\\" + Request.QueryString["na"] + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
133 return true;
134 }
135 }
136 return false;
137 }
138}
运行测试一下 图片成功生成
开始录制:
提交后页面在处理中:
视频缩略图生成后:
新Blog,第一贴 以后都在这里写。嘿嘿,写给自己看。