add controller for video

This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-07-22 16:01:28 +03:00
parent bfbfdf6658
commit 6f78b88007
7 changed files with 80 additions and 35 deletions
@@ -0,0 +1,10 @@
namespace Azaion.Repository;
public class Constants
{
public const string MEDIA_HLS_FOLDER = "/azaion-media/hls";
public const string M3_U8_EXT = "m3u8";
public const string TS_EXT = "ts";
public const string FFMPEG_FILE = "/opt/homebrew/bin/ffmpeg";
}
@@ -1,12 +1,22 @@
using Azaion.Video.DTO; using Azaion.Video.DTO;
using IOPath = System.IO.Path;
namespace Azaion.Repository.Entities; namespace Azaion.Repository.Entities;
public class Media public class Media
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Path { get; set; } = null!; public string Path { get; set; } = null!;
public Guid? AnnotatorId { get; set; } public int Resolution { get; set; }
public MediaStatusEnum Status { get; set; } public int Bitrate { get; set; }
public DateTime CreatedDate { get; set; } public Guid? AnnotatorId { get; set; }
public MediaStatusEnum Status { get; set; }
public DateTime CreatedDate { get; set; }
private string MediaName => IOPath.GetFileNameWithoutExtension(Path);
private string OutDir => Directory.CreateDirectory(IOPath.Combine(Constants.MEDIA_HLS_FOLDER, MediaName)).FullName;
public string M3U8File => IOPath.Combine(OutDir, $"{MediaName}.{Constants.M3_U8_EXT}");
public string SegmentFile => IOPath.Combine(OutDir, $"{MediaName}%03d.{Constants.TS_EXT}");
} }
+2 -1
View File
@@ -1,11 +1,12 @@
using Azaion.Repository.DTO; using Azaion.Repository.DTO;
using Azaion.Repository.Entities;
namespace Azaion.Video; namespace Azaion.Video;
public interface IVideoManager public interface IVideoManager
{ {
List<VideoDto> GetVideos(); List<VideoDto> GetVideos();
void OpenVideo(Guid mediaId); Media Get(Guid mediaId);
void CreateAnnotation(Guid mediaId, DateTime time) void CreateAnnotation(Guid mediaId, DateTime time)
{ {
+28 -3
View File
@@ -1,5 +1,7 @@
using System.Diagnostics;
using Azaion.Repository; using Azaion.Repository;
using Azaion.Repository.DTO; using Azaion.Repository.DTO;
using Azaion.Repository.Entities;
namespace Azaion.Video; namespace Azaion.Video;
@@ -19,11 +21,35 @@ public class VideoManager(IDbFactory dbFactory) : IVideoManager
.ToList()); .ToList());
} }
public void OpenVideo(Guid mediaId) public void EncodeVideo(Guid mediaId)
{ {
throw new NotImplementedException(); var media = dbFactory.Run(db => db.Medias.SingleOrDefault(x => x.Id == mediaId))!;
string arguments = string.Concat($"-i \"{media.Path}\" ",
"-f hls ",
"-hls_time 2 ",
"-hls_playlist_type vod ",
"-hls_flags independent_segments ",
"-hls_segment_type mpegts ",
$"-hls_segment_filename \"{media.SegmentFile}\"",
$"\"{media.M3U8File}\"");
var process = new Process
{
StartInfo = new()
{
FileName = Constants.FFMPEG_FILE,
Arguments = arguments,
UseShellExecute = true,
},
EnableRaisingEvents = true,
};
process.Start();
} }
public Media Get(Guid mediaId) =>
dbFactory.Run(db => db.Medias.SingleOrDefault(x => x.Id == mediaId))!;
public void OpenTestVideo() public void OpenTestVideo()
{ {
@@ -31,6 +57,5 @@ public class VideoManager(IDbFactory dbFactory) : IVideoManager
public void FinishAnnotation(Guid mediaId) public void FinishAnnotation(Guid mediaId)
{ {
throw new NotImplementedException();
} }
} }
@@ -13,6 +13,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Azaion.Repository\Azaion.Repository.csproj" /> <ProjectReference Include="..\Azaion.Repository\Azaion.Repository.csproj" />
<ProjectReference Include="..\Azaion.Video\Azaion.Video.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -0,0 +1,19 @@
using Azaion.Video;
using Microsoft.AspNetCore.Mvc;
namespace Azaion.WebService.Controllers;
[Route("/controller")]
[ApiController]
public class VideoController(IVideoManager videoManager) : Controller
{
[HttpGet("{guid}")]
public IActionResult GetVideo(Guid guid)
{
var media = videoManager.Get(guid);
var fileStream = new FileStream(media.M3U8File, FileMode.Open);
var fileSize = new FileInfo(media.Path).Length;
Response.ContentLength = fileSize;
return File(fileStream, "application/x-mpegURL", true);
}
}
+4 -25
View File
@@ -1,14 +1,17 @@
using Azaion.Repository; using Azaion.Repository;
using Azaion.Repository.DTO; using Azaion.Repository.DTO;
using Azaion.Video;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection(nameof(ConnectionStrings))); builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection(nameof(ConnectionStrings)));
builder.Services.AddSingleton<IDbFactory, DbFactory>(sp => new DbFactory(sp.GetService<IOptions<ConnectionStrings>>()!.Value.FraudDb!)); builder.Services.AddSingleton<IDbFactory, DbFactory>(sp => new DbFactory(sp.GetService<IOptions<ConnectionStrings>>()!.Value.FraudDb!));
builder.Services.AddScoped<IVideoManager, VideoManager>();
var app = builder.Build(); var app = builder.Build();
@@ -20,29 +23,5 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
var summaries = new[] app.MapControllers();
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run(); app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}