[英]Stream videos from Azure blob storage and ASP.NET Core 3
我正在使用最新和推薦Azure.Storage.Blobs
package。 我將視頻文件作為塊上傳,效果很好。 現在的問題是將視頻返回給 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;
但是瀏覽器中沒有顯示任何內容。 實現這一目標的最佳方法是什么?
如果有人遇到,請回答我自己的問題。
CloudBlockBlob
(我正在使用的版本:11.2.2)現在具有OpenReadAsync()
方法,該方法返回 stream。 在我的情況下,我將這個 stream 返回給videojs
,它自己處理Range
header。
請嘗試在返回之前將 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;
我相信僅使用 Azure Blob 是不可能實現的。 更多信息在這里: https://stackoverflow.com/a/26053910/1384539
但總而言之,您可以使用提供搜索開始/結束 position 的 CDN: https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm
另一種可能性是使用支持流媒體的 Azure 媒體服務。 您的方法實際上是一種漸進式下載,這不是完全相同的想法,您可能會在網絡外花費很多。 (假設您對同一個文件有很多訪問權限)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.