Compare commits

..

3 Commits

Author SHA1 Message Date
syeopite
3615bb0e62
Update src/invidious/videos/caption.cr
Co-authored-by: Samantaz Fox <coding@samantaz.fr>
2023-08-24 16:21:05 -07:00
syeopite
7d435f082b
Update src/invidious/videos/transcript.cr
Co-authored-by: Samantaz Fox <coding@samantaz.fr>
2023-08-24 23:20:20 +00:00
syeopite
1f7592e599
Refactor structure of caption.cr
Rename CaptionsMetadata to Metadata
Nest Metadata under Captions
Unnest LANGUAGES constant from Metadata to main Captions module
2023-08-24 16:00:02 -07:00
5 changed files with 89 additions and 93 deletions

View File

@ -7,7 +7,7 @@ module Invidious::Frontend::WatchPage
getter full_videos : Array(Hash(String, JSON::Any))
getter video_streams : Array(Hash(String, JSON::Any))
getter audio_streams : Array(Hash(String, JSON::Any))
getter captions : Array(Invidious::Videos::CaptionMetadata)
getter captions : Array(Invidious::Videos::Captions::Metadata)
def initialize(
@full_videos,

View File

@ -24,7 +24,7 @@ struct Video
property updated : Time
@[DB::Field(ignore: true)]
@captions = [] of Invidious::Videos::CaptionMetadata
@captions = [] of Invidious::Videos::Captions::Metadata
@[DB::Field(ignore: true)]
property adaptive_fmts : Array(Hash(String, JSON::Any))?
@ -215,9 +215,9 @@ struct Video
keywords.includes? "YouTube Red"
end
def captions : Array(Invidious::Videos::CaptionMetadata)
def captions : Array(Invidious::Videos::Captions::Metadata)
if @captions.empty? && @info.has_key?("captions")
@captions = Invidious::Videos::CaptionMetadata.from_yt_json(info["captions"])
@captions = Invidious::Videos::Captions::Metadata.from_yt_json(info["captions"])
end
return @captions

View File

@ -1,7 +1,8 @@
require "json"
module Invidious::Videos
struct CaptionMetadata
module Captions
struct Metadata
property name : String
property language_code : String
property base_url : String
@ -12,12 +13,12 @@ module Invidious::Videos
end
# Parse the JSON structure from Youtube
def self.from_yt_json(container : JSON::Any) : Array(CaptionMetadata)
def self.from_yt_json(container : JSON::Any) : Array(Captions::Metadata)
caption_tracks = container
.dig?("playerCaptionsTracklistRenderer", "captionTracks")
.try &.as_a
captions_list = [] of CaptionMetadata
captions_list = [] of Captions::Metadata
return captions_list if caption_tracks.nil?
caption_tracks.each do |caption|
@ -27,12 +28,9 @@ module Invidious::Videos
language_code = caption["languageCode"].to_s
base_url = caption["baseUrl"].to_s
auto_generated = false
if caption["kind"]? && caption["kind"] == "asr"
auto_generated = true
end
auto_generated = (caption["kind"]? == "asr")
captions_list << CaptionMetadata.new(name, language_code, base_url, auto_generated)
captions_list << Captions::Metadata.new(name, language_code, base_url, auto_generated)
end
return captions_list
@ -103,6 +101,7 @@ module Invidious::Videos
end
return result
end
end
# List of all caption languages available on Youtube.
LANGUAGES = {

View File

@ -4,16 +4,13 @@ module Invidious::Videos
record TranscriptLine, start_ms : Time::Span, end_ms : Time::Span, line : String
def self.generate_param(video_id : String, language_code : String, auto_generated : Bool) : String
if !auto_generated
is_auto_generated = ""
elsif is_auto_generated = "asr"
end
kind = auto_generated ? "asr" : ""
object = {
"1:0:string" => video_id,
"2:base64" => {
"1:string" => is_auto_generated,
"1:string" => kind,
"2:string" => language_code,
"3:string" => "",
},
@ -37,7 +34,7 @@ module Invidious::Videos
# Convert into array of TranscriptLine
lines = self.parse(initial_data)
# Taken from Invidious::Videos::CaptionMetadata.timedtext_to_vtt()
# Taken from Invidious::Videos::Captions::Metadata.timedtext_to_vtt()
vtt = String.build do |vtt|
vtt << <<-END_VTT
WEBVTT

View File

@ -89,7 +89,7 @@
<label for="captions[0]"><%= translate(locale, "preferences_captions_label") %></label>
<% preferences.captions.each_with_index do |caption, index| %>
<select class="pure-u-1-6" name="captions[<%= index %>]" id="captions[<%= index %>]">
<% Invidious::Videos::CaptionMetadata::LANGUAGES.each do |option| %>
<% Invidious::Videos::Captions::LANGUAGES.each do |option| %>
<option value="<%= option %>" <% if preferences.captions[index] == option %> selected <% end %>><%= translate(locale, option.blank? ? "none" : option) %></option>
<% end %>
</select>