📄 src/Cli/Program.cs
using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Slopper.Cli;
using Slopper.Cli.YouTubeAuth;
using Slopper.Domain;
using Slopper.Domain.Describer;
using Slopper.Infrastructure.Ai;
using Slopper.Infrastructure.Database;
using Slopper.Infrastructure.Ffmpeg;
using Slopper.Infrastructure.YouTube;

var builder = Host.CreateApplicationBuilder();

builder.ConfigureOpenTelemetry();

builder.Services.AddClipSelector().AddClipGenerator().AddCleaner();

builder.Services.AddJellyfinDatabase().AddSlopperDatabase().AddFfmpegServices().AddAi().AddYouTubeUploader();

builder.Services.AddYouTubeAuth();

using var app = builder.Build();

await app.StartAsync();

var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
var logger = app.Services.GetRequiredService<ILogger<Program>>();

using var scope = app.Services.CreateScope();
var timeProvider = scope.ServiceProvider.GetRequiredService<TimeProvider>();
var clipSelector = scope.ServiceProvider.GetRequiredService<ClipSelector>();
var clipRepository = scope.ServiceProvider.GetRequiredService<IClipRepository>();
var clipDescriber = scope.ServiceProvider.GetRequiredService<ClipDescriber>();
var clipExtractor = scope.ServiceProvider.GetRequiredService<IClipExtractor>();

var media = new MediaItem(Guid.Parse(args[0]), args[1], new Subtitles.Embedded(int.Parse(args[2])));
var (start, duration) = await clipSelector.PickClip(media, lifetime.ApplicationStopping);

var utcNow = timeProvider.GetUtcNow();
var clipId = Guid.CreateVersion7(utcNow);
var clipPath = Path.Join(args[3], $"{clipId}.mp4");

var descriptionTask = clipDescriber.DescribeClip(media, start, duration, lifetime.ApplicationStopping);

await clipExtractor.ExtractClip(media, start, duration, clipPath, lifetime.ApplicationStopping);

var description = await descriptionTask;

var clip = new Clip(clipId, media.Id, clipPath, start, duration, utcNow, description.Caption)
{
    Tags = description.Tags,
};
await clipRepository.Save(clip, lifetime.ApplicationStopping);

await app.StopAsync();