【问题标题】:Asynchronously Streaming Video with .Net Core API from Azure Blob Storage使用 Azure Blob 存储中的 .Net Core API 异步流式传输视频
【发布时间】:2020-08-31 08:39:56
【问题描述】:

我发现了一堆示例,这些示例使用了我的应用程序中不可用的对象,并且似乎与我的 .NET Core Web API 版本不匹配。从本质上讲,我正在开发一个在网页上有标签的项目,并希望使用来自服务器的流加载视频,而不是直接通过路径提供文件。一个原因是文件的来源可能会改变,并且通过路径提供它们不是我的客户想要的。所以我需要能够打开一个流并异步写入视频文件。

由于某种原因,这会产生 JSON 数据,所以这是错误的。我正在从 Azure Blob 存储下载视频文件并以流的形式返回,但我只是不明白如何将流式视频文件发送到 HTML 中的标签。

我的 API 控制器,

[AllowAnonymous]
    [HttpGet("getintroductoryvideos")]
    public async Task<Stream> GetIntroductoryVideos()
    {
        try
        {
            return  _documentsService.WriteContentToStream().Result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我的服务类,

 public async Task<Stream> WriteContentToStream()
    {
        var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
        await cloudBlob.FetchAttributesAsync();

        var fileStream = new MemoryStream();
        await cloudBlob.DownloadToStreamAsync(fileStream);
        return fileStream;
    }

【问题讨论】:

  • 这是什么_blobService?

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


【解决方案1】:

你可以试试下面的代码:

API 控制器:

[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<FileContentResult> GetIntroductoryVideos(string videoname)
{        
   return  await _documentsService.WriteContentToStream();        
}

服务类:

public async Task<FileContentResult> WriteContentToStream()
{
    var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);

    MemoryStream fileStream = new MemoryStream();
    await cloudBlob.DownloadToStreamAsync(fileStream);
    return new FileContentResult (fileStream.ToArray(), "application/octet-stream");

}

HTML:

<div className="xxx">
  <video height="auto">
      <source src="xx/getintroductoryvideos?videoname=xxx" type="video/mp4" />
  </video>
</div>

【讨论】:

  • 小更新,我们需要启用“RangeProcessing”(即) var result = new FileContentResult(memStream.ToArray(), "application/octet-stream");结果.EnableRangeProcessing = true;
  • 这是什么_blobService?
  • _BlobService 是我的服务类,您在其中继承了 Azure 的实际 blob 容器
【解决方案2】:

您可能希望在返回之前避免将整个视频加载到内存中。您应该能够使用FileStreamResult 传递流:

[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<IActionResult> GetIntroductoryVideos()
{
  var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
  var stream = await cloudBlob.OpenReadAsync();
  return new FileStreamResult(stream, "application/octet-stream");
}

【讨论】:

  • 此功能也可以正常工作,但无法转发视频。而且有点慢。我认为我们需要启用 Http Range。
猜你喜欢
  • 2013-11-08
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2019-01-27
  • 2012-04-14
  • 1970-01-01
相关资源
最近更新 更多