From 7c80519cbf7e3daa029239fbbd147652817877f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Mon, 20 Jul 2015 21:10:28 +0200 Subject: [PATCH 1/3] [youtube] Extract start_time From the 't=*' in the url. Currently youtube-dl doesn't use the value, but it was requested for the mpv plugin. --- youtube_dl/extractor/common.py | 2 ++ youtube_dl/extractor/youtube.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index b9014fc23e..9e87518778 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -183,6 +183,8 @@ class InfoExtractor(object): ["Sports", "Berlin"] is_live: True, False, or None (=unknown). Whether this video is a live stream that goes on instead of a fixed-length video. + start_time: Time in seconds where the reproduction should start, as + specified in the url. Unless mentioned otherwise, the fields should be Unicode strings. diff --git a/youtube_dl/extractor/youtube.py b/youtube_dl/extractor/youtube.py index 3d8b31f983..afbd34f4ac 100644 --- a/youtube_dl/extractor/youtube.py +++ b/youtube_dl/extractor/youtube.py @@ -19,6 +19,7 @@ from ..compat import ( compat_urllib_parse, compat_urllib_parse_unquote, compat_urllib_parse_unquote_plus, + compat_urllib_parse_urlparse, compat_urllib_request, compat_urlparse, compat_str, @@ -31,6 +32,7 @@ from ..utils import ( get_element_by_id, int_or_none, orderedSet, + parse_duration, str_to_int, unescapeHTML, unified_strdate, @@ -317,7 +319,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): IE_NAME = 'youtube' _TESTS = [ { - 'url': 'http://www.youtube.com/watch?v=BaW_jenozKc', + 'url': 'http://www.youtube.com/watch?v=BaW_jenozKcj&t=1s', 'info_dict': { 'id': 'BaW_jenozKc', 'ext': 'mp4', @@ -329,6 +331,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): 'categories': ['Science & Technology'], 'like_count': int, 'dislike_count': int, + 'start_time': 1, } }, { @@ -889,6 +892,14 @@ class YoutubeIE(YoutubeBaseInfoExtractor): 'http' if self._downloader.params.get('prefer_insecure', False) else 'https') + start_time = None + parsed_url = compat_urllib_parse_urlparse(url) + for component in [parsed_url.fragment, parsed_url.query]: + query = compat_parse_qs(component) + if 't' in query: + start_time = parse_duration(query['t'][0]) + break + # Extract original video URL from URL with redirection, like age verification, using next_url parameter mobj = re.search(self._NEXT_URL_RE, url) if mobj: @@ -1255,6 +1266,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): 'average_rating': float_or_none(video_info.get('avg_rating', [None])[0]), 'formats': formats, 'is_live': is_live, + 'start_time': start_time, } From 297a564beeb20ca8b00d94f5707532110631f409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Thu, 23 Jul 2015 13:20:21 +0200 Subject: [PATCH 2/3] [youtube] Extract end_time --- youtube_dl/extractor/common.py | 2 ++ youtube_dl/extractor/youtube.py | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index 9e87518778..1272834c51 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -185,6 +185,8 @@ class InfoExtractor(object): live stream that goes on instead of a fixed-length video. start_time: Time in seconds where the reproduction should start, as specified in the url. + end_time: Time in seconds where the reproduction should end, as + specified in the url. Unless mentioned otherwise, the fields should be Unicode strings. diff --git a/youtube_dl/extractor/youtube.py b/youtube_dl/extractor/youtube.py index afbd34f4ac..117ef2e776 100644 --- a/youtube_dl/extractor/youtube.py +++ b/youtube_dl/extractor/youtube.py @@ -319,7 +319,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): IE_NAME = 'youtube' _TESTS = [ { - 'url': 'http://www.youtube.com/watch?v=BaW_jenozKcj&t=1s', + 'url': 'http://www.youtube.com/watch?v=BaW_jenozKcj&t=1s&end=9', 'info_dict': { 'id': 'BaW_jenozKc', 'ext': 'mp4', @@ -332,6 +332,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): 'like_count': int, 'dislike_count': int, 'start_time': 1, + 'end_time': 9, } }, { @@ -893,12 +894,14 @@ class YoutubeIE(YoutubeBaseInfoExtractor): else 'https') start_time = None + end_time = None parsed_url = compat_urllib_parse_urlparse(url) for component in [parsed_url.fragment, parsed_url.query]: query = compat_parse_qs(component) - if 't' in query: + if start_time is None and 't' in query: start_time = parse_duration(query['t'][0]) - break + if end_time is None and 'end' in query: + end_time = parse_duration(query['end'][0]) # Extract original video URL from URL with redirection, like age verification, using next_url parameter mobj = re.search(self._NEXT_URL_RE, url) @@ -1267,6 +1270,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): 'formats': formats, 'is_live': is_live, 'start_time': start_time, + 'end_time': end_time, } From 2929fa0e79dfd3a1366e7e23eb4344bc93dd3a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Thu, 23 Jul 2015 13:21:18 +0200 Subject: [PATCH 3/3] [youtube] Also look into the 'start' field for start_time --- youtube_dl/extractor/youtube.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/youtube_dl/extractor/youtube.py b/youtube_dl/extractor/youtube.py index 117ef2e776..462d244d88 100644 --- a/youtube_dl/extractor/youtube.py +++ b/youtube_dl/extractor/youtube.py @@ -900,6 +900,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor): query = compat_parse_qs(component) if start_time is None and 't' in query: start_time = parse_duration(query['t'][0]) + if start_time is None and 'start' in query: + start_time = parse_duration(query['start'][0]) if end_time is None and 'end' in query: end_time = parse_duration(query['end'][0])