From 04630462d39ab82844db218ed54300b9c48c1c62 Mon Sep 17 00:00:00 2001 From: Jaap <33700526+GewoonJaap@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:38:47 +0100 Subject: [PATCH 1/2] Add translation --- ConsoleApp/Program.cs | 2 +- Data/VideoToConvertObject.cs | 4 ++- Utility/ConsoleUtil.cs | 7 ++++++ Utility/VideoFinder.cs | 3 ++- WhisperAI/AudioProcessor.cs | 48 ++++++++++++++++++++++++++---------- 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/ConsoleApp/Program.cs b/ConsoleApp/Program.cs index ddd5219..1a1769e 100644 --- a/ConsoleApp/Program.cs +++ b/ConsoleApp/Program.cs @@ -55,7 +55,7 @@ await videosToConvert.Videos.ForEachAsync(async video => var audioPath = Extractor.ExtractAudioFromVideoFile(video.VideoPath); var audioProcessor = new AudioProcessor(); - await audioProcessor.ProcessAudio(audioPath, video.LanguageCode, videosToConvert.SubtitleOutputPath); + await audioProcessor.ProcessAudio(audioPath, video.LanguageCode, videosToConvert.SubtitleOutputPath, video.ShouldTranslate); //remove audioPath file File.Delete(audioPath); Console.WriteLine($"Finished audio processing for video: {video.VideoName}"); diff --git a/Data/VideoToConvertObject.cs b/Data/VideoToConvertObject.cs index 569bd51..f424f81 100644 --- a/Data/VideoToConvertObject.cs +++ b/Data/VideoToConvertObject.cs @@ -6,11 +6,13 @@ public class VideoToConvertObject public string LanguageCode { get; } public string VideoName { get; } + public bool ShouldTranslate { get; } - public VideoToConvertObject(string videoPath, string languageCode) + public VideoToConvertObject(string videoPath, string languageCode, bool shouldTranslate = false) { VideoPath = videoPath; LanguageCode = languageCode; VideoName = Path.GetFileName(videoPath); + ShouldTranslate = shouldTranslate; } } diff --git a/Utility/ConsoleUtil.cs b/Utility/ConsoleUtil.cs index 10f4a65..e4afffb 100644 --- a/Utility/ConsoleUtil.cs +++ b/Utility/ConsoleUtil.cs @@ -13,6 +13,13 @@ public static string AskForLanguageCode(string fileName) return languageCode; } + public static bool AskIfNeedsToBeTranslated() + { + Console.WriteLine("Do you want to translate the subtitles to English? (yes/no) Default: no"); + var translateSubtitles = Console.ReadLine() ?? string.Empty; + return translateSubtitles.ToLower() == "yes" || translateSubtitles.ToLower() == "y"; + } + public static void LogException(Exception ex) { Console.WriteLine("An error occured. Please report the following on our GitHub page: https://github.com/GewoonJaap/WinWhisper/issues/new?assignees=&labels=&projects=&template=bug_report.md&title="); diff --git a/Utility/VideoFinder.cs b/Utility/VideoFinder.cs index e3f73c9..7f11719 100644 --- a/Utility/VideoFinder.cs +++ b/Utility/VideoFinder.cs @@ -52,6 +52,7 @@ private static VideoToConvertObject GetVideoDetails(string videoPath) { var videoName = Path.GetFileName(videoPath); var languageCode = ConsoleUtil.AskForLanguageCode(videoName); - return new VideoToConvertObject(videoPath, languageCode); + var shouldTranslate = ConsoleUtil.AskIfNeedsToBeTranslated(); + return new VideoToConvertObject(videoPath, languageCode, shouldTranslate); } } diff --git a/WhisperAI/AudioProcessor.cs b/WhisperAI/AudioProcessor.cs index 4b77430..51bc134 100644 --- a/WhisperAI/AudioProcessor.cs +++ b/WhisperAI/AudioProcessor.cs @@ -9,7 +9,7 @@ public class AudioProcessor private List _segments = new(); private const GgmlType ModelType = GgmlType.Base; - public async Task ProcessAudio(string wavPath, string languageCode, string subtitleOutputPath) + public async Task ProcessAudio(string wavPath, string languageCode, string subtitleOutputPath, bool shouldTranslate) { var modelName = ModelNameFetcher.GgmlTypeToString(ModelType); var modelPath = FolderManager.Models + "/" + modelName; @@ -24,21 +24,14 @@ public async Task ProcessAudio(string wavPath, string languageCode, string subti fileWriter.Close(); Console.WriteLine("Downloaded model"); } - - var whisperFactory = WhisperFactory.FromPath(modelPath); - var builder = whisperFactory.CreateBuilder() - .WithSegmentEventHandler(OnNewSegment); + var processor = SetupProcessor(modelPath, languageCode, shouldTranslate, OnNewSegment); - if (languageCode.Length > 0) + if(processor is null) { - builder.WithLanguage(languageCode); + Console.WriteLine("Something went wrong while setting up the processor. Please try again."); + return; } - else - { - builder.WithLanguageDetection(); - } - var processor = builder.Build(); void OnNewSegment(SegmentData segmentData) { @@ -54,9 +47,13 @@ void OnNewSegment(SegmentData segmentData) { GC.KeepAlive(fileStream); GC.KeepAlive(processor); + Console.WriteLine("Processing audio file"); + processor.Process(fileStream); + Console.WriteLine("Processed"); + processor.Dispose(); } //Sort segments by start time @@ -64,7 +61,8 @@ void OnNewSegment(SegmentData segmentData) //remove all segments that have same time + content _segments = _segments.Distinct().ToList(); //Write segments to file as subtitles - var outputFilePath = GetOutputFilePath(wavPath, languageCode.Length == 0 ? "en" : languageCode, subtitleOutputPath); + var outputLanguagecode = languageCode.Length == 0 || shouldTranslate ? "en" : languageCode; + var outputFilePath = GetOutputFilePath(wavPath, outputLanguagecode, subtitleOutputPath); var writer = new StreamWriter(outputFilePath); var subtitleIndex = 0; @@ -126,5 +124,29 @@ private static string GetOutputFilePath(string wavPath, string languageCode, str var finalOutputPath = outputPath.Length == 0 ? FolderManager.SubtitlesFolder : outputPath; return finalOutputPath + "/" + fileName + "." + languageCode.ToUpper() + ".srt"; } + + private static WhisperProcessor? SetupProcessor(string modelPath, string languageCode, bool shouldTranslate, OnSegmentEventHandler OnNewSegment) + { + var whisperFactory = WhisperFactory.FromPath(modelPath); + + var builder = whisperFactory.CreateBuilder() + .WithSegmentEventHandler(OnNewSegment); + + if (languageCode.Length > 0) + { + builder.WithLanguage(languageCode); + } + else + { + builder.WithLanguageDetection(); + } + + if (shouldTranslate) + { + builder.WithTranslate(); + } + + return builder.Build(); + } } } \ No newline at end of file From 71cadbff881170b20f0ad05b4057f90add1ff2d4 Mon Sep 17 00:00:00 2001 From: Jaap <33700526+GewoonJaap@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:43:26 +0100 Subject: [PATCH 2/2] Update ConsoleApp.csproj --- ConsoleApp/ConsoleApp.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ConsoleApp/ConsoleApp.csproj b/ConsoleApp/ConsoleApp.csproj index f146cc8..285bc0d 100644 --- a/ConsoleApp/ConsoleApp.csproj +++ b/ConsoleApp/ConsoleApp.csproj @@ -8,7 +8,7 @@ WinWhisper WinWhisper-White_Icon.ico AnyCPU;ARM64 - 1.2.0 + 1.3.0 WinWhisper is a Windows application that uses AI to detect and remove background noise from audio files. Jaap True @@ -20,7 +20,7 @@ - +