Skip to content

Commit

Permalink
Storyboards: Use replace the NamedTuple by a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
SamantazFox committed Aug 16, 2024
1 parent 6878822 commit 8327862
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
18 changes: 9 additions & 9 deletions src/invidious/jsonify/api_v1/video_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ module Invidious::JSONify::APIv1
json.array do
storyboards.each do |storyboard|
json.object do
json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard[:width]}&height=#{storyboard[:height]}"
json.field "templateUrl", storyboard[:url]
json.field "width", storyboard[:width]
json.field "height", storyboard[:height]
json.field "count", storyboard[:count]
json.field "interval", storyboard[:interval]
json.field "storyboardWidth", storyboard[:storyboard_width]
json.field "storyboardHeight", storyboard[:storyboard_height]
json.field "storyboardCount", storyboard[:storyboard_count]
json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard.width}&height=#{storyboard.height}"
json.field "templateUrl", storyboard.url
json.field "width", storyboard.width
json.field "height", storyboard.height
json.field "count", storyboard.count
json.field "interval", storyboard.interval
json.field "storyboardWidth", storyboard.storyboard_width
json.field "storyboardHeight", storyboard.storyboard_height
json.field "storyboardCount", storyboard.storyboard_count
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions src/invidious/routes/api/v1/videos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ module Invidious::Routes::API::V1::Videos

env.response.content_type = "text/vtt"

storyboard = storyboards.select { |sb| width == "#{sb[:width]}" || height == "#{sb[:height]}" }
storyboard = storyboards.select { |sb| width == "#{sb.width}" || height == "#{sb.height}" }

if storyboard.empty?
haltf env, 404
Expand All @@ -215,21 +215,22 @@ module Invidious::Routes::API::V1::Videos

WebVTT.build do |vtt|
start_time = 0.milliseconds
end_time = storyboard[:interval].milliseconds
end_time = storyboard.interval.milliseconds

storyboard[:storyboard_count].times do |i|
url = storyboard[:url]
storyboard.storyboard_count.times do |i|
url = storyboard.url
authority = /(i\d?).ytimg.com/.match!(url)[1]?

url = url.gsub("$M", i).gsub(%r(https://i\d?.ytimg.com/sb/), "")
url = "#{HOST_URL}/sb/#{authority}/#{url}"

storyboard[:storyboard_height].times do |j|
storyboard[:storyboard_width].times do |k|
current_cue_url = "#{url}#xywh=#{storyboard[:width] * k},#{storyboard[:height] * j},#{storyboard[:width] - 2},#{storyboard[:height]}"
storyboard.storyboard_height.times do |j|
storyboard.storyboard_width.times do |k|
current_cue_url = "#{url}#xywh=#{storyboard.width * k},#{storyboard.height * j},#{storyboard.width - 2},#{storyboard.height}"
vtt.cue(start_time, end_time, current_cue_url)

start_time += storyboard[:interval].milliseconds
end_time += storyboard[:interval].milliseconds
start_time += storyboard.interval.milliseconds
end_time += storyboard.interval.milliseconds
end
end
end
Expand Down
61 changes: 34 additions & 27 deletions src/invidious/videos/storyboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@ require "http/params"

module Invidious::Videos
struct Storyboard
getter url : String
getter width : Int32
getter height : Int32
getter count : Int32
getter interval : Int32
getter storyboard_width : Int32
getter storyboard_height : Int32
getter storyboard_count : Int32

def initialize(
*, @url, @width, @height, @count, @interval,
@storyboard_width, @storyboard_height, @storyboard_count
)
end

# Parse the JSON structure from Youtube
def self.from_yt_json(container : JSON::Any)
storyboards = container.dig?("playerStoryboardSpecRenderer", "spec")
.try &.as_s.split("|")

if !storyboards
if storyboard = container.dig?("playerLiveStoryboardSpecRenderer", "spec").try &.as_s
return [{
url: storyboard.split("#")[0],
width: 106,
height: 60,
count: -1,
interval: 5000,
storyboard_width: 3,
return [Storyboard.new(
url: storyboard.split("#")[0],
width: 106,
height: 60,
count: -1,
interval: 5000,
storyboard_width: 3,
storyboard_height: 3,
storyboard_count: -1,
}]
storyboard_count: -1,
)]
end
end

items = [] of NamedTuple(
url: String,
width: Int32,
height: Int32,
count: Int32,
interval: Int32,
storyboard_width: Int32,
storyboard_height: Int32,
storyboard_count: Int32)
items = [] of Storyboard

return items if !storyboards

Expand All @@ -51,16 +58,16 @@ module Invidious::Videos
storyboard_height = storyboard_height.to_i
storyboard_count = (count / (storyboard_width * storyboard_height)).ceil.to_i

items << {
url: url.to_s.sub("$L", i).sub("$N", "M$M"),
width: width,
height: height,
count: count,
interval: interval,
storyboard_width: storyboard_width,
items << Storyboard.new(
url: url.to_s.sub("$L", i).sub("$N", "M$M"),
width: width,
height: height,
count: count,
interval: interval,
storyboard_width: storyboard_width,
storyboard_height: storyboard_height,
storyboard_count: storyboard_count,
}
storyboard_count: storyboard_count
)
end

items
Expand Down

0 comments on commit 8327862

Please sign in to comment.