#723 get duration for youtube episodes

This commit is contained in:
auouymous 2020-01-07 01:16:25 -07:00 committed by Eric Le Lay
parent bc1e735ef1
commit 2fd6eb84b1
2 changed files with 16 additions and 9 deletions

View File

@ -68,11 +68,12 @@ class YoutubeCustomDownload(download.CustomDownload):
Actual youtube-dl interaction via gPodderYoutubeDL.
"""
def __init__(self, ytdl, url):
def __init__(self, ytdl, url, episode):
self._ytdl = ytdl
self._url = url
self._reporthook = None
self._prev_dl_bytes = 0
self._episode = episode
def retrieve_resume(self, tempname, reporthook=None):
"""
@ -80,6 +81,8 @@ class YoutubeCustomDownload(download.CustomDownload):
"""
self._reporthook = reporthook
res = self._ytdl.fetch_video(self._url, tempname, self._my_hook)
if 'duration' in res and res['duration']:
self._episode.total_time = res['duration']
headers = {}
# youtube-dl doesn't return a content-type but an extension
if 'ext' in res:
@ -375,9 +378,9 @@ class gPodderYoutubeDL(download.CustomDownloader):
called from registry.custom_downloader.resolve
"""
if re.match(r'''https://www.youtube.com/watch\?v=.+''', episode.url):
return YoutubeCustomDownload(self, episode.url)
return YoutubeCustomDownload(self, episode.url, episode)
elif re.match(r'''https://www.youtube.com/watch\?v=.+''', episode.link):
return YoutubeCustomDownload(self, episode.link)
return YoutubeCustomDownload(self, episode.link, episode)
return None

View File

@ -117,7 +117,9 @@ def get_fmt_ids(youtube_config):
@registry.download_url.register
def youtube_real_download_url(config, episode):
fmt_ids = get_fmt_ids(config.youtube) if config else None
res = get_real_download_url(episode.url, fmt_ids)
res, duration = get_real_download_url(episode.url, fmt_ids)
if duration is not None:
episode.total_time = int(int(duration) / 1000)
return None if res == episode.url else res
@ -125,6 +127,8 @@ def get_real_download_url(url, preferred_fmt_ids=None):
if not preferred_fmt_ids:
preferred_fmt_ids, _, _ = formats_dict[22] # MP4 720p
duration = None
vid = get_youtube_id(url)
if vid is not None:
page = None
@ -163,11 +167,11 @@ def get_real_download_url(url, preferred_fmt_ids=None):
if 'formats' in player_response['streamingData']:
for f in player_response['streamingData']['formats']:
if 'url' in f:
yield int(f['itag']), f['url']
yield int(f['itag']), [f['url'], f.get('approxDurationMs')]
if 'adaptiveFormats' in player_response['streamingData']:
for f in player_response['streamingData']['adaptiveFormats']:
if 'url' in f:
yield int(f['itag']), f['url']
yield int(f['itag']), [f['url'], f.get('approxDurationMs')]
return
if error_message:
@ -178,7 +182,7 @@ def get_real_download_url(url, preferred_fmt_ids=None):
fmt_url_map = urllib.parse.unquote(r4.group(1))
for fmt_url_encoded in fmt_url_map.split(','):
video_info = parse_qs(fmt_url_encoded)
yield int(video_info['itag'][0]), video_info['url'][0]
yield int(video_info['itag'][0]), [video_info['url'][0], None]
fmt_id_url_map = sorted(find_urls(page), reverse=True)
@ -205,12 +209,12 @@ def get_real_download_url(url, preferred_fmt_ids=None):
logger.info('Found YouTube format: %s (fmt_id=%d)',
description, id)
url = fmt_id_url_map[id]
url, duration = fmt_id_url_map[id]
break
else:
raise YouTubeError('No preferred formats found for video ID "%s"' % vid)
return url
return url, duration
def get_youtube_id(url):