Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GewoonJaap committed Nov 7, 2023
2 parents 224155d + 1c9ad10 commit ff859dc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
4 changes: 2 additions & 2 deletions ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AssemblyName>WinWhisper</AssemblyName>
<ApplicationIcon>WinWhisper-White_Icon.ico</ApplicationIcon>
<Platforms>AnyCPU;ARM64</Platforms>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Description>WinWhisper is a Windows application that uses AI to detect and remove background noise from audio files.</Description>
<Authors>Jaap</Authors>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
Expand All @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Clowd.Squirrel" Version="2.9.42" />
<PackageReference Include="Clowd.Squirrel" Version="2.10.2" />
<PackageReference Include="Sentry" Version="3.40.1" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
4 changes: 3 additions & 1 deletion Data/VideoToConvertObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions Utility/ConsoleUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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=");
Expand Down
3 changes: 2 additions & 1 deletion Utility/VideoFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
48 changes: 35 additions & 13 deletions WhisperAI/AudioProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AudioProcessor
private List<SegmentData> _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;
Expand All @@ -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)
{
Expand All @@ -54,17 +47,22 @@ 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
_segments.Sort((x, y) => x.Start.CompareTo(y.Start));
//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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit ff859dc

Please sign in to comment.