Skip to content

Commit

Permalink
- Add support alternative DASH format
Browse files Browse the repository at this point in the history
  • Loading branch information
KOL committed Feb 16, 2016
1 parent 17b68fe commit 7e7541a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
1 change: 0 additions & 1 deletion Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,6 @@ def ApiGetChannelInfo(uid):

if res and res['items']:
res = res['items'][0]
Log.Debug(res)
ret['playlists'] = res['contentDetails']['relatedPlaylists']
try:
ret['banner'] = res['brandingSettings']['image']['bannerTvHighImageUrl']
Expand Down
58 changes: 46 additions & 12 deletions Contents/Services/Shared Code/video.pys
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ RESOLUTIONS = {
22: 720,
18: 360,
36: 240,
160: 144,
133: 240,
134: 360,
135: 480,
136: 720,
298: 720.60,
137: 1080,
299: 1080.60,
}

AUDIO = {
141: 256,
140: 128,
139: 48,
}

MAX_RESOLUTION = 22
Expand All @@ -59,22 +73,38 @@ def GetServiceURL(vid, token='', hl=''):


def GetMetaUrlByServiceURL(url):
return 'https://m.youtube.com/watch?ajax=1&v=%s' % GetOID(url)
return 'https://www.youtube.com/watch?v=%s&spf=navigate' % GetOID(url)


def GetVideoData(url):
try:
res = JSON.ObjectFromString(HTTP.Request(
GetMetaUrlByServiceURL(url),
headers={
'User-Agent': USER_AGENT
}
).content[4:])['content']

res = JSON.ObjectFromURL(GetMetaUrlByServiceURL(url))
except:
raise Ex.MediaNotAvailable

return res
ret = {}
for item in res:
if 'data' in item and 'swfcfg' in item['data']:
ret.update(item['data'])
elif 'body' in item and 'watch7-container' in item['body']:
try:
cont = HTML.ElementFromString(item['body']['watch7-container'])
ret['date_published'] = cont.xpath(
'//meta[@itemprop="datePublished"]'
)[0].get('content')
cont = cont.xpath(
'//div[@id="watch-description-text"]'
)[0]
for br in cont.xpath('*//br'):
br.tail = '\n' + br.tail if br.tail else '\n'
ret['description'] = cont.text_content()
except:
pass

if ret:
return ret

raise Ex.MediaNotAvailable


def GetOID(url):
Expand Down Expand Up @@ -128,13 +158,17 @@ def GetVideoUrls(url):

# Normal video
elif 'url_encoded_fmt_stream_map' in meta:

for item in meta['url_encoded_fmt_stream_map'].split(','):
# High definition
key = 'adaptive_fmts' if 'adaptive_fmts' in meta else 'url_encoded_fmt_stream_map'
for item in meta[key].split(','):
item = dict(map(
lambda x: (x[0], x[1][0]),
String.ParseQueryString(item).items()
))
ret[int(item['itag'])] = GetUrlFromStream(item, player_url)
itag = int(item['itag'])
if itag in RESOLUTIONS or itag in AUDIO:
ret[itag] = GetUrlFromStream(item, player_url)

# Fresh uploaded video
else:
try:
Expand Down
26 changes: 16 additions & 10 deletions Contents/Services/URL/YouTubeTV/ServiceCode.pys
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ def MetadataObjectForURL(url):
if not meta:
raise Ex.MediaNotAvailable

thumb = item['video']['thumbnail_for_watch']
thumb = meta['iurl']
if thumb[0] == '/':
thumb = 'http:%s' % thumb

return VideoClipObject(
title=u'%s' % item['video']['title'],
title=u'%s' % meta['title'],
rating_key=url.split('&')[0],
summary=u'%s / %s\n%s' % (
Video.GetFromMainContent(item, 'short_byline_text'),
Video.GetFromMainContent(item, 'date_text'),
Video.GetFromMainContent(item, 'description'),
summary=u'%s\n%s' % (
meta['author'],
item['description'] if 'description' in item else '',
),
thumb=thumb,
rating=(float(meta['avg_rating'])*2),
tags=meta['keywords'].split(','),
duration=(item['video']['length_seconds']*1000) if item[
'video'
]['length_seconds'] else None,
duration=int(meta['length_seconds'])*1000 if meta['length_seconds'] else None,
originally_available_at=Datetime.ParseDate(
item['date_published']
) if 'date_published' in item else None,
)


Expand Down Expand Up @@ -167,8 +167,14 @@ def PlayVideo(url, fmt):

fmt = int(fmt)
if fmt not in info:
fmt = sorted(info, key=lambda k: Video.RESOLUTIONS[k], reverse=True)[0]
Log.Debug('Get default format')
for fmt, v in sorted(
Video.RESOLUTIONS.items(),
lambda x,y: cmp(x[1], y[1]),
reverse=True
):
if fmt in info:
break

Log.Debug('Play itag: %d' % fmt)

Expand Down

0 comments on commit 7e7541a

Please sign in to comment.