From 06a302217098c1a3b0c3d0c4f233ac7f65b2fa76 Mon Sep 17 00:00:00 2001 From: girst Date: Mon, 24 Apr 2023 21:51:24 +0000 Subject: [PATCH] add heuristic whether a video is a 'youtube shorts' this requires moving the aspect ratio calculation to common; takting this opportunity to s/aspectr/aspect/ and simplyfying AR css. --- app/common/common.py | 20 ++++++++++++++++++++ app/youtube/lib.py | 10 ---------- app/youtube/templates/watch.html.j2 | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/common/common.py b/app/common/common.py index 6624a41..fc5c0b2 100644 --- a/app/common/common.py +++ b/app/common/common.py @@ -330,6 +330,24 @@ def video_metadata(metadata): f"{meta2.get('publishDate','1970-01-01')}T00:00:00Z" ) + # the actual video streams have exact information: + # Note that we use x:1 (cinema style) aspect ratios, omitting the ':1' part. + try: + sd = metadata['streamingData'] + some_stream = (sd.get('adaptiveFormats',[]) + sd.get('formats',[]))[0] + aspect_ratio = some_stream['width'] / some_stream['height'] + # if that's unavailable (e.g. on livestreams), fall back to 16:9 (later) + except: + aspect_ratio = None + + # shorts are <= 60 seconds and vertical or square. if we were unable to + # determine it, we set it to None. + is_short = ( + None if length is None or aspect_ratio is None else + True if length <= 60 and aspect_ratio <= 1 else + False + ) + # Note: 'premiere' videos have livestream=False and published= will be the # start of the premiere. return { @@ -339,8 +357,10 @@ def video_metadata(metadata): 'published': published_at, 'views': int(meta1['viewCount']), 'length': length, + 'aspect': aspect_ratio or 16/9, 'livestream': meta1['isLiveContent'], 'premiere': meta1.get('isUpcoming') and not meta1['isLiveContent'], + 'shorts': is_short, } def store_video_metadata(video_id): diff --git a/app/youtube/lib.py b/app/youtube/lib.py index bc1e442..9d42320 100644 --- a/app/youtube/lib.py +++ b/app/youtube/lib.py @@ -8,15 +8,6 @@ from ..common.innertube import prepare_infocards, prepare_endcards, G def prepare_metadata(metadata): meta = metadata['videoDetails'] - # the actual video streams have exact information: - try: - sd = metadata['streamingData'] - some_stream = (sd.get('adaptiveFormats',[]) + sd.get('formats',[]))[0] - aspect_ratio = some_stream['width'] / some_stream['height'] - # if that's unavailable (e.g. on livestreams), fall back to 16:9 - except: - aspect_ratio = 16/9 - # Note: we could get subtitles in multiple formats directly by querying # https://video.google.com/timedtext?hl=en&type=list&v= followed by # https://www.youtube.com/api/timedtext?lang=&v=&fmt={srv1|srv2|srv3|ttml|vtt}, @@ -48,7 +39,6 @@ def prepare_metadata(metadata): return { **video_metadata(metadata), 'description': meta['shortDescription'], - 'aspectr': aspect_ratio, 'unlisted': not meta['isCrawlable'], 'poster': poster, 'endcards': endcards, diff --git a/app/youtube/templates/watch.html.j2 b/app/youtube/templates/watch.html.j2 index 1beb3d0..891fa64 100644 --- a/app/youtube/templates/watch.html.j2 +++ b/app/youtube/templates/watch.html.j2 @@ -9,7 +9,7 @@ {% block content %} {% if video_url %} -
+
{% else %}{#TODO: this'll break livestreams #} - + {% endif %} {% if video_error %} -- 2.39.3