Skip to content

Commit

Permalink
Merge pull request #362 from NickvisionApps/subtitle-langs
Browse files Browse the repository at this point in the history
Customizable Subtitle Langs
  • Loading branch information
nlogozzo authored Jun 13, 2023
2 parents 4a46903 + bfbd617 commit 88c2073
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 44 deletions.
40 changes: 39 additions & 1 deletion NickvisionTubeConverter.GNOME/Blueprints/preferences_dialog.blp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,45 @@ Adw.PreferencesWindow _root {
}
}
}


Adw.EntryRow _subtitleLangsRow {
title: _("Subtitle Languages (Comma-Separated)");
show-apply-button: true;

[prefix]
Gtk.Image {
icon-name: "subtitles-symbolic";
}

[suffix]
Gtk.MenuButton {
valign: center;
direction: none;
icon-name: "dialog-question-symbolic";

popover: Gtk.Popover {
child: Gtk.Box {
orientation: vertical;
spacing: 6;
margin-start: 6;
margin-top: 6;
margin-end: 6;
margin-bottom: 6;

Adw.Clamp {
maximum-size: 300;
child: Gtk.Label {
wrap: true;
label: _("Some sites use two-letter language codes, whereas others use three-letter. For example, \"en\" and \"eng\" are both used for English.\n\nPlease specify both two-letter and three-letter codes for your languages for the best results.");
};
}
};
};

styles ["flat"]
}
}

