C# 从字节范围播放视频
我正在制作一个小应用程序,它将视频文件的一部分发送到客户端,以便在C# 从字节范围播放视频,c#,html,asp.net,asp.net-web-api,html5-video,C#,Html,Asp.net,Asp.net Web Api,Html5 Video,我正在制作一个小应用程序,它将视频文件的一部分发送到客户端,以便在元素上播放。 这是我的代码: [RoutePrefix("api/video")] public class VideoApiController : ApiController { [Route("")] [HttpGet] public async Task<HttpResponseMessage> GetVideoAsync([FromUri] GetVideoViewModel mode
元素上播放。
这是我的代码:
[RoutePrefix("api/video")]
public class VideoApiController : ApiController
{
[Route("")]
[HttpGet]
public async Task<HttpResponseMessage> GetVideoAsync([FromUri] GetVideoViewModel model)
{
var path = System.Web.Hosting.HostingEnvironment.MapPath($"~/wwwroot/{model.FileName}");
if (path == null || !File.Exists(path))
return new HttpResponseMessage(HttpStatusCode.NotFound);
using (var fileStream = File.OpenRead(path))
{
var bytes = new byte[model.To];
fileStream.Read(bytes, 0, bytes.Length - 1);
var memoryStream = new MemoryStream(bytes);
memoryStream.Seek(0, SeekOrigin.Begin);
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.PartialContent);
httpResponseMessage.Content = new ByteRangeStreamContent(memoryStream, new RangeHeaderValue(model.From, model.To), "video/webm");
return httpResponseMessage;
}
}
}
[RoutePrefix(“api/视频”)]
公共类VideoApicController:ApicController
{
[路线(“”)
[HttpGet]
公共异步任务GetVideoAsync([FromUri]GetVideoViewModel模型)
{
var path=System.Web.Hosting.HostingEnvironment.MapPath($“~/wwwroot/{model.FileName}”);
如果(path==null | |!File.Exists(path))
返回新的HttpResponseMessage(HttpStatusCode.NotFound);
使用(var fileStream=File.OpenRead(path))
{
var bytes=新字节[model.To];
读取(字节,0,字节.长度-1);
var memoryStream=新的memoryStream(字节);
memoryStream.Seek(0,SeekOrigin.Begin);
var httpResponseMessage=新的httpResponseMessage(HttpStatusCode.PartialContent);
httpResponseMessage.Content=新的ByteRangeStreamContent(memoryStream,新的RangeHeaderValue(model.From,model.To),“视频/webm”);
返回httpResponseMessage;
}
}
}
然后,我和邮递员测试了我的终点。
如果我选择的字节范围为0到100000(),则结果面板上可能会显示视频:
但是,当我在第一个tiem中选择从100000到200000()的字节范围时,视频为空:
据我所知,video/webm
使用编解码器,元数据包含在流的前几个字节中。
如果我想播放视频的一部分,而不是从一开始就播放它。我该怎么办?
谢谢。这不太容易做到 流的第一部分不仅仅是一些元数据,它是告诉玩家如何播放以及我们正在播放什么所需的所有信息,甚至是流中要使用的颜色信息 你可以做的是读取视频的标题,提取关键帧(称为线索),然后在流中搜索,并开始用新的标题将比特流出来。基本上,您将从头开始构建一个只适用于这种特定视频格式(编解码器)的web流媒体软件 但这里有一些信息可以让你知道如何写 或者您使用ffmpeg并寻找关键帧以跳转到一个好位置,让数百名其他开发人员沮丧地尖叫 微软有一个僵尸项目,让你包装 这里有一些其他的选择。 以及你如何寻找
另一个解决方案是安装一个视频流软件来处理这个问题。这不是很容易做到的 流的第一部分不仅仅是一些元数据,它是告诉玩家如何播放以及我们正在播放什么所需的所有信息,甚至是流中要使用的颜色信息 你可以做的是读取视频的标题,提取关键帧(称为线索),然后在流中搜索,并开始用新的标题将比特流出来。基本上,您将从头开始构建一个只适用于这种特定视频格式(编解码器)的web流媒体软件 但这里有一些信息可以让你知道如何写 或者您使用ffmpeg并寻找关键帧以跳转到一个好位置,让数百名其他开发人员沮丧地尖叫 微软有一个僵尸项目,让你包装 这里有一些其他的选择。 以及你如何寻找 另一个解决方案是安装一个视频流软件来处理这个问题