diff --git a/src/Directory.packages.props b/src/Directory.packages.props index a2138ae..c18e98a 100644 --- a/src/Directory.packages.props +++ b/src/Directory.packages.props @@ -7,6 +7,7 @@ + diff --git a/src/StreamShorts.Console/Commands/DefaultCommand.cs b/src/StreamShorts.Console/Commands/DefaultCommand.cs index 1b5e5a4..62a921a 100644 --- a/src/StreamShorts.Console/Commands/DefaultCommand.cs +++ b/src/StreamShorts.Console/Commands/DefaultCommand.cs @@ -4,13 +4,15 @@ internal sealed class DefaultCommand( IFileSystem fileSystem, IAnsiConsole console, IAudioExtractor audioExtractor, - ITranscriber transcriber + ITranscriber transcriber, + ITranscriptAnalyzer transcriptAnalyzer ) : AsyncCommand { private readonly IFileSystem _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); private readonly IAnsiConsole _console = console ?? throw new ArgumentNullException(nameof(console)); private readonly IAudioExtractor _audioExtractor = audioExtractor ?? throw new ArgumentNullException(nameof(audioExtractor)); private readonly ITranscriber _transcriber = transcriber ?? throw new ArgumentNullException(nameof(transcriber)); + private readonly ITranscriptAnalyzer _transcriptAnalyzer = transcriptAnalyzer ?? throw new ArgumentNullException(nameof(transcriptAnalyzer)); internal class Settings : CommandSettings { @@ -79,6 +81,15 @@ internal sealed class DefaultCommand( _console.MarkupLine($"[blue]Transcription completed[/] [green]successfully![/]"); + TranscriptAnalysis? analysis = null; + + await _console.Status() + .Spinner(Spinner.Known.Dots) + .StartAsync("Analyzing transcript...", async ctx => + { + analysis = await _transcriptAnalyzer.AnalyzeAsync(transcriptionSegments); + }); + return 0; } } \ No newline at end of file diff --git a/src/StreamShorts.Console/Program.cs b/src/StreamShorts.Console/Program.cs index a8b4730..9b74e8d 100644 --- a/src/StreamShorts.Console/Program.cs +++ b/src/StreamShorts.Console/Program.cs @@ -38,89 +38,6 @@ finally await Log.CloseAndFlushAsync(); } -// // Step 3: Split the wave stream into 2 minute segments -// waveStream.Position = 0; -// var segmentDuration = TimeSpan.FromMinutes(2); -// var segments = new List(); -// using var waveReader = new WaveFileReader(waveStream); -// var segmentCount = (int)Math.Ceiling(waveReader.TotalTime.TotalMilliseconds / segmentDuration.TotalMilliseconds); - -// Directory.CreateDirectory("segments"); - -// foreach (var i in Enumerable.Range(0, segmentCount)) -// { -// waveStream.Position = 0; -// using var segmentWaveReader = new WaveFileReader(waveStream); -// var segment = segmentWaveReader.ToSampleProvider() -// .Skip(i * segmentDuration) -// .Take(segmentDuration); -// var segmentProvider = segment.ToWaveProvider16(); -// var segmentStream = new MemoryStream(); -// WaveFileWriter.WriteWavFileToStream(segmentStream, segmentProvider); -// segmentStream.Position = 0; -// segments.Add(segmentStream); -// } - -// // Step 4: Transcribe each segment using Whisper -// using var modelMemoryStream = new MemoryStream(); -// var model = await WhisperGgmlDownloader.Default.GetGgmlModelAsync(GgmlType.TinyEn); -// await model.CopyToAsync(modelMemoryStream); -// var whisperFactory = WhisperFactory.FromBuffer(modelMemoryStream.ToArray()); -// using var whisperProcessor = whisperFactory.CreateBuilder() -// .WithLanguage("en") -// .Build(); - -// var completeTranscription = new StringBuilder(); - -// foreach (var (i, segment) in segments.Select((s, index) => (index, s))) -// { -// var durationOffset = TimeSpan.FromMilliseconds(i * segmentDuration.TotalMilliseconds); -// var segmentTranscription = new StringBuilder(); - -// await foreach (var result in whisperProcessor.ProcessAsync(segment, CancellationToken.None)) -// { -// var startTime = result.Start + durationOffset; -// var endTime = result.End + durationOffset; -// segmentTranscription.AppendLine(CultureInfo.CurrentCulture, $"[{startTime:hh\\:mm\\:ss} - {endTime:hh\\:mm\\:ss}] {result.Text}"); -// } - -// completeTranscription.Append(segmentTranscription); -// } - -// // Step 5: Send the transcription to LLM for analysis -// // TODO: Explore this prompt further...seems break -// // when transcription is long -// var prompt = $$""" -// I need your help to transform my YouTube live stream transcript into engaging YouTube Shorts. Act as my content editor and pinpoint **all potential candidate segments** that are perfect for short-form video. I'm looking for clips that are: - -//   * **Funny:** Moments that will make viewers laugh. -//   * **Informative:** Sections packed with valuable information or tips. -//   * **Insightful:** Portions offering unique perspectives or 'aha\!' moments. - -// For each suggested short, please provide: - -//   * The **start time** of the initial segment and the **end time** of the final segment. The duration of each short should be no longer than 3 minutes, but **aim for durations between 15 seconds and 60 seconds**. However, the short **must be as long as necessary to capture the complete thought or idea**, even if it means exceeding the target range or extending slightly to capture all necessary dialogue. -//   * A concise **title** that grabs attention. -//   * A brief **description** highlighting the short's content and its appeal. -//   * An **explanation** of why this particular segment is suitable for a YouTube Short, focusing on its potential for discoverability and engagement. - -// Please format your response as a JSON array of objects with the following structure: - -// ```json -// { -//   ""title"": ""string"", -//   "start_time": "string", -//   "end_time": "string", -//   "description": "string", -//   "explanation": "string" -// } -// ``` - -// Here is the transcript of my YouTube live stream: - -// {{completeTranscription}} -// """; - // if (string.IsNullOrWhiteSpace(apiKey)) // { // throw new InvalidOperationException("GeminiApiKey is not configured in appsettings.json."); diff --git a/src/StreamShorts.Console/Usings.cs b/src/StreamShorts.Console/Usings.cs index 0f992ee..f3aee19 100644 --- a/src/StreamShorts.Console/Usings.cs +++ b/src/StreamShorts.Console/Usings.cs @@ -16,4 +16,5 @@ global using Spectre.Console.Cli; global using StreamShorts.Console.Commands; global using StreamShorts.Console.Hosting; global using StreamShorts.Library.Media.Audio; -global using StreamShorts.Library.Transcription; \ No newline at end of file +global using StreamShorts.Library.Transcription; +global using StreamShorts.Library.Analysis; \ No newline at end of file diff --git a/src/StreamShorts.Library/Analysis/GeminiAnalyzer.cs b/src/StreamShorts.Library/Analysis/GeminiAnalyzer.cs new file mode 100644 index 0000000..6282f83 --- /dev/null +++ b/src/StreamShorts.Library/Analysis/GeminiAnalyzer.cs @@ -0,0 +1,88 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +using NAudio.CoreAudioApi; + +using StreamShorts.Library.Transcription; + +namespace StreamShorts.Library.Analysis; + +public sealed class GeminiAnalyzer( + IHttpClientFactory httpClientFactory, + string apiKey +) : ITranscriptAnalyzer +{ + private readonly IHttpClientFactory _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); + private readonly string _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); + + public async Task AnalyzeAsync(IEnumerable segments) + { + using var client = _httpClientFactory.CreateClient(); + client.Timeout = TimeSpan.FromMinutes(5); + + // TODO: Load prompt from resource file + var requestUrl = $"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key={_apiKey}"; + using var request = new HttpRequestMessage(HttpMethod.Post, requestUrl) + { + Content = new StringContent( + JsonSerializer.Serialize(new + { + contents = new[] + { + new + { + role = "user", + parts = new[] + { + new + { + text = prompt + } + }, + } + }, + generationConfig = new + { + responseMimeType = "application/json", + } + }), + Encoding.UTF8, + "application/json" + ) + }; + var response = await client.SendAsync(request).ConfigureAwait(false); + var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + var responseJson = JsonSerializer.Deserialize(responseContent); + var candidatesText = responseJson? + .Candidates? + .FirstOrDefault()? + .Content + .Parts?.FirstOrDefault()? + .Text; + + var clips = JsonSerializer.Deserialize>(candidatesText ?? string.Empty); + return new TranscriptAnalysis(clips ?? []); + } +} + +// TODO: Sort this shit out +record LLMResponse( + [property: JsonPropertyName("candidates")] + Candidate[] Candidates +); + +record Candidate( + [property: JsonPropertyName("content")] + Content Content +); + +record Content( + [property: JsonPropertyName("parts")] + Part[] Parts +); + +record Part( + [property: JsonPropertyName("text")] + string Text +); diff --git a/src/StreamShorts.Library/Analysis/ITranscriptAnalyzer.cs b/src/StreamShorts.Library/Analysis/ITranscriptAnalyzer.cs new file mode 100644 index 0000000..f9ce38e --- /dev/null +++ b/src/StreamShorts.Library/Analysis/ITranscriptAnalyzer.cs @@ -0,0 +1,8 @@ +using StreamShorts.Library.Transcription; + +namespace StreamShorts.Library.Analysis; + +public interface ITranscriptAnalyzer +{ + Task AnalyzeAsync(IEnumerable segments); +} \ No newline at end of file diff --git a/src/StreamShorts.Library/Analysis/ShortClip.cs b/src/StreamShorts.Library/Analysis/ShortClip.cs new file mode 100644 index 0000000..8582caa --- /dev/null +++ b/src/StreamShorts.Library/Analysis/ShortClip.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; + +namespace StreamShorts.Library.Analysis; + +public record ShortClip( + [property: JsonPropertyName("title")] + string Title, + [property: JsonPropertyName("description")] + string Description, + [property: JsonPropertyName("explanation")] + string Explanation, + [property: JsonPropertyName("start_time")] + TimeSpan StartTime, + [property: JsonPropertyName("end_time")] + TimeSpan EndTime +); diff --git a/src/StreamShorts.Library/Analysis/TranscriptAnalysis.cs b/src/StreamShorts.Library/Analysis/TranscriptAnalysis.cs new file mode 100644 index 0000000..56ead05 --- /dev/null +++ b/src/StreamShorts.Library/Analysis/TranscriptAnalysis.cs @@ -0,0 +1,6 @@ +namespace StreamShorts.Library.Analysis; + +public class TranscriptAnalysis(IEnumerable shortClips) +{ + public IEnumerable ShortClips { get; init; } = shortClips; +} \ No newline at end of file diff --git a/src/StreamShorts.Library/Resources/DefaultAnalysisPrompt.md b/src/StreamShorts.Library/Resources/DefaultAnalysisPrompt.md new file mode 100644 index 0000000..5c47082 --- /dev/null +++ b/src/StreamShorts.Library/Resources/DefaultAnalysisPrompt.md @@ -0,0 +1,28 @@ +I need your help to transform my YouTube live stream transcript into engaging YouTube Shorts. Act as my content editor and pinpoint **all potential candidate segments** that are perfect for short-form video. I'm looking for clips that are: + +  - **Funny:** Moments that will make viewers laugh. +  - **Informative:** Sections packed with valuable information or tips. +  - **Insightful:** Portions offering unique perspectives or 'aha\!' moments. + +For each suggested short, please provide: + +  - The **start time** of the initial segment and the **end time** of the final segment. The duration of each short should be no longer than 3 minutes, but **aim for durations between 15 seconds and 60 seconds**. However, the short **must be as long as necessary to capture the complete thought or idea**, even if it means exceeding the target range or extending slightly to capture all necessary dialogue. +  - A concise **title** that grabs attention. +  - A brief **description** highlighting the short's content and its appeal. +  - An **explanation** of why this particular segment is suitable for a YouTube Short, focusing on its potential for discoverability and engagement. + +Please format your response as a JSON array of objects with the following structure: + +```json +{ +  "title": "string", +  "start_time": "string", +  "end_time": "string", +  "description": "string", +  "explanation": "string" +} +``` + +Here is the transcript of my YouTube live stream: + +{0} diff --git a/src/StreamShorts.Library/StreamShorts.Library.csproj b/src/StreamShorts.Library/StreamShorts.Library.csproj index 97627db..0f0e35d 100644 --- a/src/StreamShorts.Library/StreamShorts.Library.csproj +++ b/src/StreamShorts.Library/StreamShorts.Library.csproj @@ -1,6 +1,7 @@  +