Adw.ActionRow {
title: _("Cookies File");
subtitle: _("A cookies file can be provided to yt-dlp to allow downloading media that requires a login.");
Expand Down
2 changes: 1 addition & 1 deletion NickvisionTubeConverter.GNOME/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Program()
_mainWindowController.AppInfo.ShortName = _("Tube Converter");
_mainWindowController.AppInfo.Description = $"{_("Download web video and audio")}.";
_mainWindowController.AppInfo.Version = "2023.6.1-beta2";
_mainWindowController.AppInfo.Changelog = "<ul><li>Added authentication options when adding a download if needed</li><li>Added a Keyring to store credentials that can be used when authenticating</li><li>Added the advanced download option to specify a specific timeframe of a media to download</li><li>Added the ability to embed chapters in a download</li><li>Added the ability to turn on automatically cropping audio thumbnails</li><li>Playlist validation is a lot faster now</li><li>The \"Number Titles\" switch's state will now be remembered and used again for future downloads</li><li>The previous \"Video Resolution\" will be remembered and pre-selected if available for future downloads</li><li>Comment, Description, Synopsis, and Purl fields will no longer be embedded in metadata</li><li>If a download fails and was not stopped, Tube Converter will automatically retry it one more time</li><li>Fixed an issue where some websites were not validated</li><li>Fixed an issue where the incorrect file extension was sometimes shown for generic downloads</li><li>Updated translations (Thanks everyone on Weblate!)</li></ul>";
_mainWindowController.AppInfo.Changelog = "<ul><li>Added authentication options when adding a download if needed</li><li>Added a Keyring to store credentials that can be used when authenticating</li><li>Added the advanced download option to specify a specific timeframe of a media to download</li><li>Added the ability to embed chapters in a download</li><li>Added the ability to turn on automatically cropping audio thumbnails</li><li>Added the ability to specify a comma-separated list of language codes for subtitle downloading</li><li>Improved the speed of playlist validation</li><li>The \"Number Titles\" switch's state will now be remembered and used again for future downloads</li><li>The previous \"Video Resolution\" will be remembered and pre-selected if available for future downloads</li><li>Comment, Description, Synopsis, and Purl fields will no longer be embedded in metadata</li><li>If a download fails and was not stopped, Tube Converter will automatically retry it one more time</li><li>Fixed an issue where some websites were not validated</li><li>Fixed an issue where the incorrect file extension was sometimes shown for generic downloads</li><li>Updated translations (Thanks everyone on Weblate!)</li></ul>";
_mainWindowController.AppInfo.GitHubRepo = new Uri("https://github.com/NickvisionApps/TubeConverter");
_mainWindowController.AppInfo.IssueTracker = new Uri("https://github.com/NickvisionApps/TubeConverter/issues/new");
_mainWindowController.AppInfo.SupportUrl = new Uri("https://github.com/NickvisionApps/TubeConverter/discussions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<file preprocess="xml-stripblanks">playlist-symbolic.svg</file>
<file preprocess="xml-stripblanks">settings-symbolic.svg</file>
<file preprocess="xml-stripblanks">speedometer-symbolic.svg</file>
<file preprocess="xml-stripblanks">subtitles-symbolic.svg</file>
<file preprocess="xml-stripblanks">tag-outline-symbolic.svg</file>
<file preprocess="xml-stripblanks">wrench-wide-symbolic.svg</file>
</gresource>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions NickvisionTubeConverter.GNOME/Views/PreferencesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public partial class PreferencesDialog : Adw.PreferencesWindow
[Gtk.Connect] private readonly Gtk.Button _ariaMaxConnectionsPerServerResetButton;
[Gtk.Connect] private readonly Gtk.SpinButton _ariaMinSplitSizeSpin;
[Gtk.Connect] private readonly Gtk.Button _ariaMinSplitSizeResetButton;
[Gtk.Connect] private readonly Adw.EntryRow _subtitleLangsRow;
[Gtk.Connect] private readonly Adw.ViewStack _cookiesViewStack;
[Gtk.Connect] private readonly Gtk.Button _selectCookiesFileButton;
[Gtk.Connect] private readonly Gtk.Button _cookiesFileButton;
Expand Down Expand Up @@ -77,6 +78,7 @@ private PreferencesDialog(Gtk.Builder builder, PreferencesViewController control
OnThemeChanged();
}
};
_subtitleLangsRow.OnApply += SubtitleLangsChanged;
_selectCookiesFileButton.OnClicked += SelectCookiesFile;
_cookiesFileButton.OnClicked += SelectCookiesFile;
_unsetCookiesFileButton.OnClicked += UnsetCookiesFile;
Expand All @@ -96,6 +98,7 @@ private PreferencesDialog(Gtk.Builder builder, PreferencesViewController control
_ariaMaxConnectionsPerServerResetButton.OnClicked += (sender, e) => _ariaMaxConnectionsPerServerSpin.SetValue(16);
_ariaMinSplitSizeSpin.SetValue(_controller.AriaMinSplitSize);
_ariaMinSplitSizeResetButton.OnClicked += (sender, e) => _ariaMinSplitSizeSpin.SetValue(20);
_subtitleLangsRow.SetText(_controller.SubtitleLangs);
if (File.Exists(_controller.CookiesPath))
{
_cookiesViewStack.SetVisibleChildName("file-selected");
Expand Down Expand Up @@ -155,6 +158,28 @@ private void OnThemeChanged()
};
}

/// <summary>
/// Occurs when the subtitle langs row is applied
/// </summary>
/// <param name="sender">Adw.EntryRow</param>
/// <param name="e">EventArgs</param>
private void SubtitleLangsChanged(Adw.EntryRow sender, EventArgs e)
{
_subtitleLangsRow.SetTitle(_("Subtitle Languages (Comma-Separated)"));
_subtitleLangsRow.RemoveCssClass("error");
var valid = _controller.ValidateSubtitleLangs(_subtitleLangsRow.GetText());
if(valid)
{
_controller.SubtitleLangs = _subtitleLangsRow.GetText();
_controller.SaveConfiguration();
}
else
{
_subtitleLangsRow.SetTitle(_("Subtitle Languages (Invalid)"));
_subtitleLangsRow.AddCssClass("error");
}
}

/// <summary>
/// Occurs when a button to select cookies file is clicked
/// </summary>
Expand Down
14 changes: 0 additions & 14 deletions NickvisionTubeConverter.GNOME/nuget-sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,6 @@
"dest": "nuget-sources",
"dest-filename": "nickvision.keyring.2023.6.3.nupkg"
},
{
"type": "file",
"url": "https://api.nuget.org/v3-flatcontainer/python.deployment/2.0.0/python.deployment.2.0.0.nupkg",
"sha512": "b1fdba1a34f399baaad0360326ce6928bfee90f55a5f86c8b41c9639f099db7e7b3bba590c86a7cee043b298a00e7dc3a774f9bec355f58856f250683a937d6d",
"dest": "nuget-sources",
"dest-filename": "python.deployment.2.0.0.nupkg"
},
{
"type": "file",
"url": "https://api.nuget.org/v3-flatcontainer/python.included/3.11.2/python.included.3.11.2.nupkg",
"sha512": "867632c3dd439384e97eddfae8fc3340e6c9dfc8296daf7935b21f6caa48ef05c02ee8bae97c4aab17a6cda3ca5faad27044b529690c187e00c871dad8979109",
"dest": "nuget-sources",
"dest-filename": "python.included.3.11.2.nupkg"
},
{
"type": "file",
"url": "https://api.nuget.org/v3-flatcontainer/pythonnet/3.0.1/pythonnet.3.0.1.nupkg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class MainWindowController : IDisposable
/// <summary>
/// The DownloadOptions for a download
/// </summary>
public DownloadOptions DownloadOptions => new DownloadOptions(Configuration.Current.OverwriteExistingFiles, Configuration.Current.UseAria, Configuration.Current.CookiesPath, Configuration.Current.AriaMaxConnectionsPerServer, Configuration.Current.AriaMinSplitSize, Configuration.Current.EmbedMetadata, Configuration.Current.EmbedChapters);
public DownloadOptions DownloadOptions => new DownloadOptions(Configuration.Current.OverwriteExistingFiles, Configuration.Current.UseAria, Configuration.Current.CookiesPath, Configuration.Current.AriaMaxConnectionsPerServer, Configuration.Current.AriaMinSplitSize, Configuration.Current.SubtitleLangs, Configuration.Current.EmbedMetadata, Configuration.Current.EmbedChapters);

/// <summary>
/// Occurs when a notification is sent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using NickvisionTubeConverter.Shared.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace NickvisionTubeConverter.Shared.Controllers;

Expand All @@ -7,6 +11,8 @@ namespace NickvisionTubeConverter.Shared.Controllers;
/// </summary>
public class PreferencesViewController
{
private readonly List<string> _supportedLangCodes;

/// <summary>
/// Gets the AppInfo object
/// </summary>
Expand All @@ -17,7 +23,11 @@ public class PreferencesViewController
/// </summary>
internal PreferencesViewController()
{

var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(x => !string.IsNullOrEmpty(x.Name)).ToArray();
var codes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
codes.UnionWith(cultures.Select(x => x.TwoLetterISOLanguageName));
codes.UnionWith(cultures.Select(x => x.ThreeLetterISOLanguageName));
_supportedLangCodes = codes.ToList();
}

/// <summary>
Expand Down Expand Up @@ -110,6 +120,23 @@ public int AriaMinSplitSize
set => Configuration.Current.AriaMinSplitSize = value;
}

/// <summary>
/// A comma separated list of language codes for subtitle downloads
/// </summary>
public string SubtitleLangs
{
get => Configuration.Current.SubtitleLangs;

set
{
if(value[value.Length - 1] == ',')
{
value = value.Remove(value.Length - 1);
}
Configuration.Current.SubtitleLangs = value;
}
}

/// <summary>
/// The path of the cookies file to use for yt-dlp
/// </summary>
Expand Down Expand Up @@ -160,6 +187,32 @@ public bool EmbedChapters
set => Configuration.Current.EmbedChapters = value;
}

