Your CreateURLFromBuffer does exactly what I want to do. I am trying to create a Tesla Sentry viewer Blazor Webassembly app, but when I try to implement your package, and use your example, I get the following error:
Not allowed to load local resource: blob:System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.String,TeslaViewer.Pages.Index+<GetVideoStream>d__9]
@page "/"
@using FFmpegBlazor;
@using Microsoft.AspNetCore.Components.Forms;
@using System.Linq;
@using System.Globalization;
@inject IJSRuntime Runtime
<h3>Sentry Clips Viewer</h3>
<InputFile OnChange="@LoadFiles" multiple webkitdirectory directory @ref="inputFile" />
@if (events != null)
{
<InputSelect @bind-Value="selectedEvent" @oninput="OnEventChanged">
@foreach (var e in events)
{
<option value="@e.Key">@e.Key.ToString("yyyy-MM-dd_HH-mm-ss")</option>
}
</InputSelect>
if (selectedFiles != null)
{
<div>
@foreach (var file in selectedFiles)
{
<h4>@file.Name</h4>
<video controls src="blob:@GetVideoStream(file)" style="max-width: 300px;"></video>
<br />
}
</div>
}
}
@code {
private IEnumerable<IGrouping<DateTime, IBrowserFile>>? events;
private List<IBrowserFile>? selectedFiles;
private DateTime selectedEvent;
private InputFile? inputFile;
protected override async Task OnInitializedAsync()
{
if (FFmpegFactory.Runtime == null)
{
FFmpegFactory.Logger += WriteLogs;
}
//initialize Library
await FFmpegFactory.Init(Runtime);
}
void WriteLogs(Logs m)
{
Console.WriteLine(m.Type + " " + m.Message);
}
private void LoadFiles(InputFileChangeEventArgs args)
{
var files = args.GetMultipleFiles(int.MaxValue);
events = files
.Where(x => x.ContentType == "video/mp4")
.GroupBy(x => DateTime.ParseExact(x.Name.Substring(0, 19), "yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture, DateTimeStyles.None))
.OrderByDescending(x => x.Key)
.ToList();
selectedEvent = events.First().Key;
StateHasChanged();
}
private void OnEventChanged()
{
selectedFiles = events
.Single(x => x.Key == selectedEvent)
.ToList();
}
private async Task<string> GetVideoStream(IBrowserFile file)
{
using var stream = file.OpenReadStream(int.MaxValue); //Max size for file that can be read
var buffer = new byte[file.Size];
//read all bytes
await stream.ReadAsync(buffer);
//create a video link from buffer so that video can be played
var url = FFmpegFactory.CreateURLFromBuffer(buffer, file.Name, file.ContentType);
return url;
}
}