Skip to content

Commit

Permalink
Storyboards: Fix broken first storyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
SamantazFox committed Aug 16, 2024
1 parent da3d58f commit 7b50388
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/invidious/videos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ struct Video

def storyboards
container = info.dig?("storyboards") || JSON::Any.new("{}")
return IV::Videos::Storyboard.from_yt_json(container)
return IV::Videos::Storyboard.from_yt_json(container, self.length_seconds)
end

def paid
Expand Down
13 changes: 10 additions & 3 deletions src/invidious/videos/storyboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Invidious::Videos
end

# Parse the JSON structure from Youtube
def self.from_yt_json(container : JSON::Any) : Array(Storyboard)
def self.from_yt_json(container : JSON::Any, length_seconds : Int32) : Array(Storyboard)
# Livestream storyboards are a bit different
# TODO: document exactly how
if storyboard = container.dig?("playerLiveStoryboardSpecRenderer", "spec").try &.as_s
Expand Down Expand Up @@ -70,8 +70,9 @@ module Invidious::Videos
# columns/rows: maximum amount of thumbnails that can be stuffed in a
# single image, horizontally and vertically.
# interval: interval between two thumbnails, in milliseconds
# name: storyboard filename. Usually "M$M" or "default"
# sigh: URL cryptographic signature
width, height, count, columns, rows, interval, _, sigh = sb.split("#")
width, height, count, columns, rows, interval, name, sigh = sb.split("#")

width = width.to_i
height = height.to_i
Expand All @@ -85,7 +86,7 @@ module Invidious::Videos
url.query = params.to_s

# Replace the template parts with what we have
url.path = url.path.sub("$L", i).sub("$N", "M$M")
url.path = url.path.sub("$L", i).sub("$N", name)

# This value represents the maximum amount of thumbnails that can fit
# in a single image. The last image (or the only one for short videos)
Expand All @@ -96,6 +97,12 @@ module Invidious::Videos
# hold all of the thumbnails. It can't be less than 1.
images_count = (count / thumbnails_per_image).ceil.to_i

# Compute the interval when needed (in general, that's only required
# for the first "default" storyboard).
if interval == 0
interval = ((length_seconds / count) * 1_000).to_i
end

Storyboard.new(
url: url,
width: width,
Expand Down

0 comments on commit 7b50388

Please sign in to comment.