Get correct video dimensions

This commit is contained in:
MMaker 2025-02-26 00:31:51 -05:00
parent 77f9545db6
commit 5a194507a0
Signed by: mmaker
GPG Key ID: CCE79B8FEDA40FB2

22
app.py
View File

@ -173,6 +173,12 @@ def allow_download(params):
return False
return True
def get_video_resolution(params):
if not params:
return None, None
video = params['media']['domand']['videos'][0]
return video['width'], video['height']
def get_video_quality(params, quality_level_threshold=3):
"""Get the code of the best video quality available (optionally below a certain threshold)"""
videos = params['media']['domand']['videos']
@ -275,6 +281,7 @@ def proxy(video_id):
params["video"]["thumbnail"].get("middleUrl") or
params["video"]["thumbnail"].get("url")
) if params else None
video_width, video_height = get_video_resolution(params) if params else (None, None)
download_allowed = allow_download(params) if params else False
video_quality = get_video_quality(params) if params else None
@ -291,8 +298,6 @@ def proxy(video_id):
cdn_video_url = get_cdn_url(video_id)
og_tags = soup.find_all("meta", property=lambda x: x)
og_video_width = None
og_video_height = None
for tag in og_tags:
if 'data-server' in tag.attrs:
del tag.attrs['data-server']
@ -302,21 +307,16 @@ def proxy(video_id):
# Fix video URL
if tag.get("property") == "og:video:url" or tag.get("property") == "og:video:secure_url":
tag["content"] = cdn_video_url
# Set vars
if tag.get("property") == "og:video:width":
og_video_width = tag.get("content")
if tag.get("property") == "og:video:height":
og_video_height = tag.get("content")
og_tags_str = "\n".join(str(tag) for tag in og_tags if tag.get("property") not in ["og:site_name"])
og_tags_str += '\n<meta content="0" property="twitter:image"/>'
og_tags_str += '\n<meta content="player" property="twitter:card"/>'
og_tags_str += '\n<meta content="video/mp4" property="twitter:player:stream:content_type"/>'
og_tags_str += f'\n<meta content="{cdn_video_url}" property="twitter:player:stream"/>'
if og_video_width:
og_tags_str += f'\n<meta content="{og_video_width}" property="twitter:player:width"/>'
if og_video_height:
og_tags_str += f'\n<meta content="{og_video_height}" property="twitter:player:height"/>'
if video_width:
og_tags_str += f'\n<meta content="{video_width}" property="twitter:player:width"/>'
if video_height:
og_tags_str += f'\n<meta content="{video_height}" property="twitter:player:height"/>'
og_tags_str += '\n<meta content="ニコニコ動画" property="og:site_name"/>'
og_tags_str += f'\n<link rel="alternate" href="{get_oembed_url(params)}" type="application/json+oembed" title="{video_id}"/>'
html_response = f"""<!DOCTYPE html>