From 7c9c129ee8f3024033cd41521064376d359fa41e Mon Sep 17 00:00:00 2001 From: girst Date: Fri, 25 Sep 2020 16:41:08 +0200 Subject: [PATCH] support "?t=1h2m3s" and "?t=123s" style start-offsets documentation on media fragments: https://www.w3.org/TR/2012/REC-media-frags-20120925/ --- app/youtube/__init__.py | 12 ++++++++++++ app/youtube/templates/watch.html.j2 | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/youtube/__init__.py b/app/youtube/__init__.py index 37cdc19..1229206 100644 --- a/app/youtube/__init__.py +++ b/app/youtube/__init__.py @@ -354,3 +354,15 @@ def format_time(i): m = i // 60 % 60 s = i % 60 return '%d:%02d:%02d' % (h,m,s) if h else '%02d:%02d' % (m,s) + +@frontend.app_template_filter('timeoffset') +def timeoffset(s): + if s is None: + return None + match = re.match(r"^(\d+)s?$", s) # e.g. 2040s + if match: + return match.group(1) + match = re.match(r"^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$", s) # e.g. 34m, 1h23s + if match: + return ":".join([n.zfill(2) for n in match.groups('0')]) + return None diff --git a/app/youtube/templates/watch.html.j2 b/app/youtube/templates/watch.html.j2 index 1cf7db1..a89e78f 100644 --- a/app/youtube/templates/watch.html.j2 +++ b/app/youtube/templates/watch.html.j2 @@ -7,7 +7,7 @@ {% if video_url %}