mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 04:26:30 +00:00
add controller for video
This commit is contained in:
@@ -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 IOPath = System.IO.Path;
|
||||
|
||||
namespace Azaion.Repository.Entities;
|
||||
|
||||
public class Media
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Path { get; set; } = null!;
|
||||
public Guid? AnnotatorId { get; set; }
|
||||
public MediaStatusEnum Status { get; set; }
|
||||
public DateTime CreatedDate { get; set; }
|
||||
public Guid Id { get; set; }
|
||||
public string Path { get; set; } = null!;
|
||||
public int Resolution { get; set; }
|
||||
public int Bitrate { 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}");
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
using Azaion.Repository.DTO;
|
||||
using Azaion.Repository.Entities;
|
||||
|
||||
namespace Azaion.Video;
|
||||
|
||||
public interface IVideoManager
|
||||
{
|
||||
List<VideoDto> GetVideos();
|
||||
void OpenVideo(Guid mediaId);
|
||||
Media Get(Guid mediaId);
|
||||
|
||||
void CreateAnnotation(Guid mediaId, DateTime time)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
using Azaion.Repository;
|
||||
using Azaion.Repository.DTO;
|
||||
using Azaion.Repository.Entities;
|
||||
|
||||
namespace Azaion.Video;
|
||||
|
||||
@@ -19,11 +21,35 @@ public class VideoManager(IDbFactory dbFactory) : IVideoManager
|
||||
.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()
|
||||
{
|
||||
|
||||
@@ -31,6 +57,5 @@ public class VideoManager(IDbFactory dbFactory) : IVideoManager
|
||||
|
||||
public void FinishAnnotation(Guid mediaId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Azaion.Repository\Azaion.Repository.csproj" />
|
||||
<ProjectReference Include="..\Azaion.Video\Azaion.Video.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</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);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
using Azaion.Repository;
|
||||
using Azaion.Repository.DTO;
|
||||
using Azaion.Video;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
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.AddScoped<IVideoManager, VideoManager>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -20,29 +23,5 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
var summaries = new[]
|
||||
{
|
||||
"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.MapControllers();
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
Reference in New Issue
Block a user