This commit is contained in:
2023-12-08 14:50:02 +00:00
parent cef1b845bc
commit 865747fbae
13 changed files with 676 additions and 6 deletions

View File

@ -0,0 +1,34 @@
using FFMpegCore;
using NostrStreamer.Database;
namespace NostrStreamer.Services.Clips;
public class ClipGenerator
{
private readonly ILogger<ClipGenerator> _logger;
private readonly Config _config;
public ClipGenerator(ILogger<ClipGenerator> logger, Config config)
{
_logger = logger;
_config = config;
}
public async Task<string> GenerateClip(UserStream stream)
{
const int clipLength = 20;
var path = Path.ChangeExtension(Path.GetTempFileName(), ".mp4");
var cmd = FFMpegArguments
.FromUrlInput(new Uri(_config.DataHost, $"stream/{stream.Id}.m3u8"),
inOpt =>
{
inOpt.WithCustomArgument($"-ss -{clipLength}");
})
.OutputToFile(path, true, o => { o.WithDuration(TimeSpan.FromSeconds(clipLength)); })
.CancellableThrough(new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token);
_logger.LogInformation("Running command {cmd}", cmd.Arguments);
await cmd.ProcessAsynchronously();
return path;
}
}