/// <summary>
/// Validates a subtitles langs string
/// </summary>
/// <param name="s">The comma-separated list of lang codes</param>
/// <returns>True if valid, else false</returns>
public bool ValidateSubtitleLangs(string s)
{
if(string.IsNullOrEmpty(s))
{
return false;
}
if(s[s.Length - 1] == ',')
{
s = s.Remove(s.Length - 1);
}
var codes = s.Split(",").Select(x => x.Trim());
foreach(var code in codes)
{
if(!_supportedLangCodes.Contains(code))
{
return false;
}
}
return true;
}

/// <summary>
/// Saves the configuration to disk
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions NickvisionTubeConverter.Shared/Helpers/DependencyManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NickvisionTubeConverter.Shared.Models;
using Python.Runtime;
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down
6 changes: 6 additions & 0 deletions NickvisionTubeConverter.Shared/Models/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
Expand Down Expand Up @@ -59,6 +60,10 @@ public class Configuration
/// </summary>
public int AriaMinSplitSize { get; set; }
/// <summary>
/// A comma separated list of language codes for subtitle downloads
/// </summary>
public string SubtitleLangs { get; set; }
/// <summary>
/// The path of the cookies file to use for yt-dlp
/// </summary>
public string CookiesPath { get; set; }
Expand Down Expand Up @@ -118,6 +123,7 @@ public Configuration()
UseAria = false;
AriaMaxConnectionsPerServer = 16;
AriaMinSplitSize = 20;
SubtitleLangs = $"{CultureInfo.CurrentCulture.TwoLetterISOLanguageName},{CultureInfo.CurrentCulture.ThreeLetterISOLanguageName}";
CookiesPath = "";
DisallowConversions = false;
EmbedMetadata = true;
Expand Down
8 changes: 6 additions & 2 deletions NickvisionTubeConverter.Shared/Models/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -255,9 +254,14 @@ public void Start(DownloadOptions options)
}
if (Subtitle != Subtitle.None)
{
var subtitleLangs = options.SubtitleLangs;
if(subtitleLangs[subtitleLangs.Length - 1] == ',')
{
subtitleLangs = subtitleLangs.Remove(subtitleLangs.Length - 1);
}
_ytOpt.Add("writesubtitles", true);
_ytOpt.Add("writeautomaticsub", true);
_ytOpt.Add("subtitleslangs", new List<string> { "en", CultureInfo.CurrentCulture.TwoLetterISOLanguageName });
_ytOpt.Add("subtitleslangs", subtitleLangs.Split(",").Select(x => x.Trim()).ToList());
postProcessors.Add(new Dictionary<string, dynamic>() { { "key", "FFmpegSubtitlesConvertor" }, { "format", Subtitle.ToString().ToLower() } });
postProcessors.Add(new Dictionary<string, dynamic>() { { "key", "FFmpegEmbedSubtitle" } });
}
Expand Down
8 changes: 7 additions & 1 deletion NickvisionTubeConverter.Shared/Models/DownloadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class DownloadOptions
/// </summary>
public int AriaMinSplitSize { get; init; }
/// <summary>
/// A comma separated list of language codes for subtitle downloads
/// </summary>
public string SubtitleLangs { get; init; }
/// <summary>
/// Whether or not to embed media metadata in the downloaded file
/// </summary>
public bool EmbedMetadata { get; init; }
Expand All @@ -42,15 +46,17 @@ public class DownloadOptions
/// <param name="cookiesPath">The path to the cookies file to use for yt-dlp</param>
/// <param name="ariaMaxConnectionsPerServer">The maximum number of connections to one server for each download (-x)</param>
/// <param name="ariaMinSplitSize">The minimum size of which to split a file (-k)</param>
/// <param name="subtitleLangs">A comma separated list of language codes for subtitle downloads</param>
/// <param name="embedMetadata">Whether or not to embed media metadata in the downloaded file</param>
/// <param name="embedChapters">Whether or not to embed chapters in the downloaded file</param>
public DownloadOptions(bool overwriteExistingFiles, bool useAria, string? cookiesPath, int ariaMaxConnectionsPerServer, int ariaMinSplitSize, bool embedMetadata, bool embedChapters)
public DownloadOptions(bool overwriteExistingFiles, bool useAria, string? cookiesPath, int ariaMaxConnectionsPerServer, int ariaMinSplitSize, string subtitleLangs, bool embedMetadata, bool embedChapters)
{
OverwriteExistingFiles = overwriteExistingFiles;
UseAria = useAria;
CookiesPath = cookiesPath;
AriaMaxConnectionsPerServer = ariaMaxConnectionsPerServer;
AriaMinSplitSize = ariaMinSplitSize;
SubtitleLangs = subtitleLangs;
EmbedMetadata = embedMetadata;
EmbedChapters = embedChapters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<ItemGroup>
<PackageReference Include="GetText.NET" Version="1.8.7" />
<PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" />
<PackageReference Include="Python.Included" Version="3.11.2" />
<PackageReference Include="pythonnet" Version="3.0.1" />
<PackageReference Include="Nickvision.Keyring" Version="2023.6.3" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 88c2073

Please sign in to comment.