Invidious is an alternative front-end to YouTube
Add preferred captions
Omar Rothca7c376874fd
diff --git a/src/invidious.cr b/src/invidious.crindex 0913a137..ce818b69 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -215,6 +215,13 @@ get "/watch" do |env|audio_streams = video.audio_streams(adaptive_fmts)captions = video.captions+ if preferences+ preferred_captions = captions.select { |caption| preferences.captions.includes? caption["name"]["simpleText"] }+ preferred_captions.sort_by! { |caption| preferences.captions.index(caption["name"]["simpleText"]).not_nil! }++ captions = captions - preferred_captions+ end+ preferred_captions ||= [] of JSON::Anyvideo.description = fill_links(video.description, "https", "www.youtube.com")video.description = add_alt_links(video.description)@@ -713,6 +720,11 @@ post "/preferences" do |env|comments = env.params.body["comments"]?comments ||= "youtube"+ captions_0 = env.params.body["captions_0"]?.try &.as(String) || ""+ captions_1 = env.params.body["captions_1"]?.try &.as(String) || ""+ captions_2 = env.params.body["captions_2"]?.try &.as(String) || ""+ captions = [captions_0, captions_1, captions_2]+redirect_feed = env.params.body["redirect_feed"]?.try &.as(String)redirect_feed ||= "off"redirect_feed = redirect_feed == "on"@@ -750,6 +762,7 @@ post "/preferences" do |env|"quality" => quality,"volume" => volume,"comments" => comments,+ "captions" => captions,"redirect_feed" => redirect_feed,"dark_mode" => dark_mode,"thin_mode" => thin_mode,diff --git a/src/invidious/users.cr b/src/invidious/users.crindex c27825e7..be71cd94 100644--- a/src/invidious/users.cr+++ b/src/invidious/users.cr@@ -33,6 +33,7 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({"quality" => "hd720","volume" => 100,"comments" => "youtube",+ "captions" => ["", "", ""],"dark_mode" => false,"thin_mode " => false,"max_results" => 40,@@ -41,7 +42,6 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({"unseen_only" => false,}.to_json)-# TODO: Migrate preferences so fields will not be nilableclass PreferencesJSON.mapping({video_loop: Bool,@@ -51,18 +51,19 @@ class Preferencesvolume: Int32,comments: {type: String,- nilable: true,default: "youtube",},+ captions: {+ type: Array(String),+ default: ["", "", ""],+ },redirect_feed: {type: Bool,- nilable: true,default: false,},dark_mode: Bool,thin_mode: {type: Bool,- nilable: true,default: false,},max_results: Int32,@@ -71,7 +72,6 @@ class Preferencesunseen_only: Bool,notifications_only: {type: Bool,- nilable: true,default: false,},})diff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex cb66bd22..1985ac77 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -1,3 +1,112 @@+CAPTION_LANGUAGES = [+ "",+ "English",+ "English (auto-generated)",+ "Afrikaans",+ "Albanian",+ "Amharic",+ "Arabic",+ "Armenian",+ "Azerbaijani",+ "Bangla",+ "Basque",+ "Belarusian",+ "Bosnian",+ "Bulgarian",+ "Burmese",+ "Catalan",+ "Cebuano",+ "Chinese (Simplified)",+ "Chinese (Traditional)",+ "Corsican",+ "Croatian",+ "Czech",+ "Danish",+ "Dutch",+ "Esperanto",+ "Estonian",+ "Filipino",+ "Finnish",+ "French",+ "Galician",+ "Georgian",+ "German",+ "Greek",+ "Gujarati",+ "Haitian Creole",+ "Hausa",+ "Hawaiian",+ "Hebrew",+ "Hindi",+ "Hmong",+ "Hungarian",+ "Icelandic",+ "Igbo",+ "Indonesian",+ "Irish",+ "Italian",+ "Japanese",+ "Javanese",+ "Kannada",+ "Kazakh",+ "Khmer",+ "Korean",+ "Kurdish",+ "Kyrgyz",+ "Lao",+ "Latin",+ "Latvian",+ "Lithuanian",+ "Luxembourgish",+ "Macedonian",+ "Malagasy",+ "Malay",+ "Malayalam",+ "Maltese",+ "Maori",+ "Marathi",+ "Mongolian",+ "Nepali",+ "Norwegian",+ "Nyanja",+ "Pashto",+ "Persian",+ "Polish",+ "Portuguese",+ "Punjabi",+ "Romanian",+ "Russian",+ "Samoan",+ "Scottish Gaelic",+ "Serbian",+ "Shona",+ "Sindhi",+ "Sinhala",+ "Slovak",+ "Slovenian",+ "Somali",+ "Southern Sotho",+ "Spanish",+ "Sundanese",+ "Swahili",+ "Swedish",+ "Tajik",+ "Tamil",+ "Telugu",+ "Thai",+ "Turkish",+ "Ukrainian",+ "Urdu",+ "Uzbek",+ "Vietnamese",+ "Welsh",+ "Western Frisian",+ "Xhosa",+ "Yiddish",+ "Yoruba",+ "Zulu",+]+class Videomodule HTTPParamConverterdef self.from_rs(rs)diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecrindex 222b02f6..b7bec5d5 100644--- a/src/invidious/views/embed.ecr+++ b/src/invidious/views/embed.ecr@@ -55,7 +55,7 @@ video, #my_video, .video-js, .vjs-default-skin<% end %><% captions.each do |caption| %><track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"- srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> ">+ label="<%= caption["name"]["simpleText"]%> "><% end %><% end %><% end %>diff --git a/src/invidious/views/preferences.ecr b/src/invidious/views/preferences.ecrindex 57e40967..5fe4cf52 100644--- a/src/invidious/views/preferences.ecr+++ b/src/invidious/views/preferences.ecr@@ -56,6 +56,30 @@ function update_value(element) {</select></div>+ <div class="pure-control-group">+ <label for="captions_0">Default captions: </label>+ <select class="pure-u-1-5" name="captions_0" id="captions_0">+ <% CAPTION_LANGUAGES.each do |option| %>+ <option <% if user.preferences.captions[0] == option %> selected <% end %>><%= option %></option>+ <% end %>+ </select>+ </div>++ <div class="pure-control-group">+ <label for="captions_fallback">Fallback languages: </label>+ <select class="pure-u-1-5" name="captions_1" id="captions_1">+ <% CAPTION_LANGUAGES.each do |option| %>+ <option <% if user.preferences.captions[1] == option %> selected <% end %>><%= option %></option>+ <% end %>+ </select>++ <select class="pure-u-1-5" name="captions_2" id="captions_2">+ <% CAPTION_LANGUAGES.each do |option| %>+ <option <% if user.preferences.captions[2] == option %> selected <% end %>><%= option %></option>+ <% end %>+ </select>+ </div>+<legend>Visual preferences</legend><div class="pure-control-group"><label for="dark_mode">Dark mode: </label>diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecrindex 963f1733..0aacf91b 100644--- a/src/invidious/views/watch.ecr+++ b/src/invidious/views/watch.ecr@@ -60,9 +60,14 @@<% end %><% end %>+ <% preferred_captions.each_with_index do |caption, i| %>+ <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"+ label="<%= caption["name"]["simpleText"]%>" <% if i == 0 %>default<% end %>>+ <% end %>+<% captions.each do |caption| %><track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"- srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> ">+ label="<%= caption["name"]["simpleText"]%>"><% end %><% end %><% end %>