Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Console App #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion YouTubeSubtitlesExtractor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSubtitlesExtractor",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{54EEFCF5-7733-451D-8523-6E20173B51E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeSubtitlesExtractorTests", "test\YouTubeSubtitlesExtractorTests\YouTubeSubtitlesExtractorTests.csproj", "{9C9459D6-C28C-4599-818D-0135AF450AAE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSubtitlesExtractorTests", "test\YouTubeSubtitlesExtractorTests\YouTubeSubtitlesExtractorTests.csproj", "{9C9459D6-C28C-4599-818D-0135AF450AAE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSubtitlesExtractorConsoleApp", "consoleApp\YouTubeSubtitlesExtractorConsoleApp\YouTubeSubtitlesExtractorConsoleApp.csproj", "{98A6F65D-97EA-4AE2-B662-76497402D5B8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "consoleapp", "consoleapp", "{203D49CC-6E1F-49BA-8DE9-AE7CE216FC9E}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The console app should be under the samples directory, instead of consoleapp

EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -25,12 +29,20 @@ Global
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Release|Any CPU.Build.0 = Release|Any CPU
{98A6F65D-97EA-4AE2-B662-76497402D5B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98A6F65D-97EA-4AE2-B662-76497402D5B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98A6F65D-97EA-4AE2-B662-76497402D5B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98A6F65D-97EA-4AE2-B662-76497402D5B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DA994943-763E-4946-835F-306EEB8D19F8} = {261C8A2F-343C-4ECE-BE55-B7D25CC1DF04}
{9C9459D6-C28C-4599-818D-0135AF450AAE} = {54EEFCF5-7733-451D-8523-6E20173B51E2}
{98A6F65D-97EA-4AE2-B662-76497402D5B8} = {203D49CC-6E1F-49BA-8DE9-AE7CE216FC9E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CB32AD7A-6648-4ABD-B0D8-F7601327522F}
EndGlobalSection
EndGlobal
82 changes: 82 additions & 0 deletions consoleApp/YouTubeSubtitlesExtractorConsoleApp/Program.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the generic builder pattern like:

var host = Host.CreateDefaultBuilder(args);
...

Please refer to: https://learn.microsoft.com/dotnet/core/extensions/generic-host

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Runtime.CompilerServices;

using Aliencube.YouTubeSubtitlesExtractor;
using Aliencube.YouTubeSubtitlesExtractor.Models;


//Url with some subtitles (testing purposes)
//https://www.youtube.com/watch?v=KLe7Rxkrj94

Console.WriteLine("Input youtube url: ");
string? youtubeUrl = Console.ReadLine();

if ( string.IsNullOrWhiteSpace( youtubeUrl) )
{
Console.WriteLine("Error, youtube url empty");
return;
}

var http = new HttpClient();
var youtube = new YouTubeVideo(http);

var details = await youtube.ExtractVideoDetailsAsync(youtubeUrl);

if (details is not null)
{

Console.WriteLine("AvailableLanguageCodes: ");
for (var i = 0; i < details.AvaiableLanguageCodes.Count; i++)
{
Console.WriteLine(details.AvaiableLanguageCodes[i]);
}

Console.WriteLine("Input language code: ");
string? langCode = Console.ReadLine();
if (string.IsNullOrWhiteSpace(langCode))
{
Console.WriteLine("Error, language code empty");
return;
}


var videoSubs = await youtube.ExtractSubtitleAsync(youtubeUrl, langCode);
if (videoSubs is not null && videoSubs.Content is not null)
{
string colSep = "\t";
Console.WriteLine($"Start {colSep} End {colSep} Subtitle");
for (var i = 0; i < videoSubs.Content.Count; i++)
{
var subsContent = videoSubs.Content[i];

string substart = "-", subend = "-";
string subtext = subsContent.Text ?? "-";

if (subsContent.Start.HasValue)
{
float start_ms = subsContent.Start ?? 0;
start_ms = start_ms * 1000;

TimeSpan start_ts = TimeSpan.FromMilliseconds(start_ms);
substart = start_ts.ToString(@"hh\:mm\:ss");

if (subsContent.Duration.HasValue)
{
float duration_ms = subsContent.Duration ?? 0;
duration_ms = duration_ms * 1000;

TimeSpan duration_ts = TimeSpan.FromMilliseconds(duration_ms);
subend = start_ts.Add(duration_ts).ToString(@"hh\:mm\:ss");
}
}

Console.WriteLine($"{substart} {colSep} {subend} {colSep} {subtext}");

}
}
else
{
Console.WriteLine("Error Extracting Subtitles");
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As .NET 8 is around the corner, I'd like to target this app to net8.0

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\YouTubeSubtitlesExtractor\YouTubeSubtitlesExtractor.csproj" />
</ItemGroup>

</Project>