【问题标题】:Stream videos from Azure blob storage and ASP.NET Core 3从 Azure blob 存储和 ASP.NET Core 3 流式传输视频
【发布时间】:2021-10-06 19:31:49
【问题描述】:

我正在使用最新和推荐的Azure.Storage.Blobs 包。我将视频文件作为块上传,效果很好。现在的问题是将视频返回给 Web 客户端,即videojs。播放器正在使用Range 请求。

我的端点:

[HttpGet]
[Route("video/{id}")]
[AllowAnonymous]
public async Task<IActionResult> GetVideoStreamAsync(string id)
{
   var stream = await GetVideoFile(id);

   return File(stream, "video/mp4", true); // true is for enableRangeProcessing
}

还有我的GetVideoFile 方法

var ms = new MemoryStream();
await blobClient.DownloadToAsync(ms, null, new StorageTransferOptions
{
    InitialTransferLength = 1024 * 1024,
    MaximumConcurrency = 20,
    MaximumTransferLength = 4 * 1024 * 1024
});

ms.Position = 0;

return ms;

视频可以正常下载和流式传输。但它下载了整个视频,根本不尊重Range。我也试过DownloadTo(HttpRange)

var ms = new MemoryStream();

// parse range header... 
var range = new HttpRange(from, to);
BlobDownloadInfo info = await blobClient.DownloadAsync(range);
await info.Content.CopyToAsync(ms);
return ms;

但是浏览器中没有显示任何内容。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: c# azure asp.net-core azure-blob-storage


    【解决方案1】:

    如果有人遇到我自己的问题。

    CloudBlockBlob(我正在使用的版本:11.2.2)现在有 OpenReadAsync() 方法返回流。在我的情况下,我将此流返回到 videojs,它自己处理 Range 标头。

    【讨论】:

      【解决方案2】:

      请尝试在返回之前将内存流的位置重置为0

      var ms = new MemoryStream();
      
      // parse range header... 
      var range = new HttpRange(from, to);
      BlobDownloadInfo info = await blobClient.DownloadAsync(range);
      await info.Content.CopyToAsync(ms);
      ms.Position = 0;//ms is positioned at the end of the stream so we need to reset that.
      return ms;
      

      【讨论】:

      • 嗯,不.. 仍然没有视频
      • 你能编辑你的问题并包括前端代码吗?我想模拟一下。
      【解决方案3】:

      我相信仅使用 Azure Blob 是不可能实现的。更多信息在这里:https://stackoverflow.com/a/26053910/1384539

      但总而言之,您可以使用提供搜索开始/结束位置的 CDN:https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm

      另一种可能性是使用支持流式传输的 Azure 媒体服务。您的方法实际上是一种渐进式下载,这不是完全相同的想法,您可能会在网络外花费很多。 (假设您对同一个文件有很多访问权限)

      【讨论】:

        猜你喜欢
        • 2013-11-08
        • 1970-01-01
        • 2020-04-22
        • 2020-08-31
        • 2015-04-26
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        相关资源
        最近更新 更多