基础设施
由于这是一个小型项目,您可以使用 IIS 设置您的家用 PC,并直接从您自己的家用服务器流式传输内容。
媒体格式
在 MP4 文件中使用 H.264 编码的视频很重要,因为它是几乎所有设备都支持的格式。
内容交付
内容将通过 HTTP 流式传输,并且可以在应用程序或浏览器中查看。
编辑
由于您将要实施的只是一个小型系统,因此您可以忽略很多安全部分并首先专注于获取视频流...您仍然需要一个数据库,了解您存储了哪些视频以及它们的位置位于您的硬盘上,我的建议是将所有视频存储在一个位置,例如C:\MP4\
下一部分是设置 IIS,您可以使用您的 IP 地址,或者购买一个域并将其 A 记录更改为指向您的 IP。
然后,您可以创建一个小型数据库,其中包含一个名为 Videos 的表,列标记为 VideoID 和 FilePath,并用您的视频填充数据库。
数据库完成后,您可以继续编写一个通用处理程序来处理视频流。
创建一个名为 Watch.ashx 的新 .ashx 文件,现在无论您想观看哪个视频,您很快就可以通过像这样将 videoid 参数传递给 Watch.ashx 来实现...
http://127.0.0.1/Watch.ashx?v=VIDEOID
我在下面列出了整个课程以帮助您入门。
<%@ WebHandler Language="C#" Class="Watch" %>
using System;
using System.Globalization;
using System.IO;
using System.Web;
public class Watch : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
// Get the VideoID from the requests `v` parameters.
var videoId = context.Request.QueryString["v"];
/* With the videoId you'll need to retrieve the filepath
/ from the database. You'll need to replace the method
/ below with your own depending on whichever
/ DBMS you decide to work with.
*////////////////////////////////////////////////////////
var videoPath = DataBase.GetVideoPath(videoId);
// This method will stream the video.
this.RangeDownload(videoPath, context);
}
private void RangeDownload(string fullpath, HttpContext context)
{
long size;
long start;
long theend;
long length;
long fp = 0;
using (StreamReader reader = new StreamReader(fullpath))
{
size = reader.BaseStream.Length;
start = 0;
theend = size - 1;
length = size;
context.Response.AddHeader("Accept-Ranges", "0-" + size);
if (!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
long anotherStart;
long anotherEnd = theend;
string[] arrSplit = context.Request.ServerVariables["HTTP_RANGE"].Split('=');
string range = arrSplit[1];
if ((range.IndexOf(",", StringComparison.Ordinal) > -1))
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
if ((range.StartsWith("-")))
{
anotherStart = size - Convert.ToInt64(range.Substring(1));
}
else
{
arrSplit = range.Split('-');
anotherStart = Convert.ToInt64(arrSplit[0]);
long temp;
if ((arrSplit.Length > 1 && Int64.TryParse(arrSplit[1], out temp)))
{
anotherEnd = Convert.ToInt64(arrSplit[1]);
}
else
{
anotherEnd = size;
}
}
anotherEnd = (anotherEnd > theend) ? theend : anotherEnd;
if ((anotherStart > anotherEnd | anotherStart > size - 1 | anotherEnd >= size))
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
start = anotherStart;
theend = anotherEnd;
length = theend - start + 1;
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
context.Response.StatusCode = 206;
}
}
context.Response.ContentType = "video/mp4";
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
context.Response.AddHeader("Content-Length", length.ToString(CultureInfo.InvariantCulture));
context.Response.TransmitFile(fullpath, fp, length);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
我从cipto0382's工作中改编了上面的课程,原文可以在这里找到:
http://blogs.visigo.com/chriscoulson/easy-handling-of-http-range-requests-in-asp-net/
在临别时,为了节省带宽,我强烈建议您从 IIS 应用程序库下载媒体服务,您可以限制视频传输的比特率,以免占用您的全部带宽。