[英]Stream videos from Azure blob storage and ASP.NET Core 3
I'm using latest and recommended Azure.Storage.Blobs
package.我正在使用最新和推荐Azure.Storage.Blobs
package。 I'm uploading the video file as chunks, which works fine.我将视频文件作为块上传,效果很好。 The problem is now returning back the video to the web client, which is videojs
.现在的问题是将视频返回给 web 客户端,即videojs
。 The player is using Range
request.播放器正在使用Range
请求。
My endpoint:我的端点:
[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
}
And my GetVideoFile
method还有我的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;
The video gets downloaded and streamed just fine.视频被下载并流式传输就好了。 But it downloads the whole video and not respecting Range
at all.但它下载了整个视频,根本不尊重Range
。 I've also tried with DownloadTo(HttpRange)
我也试过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;
But nothing gets displayed in the browser.但是浏览器中没有显示任何内容。 What is the best way to achieve that?实现这一目标的最佳方法是什么?
Answering my own question if someone comes across.如果有人遇到,请回答我自己的问题。
CloudBlockBlob
(version I'm using: 11.2.2) now has OpenReadAsync()
method which returns stream. CloudBlockBlob
(我正在使用的版本:11.2.2)现在具有OpenReadAsync()
方法,该方法返回 stream。 In my case I'm returning this stream to videojs
which handles the Range
header on its own.在我的情况下,我将这个 stream 返回给videojs
,它自己处理Range
header。
Please try by resetting the memory stream's position to 0
before returning:请尝试在返回之前将 memory 流的 position 重置为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;
I believe it's not possible to achieve it only using Azure Blob.我相信仅使用 Azure Blob 是不可能实现的。 More info in here: https://stackoverflow.com/a/26053910/1384539更多信息在这里: https://stackoverflow.com/a/26053910/1384539
but in summary, you can use a CDN that offers the Seek Start / End position: https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm但总而言之,您可以使用提供搜索开始/结束 position 的 CDN: https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm
Another possibility is to use Azure Media Services that supports streamming.另一种可能性是使用支持流媒体的 Azure 媒体服务。 Your approach is actually a progressive download which is not exactly the same idea, and you'd probably spend a lot with network out.您的方法实际上是一种渐进式下载,这不是完全相同的想法,您可能会在网络外花费很多。 (assuming you have many access to the same file) (假设您对同一个文件有很多访问权限)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.