def convert_vtt_to_srt(dir): for vtt_file in glob.glob(os.path.join(dir, "*.vtt")): with open(os.path.splitext(vtt_file)[0] + '.srt', 'w') as srt: vtt = open(vtt_file, 'r') vttsub = vtt.read().decode('UTF-8') srtsub = SRTWriter().write(WebVTTReader().read(vttsub)) srt.write(srtsub.encode('UTF-8')) vtt.close() os.remove(vtt_file)
def play(url): try: params = utils.get_url(url) p = comm.get_program(params["program_id"]) listitem = xbmcgui.ListItem( label=p.get_title(), iconImage=p.thumbnail, thumbnailImage=p.thumbnail, path=p.get_url() ) listitem.setInfo("video", p.get_xbmc_list_item()) if hasattr(listitem, "addStreamInfo"): listitem.addStreamInfo("audio", p.get_xbmc_audio_stream_info()) listitem.addStreamInfo("video", p.get_xbmc_video_stream_info()) player = xbmc.Player() # Pull subtitles if available if addon.getSetting("subtitles_enabled") == "true": if p.subtitle: utils.log("Enabling subtitles: %s" % p.subtitle) profile = addon.getAddonInfo("profile") subfilename = xbmc.translatePath(os.path.join(profile, "subtitle.srt")) profiledir = xbmc.translatePath(os.path.join(profile)) if not os.path.isdir(profiledir): os.makedirs(profiledir) dfxp_data = urllib2.urlopen(p.subtitle).read().decode("utf-8") if dfxp_data: f = open(subfilename, "w") dfxp_subtitle = DFXPReader().read(dfxp_data) srt_subtitle = SRTWriter().write(dfxp_subtitle) srt_unicode = srt_subtitle.encode("utf-8") f.write(srt_unicode) f.close() if hasattr(listitem, "setSubtitles"): # This function only supported from Kodi v14+ listitem.setSubtitles([subfilename]) # Play video utils.log("Attempting to play: %s" % p.get_title()) xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem=listitem) # Enable subtitles for XBMC v13 if addon.getSetting("subtitles_enabled") == "true": if p.subtitle: if not hasattr(listitem, "setSubtitles"): while not player.isPlaying(): xbmc.sleep(100) # wait until video is being played player.setSubtitles(subfilename) except: utils.handle_error("Unable to play video")
def download_subtitle(url, destination): if not url: return False r = requests.get(url) if not r.ok: return False reader = detect_format(r.text) if not reader: return False srt = SRTWriter().write(reader().read(r.text)) if xbmcvfs.exists(destination): xbmcvfs.delete(destination) f = xbmcvfs.File(destination, 'w') f.write(srt.encode("utf-8")) f.close() return True
def play(url): try: # Remove cookies.dat for Kodi < 17.0 - causes issues with playback addon = xbmcaddon.Addon() cookies_dat = xbmc.translatePath('special://home/cache/cookies.dat') if os.path.isfile(cookies_dat): os.remove(cookies_dat) p = classes.Program() p.parse_kodi_url(url) stream_data = comm.get_stream_url(p.get_house_number(), p.get_url()) stream_url = stream_data.get('stream_url') if not stream_url: utils.log('Not Playable: {0}'.format(repr(stream_data))) raise AussieAddonsException( 'Not available: {0}\n{1}'.format(stream_data.get('msg'), stream_data.get( 'availability'))) use_ia = addon.getSetting('USE_IA') == 'true' if use_ia: if addon.getSetting('IGNORE_DRM') == 'false': try: import drmhelper if not drmhelper.check_inputstream(drm=False): return except ImportError: utils.log("Failed to import drmhelper") utils.dialog_message( 'DRM Helper is needed for inputstream.adaptive ' 'playback. Disable "Use inputstream.adaptive for ' 'playback" in settings or install drmhelper. For ' 'more information, please visit: ' 'http://aussieaddons.com/drm') return hdrs = stream_url[stream_url.find('|') + 1:] listitem = xbmcgui.ListItem(label=p.get_list_title(), path=stream_url) thumb = p.get_thumb() listitem.setArt({'icon': thumb, 'thumb': thumb}) if use_ia: listitem.setProperty('inputstreamaddon', 'inputstream.adaptive') listitem.setProperty('inputstream.adaptive.manifest_type', 'hls') listitem.setProperty('inputstream.adaptive.stream_headers', hdrs) listitem.setProperty('inputstream.adaptive.license_key', stream_url) listitem.setInfo('video', p.get_kodi_list_item()) # Add subtitles if available if p.is_captions(): captions_url = stream_data.get('captions_url') profile = xbmcaddon.Addon().getAddonInfo('profile') path = xbmc.translatePath(profile) if not os.path.isdir(path): os.makedirs(path) caption_file = os.path.join(path, 'subtitles.eng.srt') if os.path.isfile(caption_file): os.remove(caption_file) try: sess = session.Session() webvtt_data = sess.get(captions_url).text if webvtt_data: with io.BytesIO() as buf: webvtt_captions = WebVTTReader().read(webvtt_data) srt_captions = SRTWriter().write(webvtt_captions) srt_unicode = srt_captions.encode('utf-8') buf.write(srt_unicode) with io.open(caption_file, "wb") as f: f.write(buf.getvalue()) if hasattr(listitem, 'setSubtitles'): listitem.setSubtitles([caption_file]) except Exception as e: utils.log( 'Subtitles not available for this program: {0}'.format(e)) if hasattr(listitem, 'addStreamInfo'): listitem.addStreamInfo('audio', p.get_kodi_audio_stream_info()) listitem.addStreamInfo('video', p.get_kodi_video_stream_info()) xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem=listitem) except Exception: utils.handle_error('Unable to play video')
def play_video(params): """ Determine content and pass url to Kodi for playback """ try: json_url = config.BRIGHTCOVE_DRM_URL.format(config.BRIGHTCOVE_ACCOUNT, params['id']) if params['drm'] == 'True': if xbmcaddon.Addon().getSetting('ignore_drm') == 'false': if not drmhelper.check_inputstream(): return widevine = comm.get_widevine_auth(json_url) url = widevine['url'] sub_url = widevine['sub_url'] play_item = xbmcgui.ListItem(path=url) play_item.setProperty('inputstream.adaptive.manifest_type', 'mpd') play_item.setProperty('inputstream.adaptive.license_type', 'com.widevine.alpha') play_item.setProperty( 'inputstream.adaptive.license_key', widevine['key'] + ('|Content-Type=application%2F' 'x-www-form-urlencoded|A{SSM}|')) else: if params['action'] == 'listchannels': qual = int(xbmcaddon.Addon().getSetting('LIVEQUALITY')) live = True else: qual = int(xbmcaddon.Addon().getSetting('HLSQUALITY')) live = False stream_data = comm.get_stream(json_url, live=live) m3u8 = stream_data.get('url') sub_url = stream_data.get('sub_url') url = parse_m3u8(m3u8, qual=qual, live=live) play_item = xbmcgui.ListItem(path=url) utils.log('Playing {0} - {1}'.format(params.get('title'), url)) if sub_url: try: utils.log("Enabling subtitles: {0}".format(sub_url)) profile = xbmcaddon.Addon().getAddonInfo('profile') subfilename = xbmc.translatePath( os.path.join(profile, 'subtitle.srt')) profiledir = xbmc.translatePath(os.path.join(profile)) if not os.path.isdir(profiledir): os.makedirs(profiledir) with custom_session.Session() as s: webvtt_data = s.get(sub_url).text if webvtt_data: with open(subfilename, 'w') as f: webvtt_subtitle = WebVTTReader().read(webvtt_data) srt_subtitle = SRTWriter().write(webvtt_subtitle) srt_unicode = srt_subtitle.encode('utf-8') f.write(srt_unicode) if hasattr(play_item, 'setSubtitles'): # This function only supported from Kodi v14+ play_item.setSubtitles([subfilename]) except Exception as e: utils.log('Unable to add subtitles: {0}'.format(e)) xbmcplugin.setResolvedUrl(_handle, True, play_item) except Exception: utils.handle_error('Unable to play video')
def play_video(params): """ Determine content and pass url to Kodi for playback """ try: _url = sys.argv[0] _handle = int(sys.argv[1]) json_url = config.BRIGHTCOVE_DRM_URL.format(config.BRIGHTCOVE_ACCOUNT, params['id']) play_item = xbmcgui.ListItem() play_item.setProperty('inputstreamaddon', 'inputstream.adaptive') play_item.setProperty('inputstream', 'inputstream.adaptive') if params.get('drm') == 'True': if xbmcaddon.Addon().getSetting('ignore_drm') == 'false': if not drmhelper.check_inputstream(): return widevine = comm.get_widevine_auth(json_url) url = widevine['url'] sub_url = widevine['sub_url'] play_item.setPath(url) play_item.setProperty('inputstream.adaptive.manifest_type', 'mpd') play_item.setProperty('inputstream.adaptive.license_type', 'com.widevine.alpha') play_item.setProperty( 'inputstream.adaptive.license_key', widevine['key'] + ('|Content-Type=application%2F' 'x-www-form-urlencoded|A{SSM}|')) else: live = params['action'] == 'listchannels' stream_data = comm.get_stream(json_url, live=live) url = str(stream_data.get('url')) sub_url = stream_data.get('sub_url') play_item.setPath(url) play_item.setProperty('inputstream.adaptive.manifest_type', 'hls') utils.log('Playing {0} - {1}'.format(params.get('title'), url)) if sub_url: try: utils.log("Enabling subtitles: {0}".format(sub_url)) profile = xbmcaddon.Addon().getAddonInfo('profile') subfilename = xbmc.translatePath( os.path.join(profile, 'subtitle.srt')) profiledir = xbmc.translatePath(os.path.join(profile)) if not os.path.isdir(profiledir): os.makedirs(profiledir) with custom_session.Session() as s: webvtt_data = s.get(sub_url).text if webvtt_data: with open(subfilename, 'w') as f: webvtt_subtitle = WebVTTReader().read(webvtt_data) srt_subtitle = SRTWriter().write(webvtt_subtitle) srt_unicode = srt_subtitle.encode('utf-8') f.write(srt_unicode) if hasattr(play_item, 'setSubtitles'): # This function only supported from Kodi v14+ play_item.setSubtitles([subfilename]) except Exception as e: utils.log('Unable to add subtitles: {0}'.format(e)) play_item.setProperty('isPlayable', 'true') if hasattr(play_item, 'setIsFolder'): play_item.setIsFolder(False) # TODO: add more info play_item.setInfo( 'video', { 'mediatype': 'episode', 'tvshowtitle': params.get('series_title', ''), 'title': params.get('episode_name', ''), 'plot': params.get('desc', ''), 'plotoutline': params.get('desc', ''), 'duration': params.get('duration', ''), 'aired': params.get('airdate', ''), 'season': params.get('season_no', ''), 'episode': params.get('episode_no', '') }) xbmcplugin.setResolvedUrl(_handle, True, play_item) if params['action'] != 'listepisodes': return next_item = comm.get_next_episode(params) if not next_item: return try: import upnext except Exception as e: utils.log('UpNext addon not installed: %s' % e) return upnext_info = dict(current_episode=dict( episodeid=params.get('id', ''), tvshowid=params.get('series_slug', ''), title=params.get('episode_name', ''), art={ 'thumb': params.get('thumb', ''), 'tvshow.fanart': params.get('fanart', ''), }, season=params.get('season_no', ''), episode=params.get('episode_no', ''), showtitle=params.get('series_title', ''), plot=params.get('desc', ''), playcount=0, rating=None, firstaired=params.get('airdate', ''), runtime=params.get('duration', ''), ), next_episode=dict( episodeid=next_item.id, tvshowid=next_item.series_slug, title=next_item.episode_name, art={ 'thumb': next_item.thumb, 'tvshow.fanart': next_item.fanart, }, season=next_item.season_no, episode=next_item.episode_no, showtitle=next_item.series_title, plot=next_item.desc, playcount=0, rating=None, firstaired=next_item.airdate, runtime=next_item.duration, ), play_url='{0}?action=listepisodes{1}'.format( _url, next_item.make_kodi_url())) upnext.send_signal(xbmcaddon.Addon().getAddonInfo('id'), upnext_info) except Exception: utils.handle_error('Unable to play video')
def play(url): try: # Remove cookies.dat for Kodi < 17.0 - causes issues with playback addon = xbmcaddon.Addon() cookies_dat = xbmc.translatePath('special://home/cache/cookies.dat') if os.path.isfile(cookies_dat): os.remove(cookies_dat) p = classes.Program() p.parse_xbmc_url(url) stream = comm.get_stream_url(p.get_house_number(), p.get_url()) use_ia = addon.getSetting('use_ia') == 'true' if use_ia: if addon.getSetting('ignore_drm') == 'false': try: import drmhelper if not drmhelper.check_inputstream(drm=False): return except ImportError: utils.log("Failed to import drmhelper") utils.dialog_message( 'DRM Helper is needed for inputstream.adaptive ' 'playback. Disable "Use inputstream.adaptive for ' 'playback" in settings or install drmhelper. For ' 'more information, please visit: ' 'http://aussieaddons.com/drm') return hdrs = stream[stream.find('|') + 1:] listitem = xbmcgui.ListItem(label=p.get_list_title(), iconImage=p.thumbnail, thumbnailImage=p.thumbnail, path=stream) if use_ia: listitem.setProperty('inputstreamaddon', 'inputstream.adaptive') listitem.setProperty('inputstream.adaptive.manifest_type', 'hls') listitem.setProperty('inputstream.adaptive.stream_headers', hdrs) listitem.setProperty('inputstream.adaptive.license_key', stream) listitem.setInfo('video', p.get_kodi_list_item()) # Add subtitles if available if p.subtitle_url: profile = xbmcaddon.Addon().getAddonInfo('profile') path = xbmc.translatePath(profile).decode('utf-8') if not os.path.isdir(path): os.makedirs(path) subfile = xbmc.translatePath( os.path.join(path, 'subtitles.eng.srt')) if os.path.isfile(subfile): os.remove(subfile) try: webvtt_data = urllib2.urlopen( p.subtitle_url).read().decode('utf-8') if webvtt_data: with open(subfile, 'w') as f: webvtt_subtitle = WebVTTReader().read(webvtt_data) srt_subtitle = SRTWriter().write(webvtt_subtitle) srt_unicode = srt_subtitle.encode('utf-8') f.write(srt_unicode) if hasattr(listitem, 'setSubtitles'): listitem.setSubtitles([subfile]) except Exception as e: utils.log( 'Subtitles not available for this program {0}'.format(e)) if hasattr(listitem, 'addStreamInfo'): listitem.addStreamInfo('audio', p.get_kodi_audio_stream_info()) listitem.addStreamInfo('video', p.get_kodi_video_stream_info()) xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem=listitem) except Exception: utils.handle_error('Unable to play video')
def play(params): try: p = comm.get_program(params) # enable HD streams if ADDON.getSetting('hd_enabled') == 'true': if p.dash_url: if '&rule=sd-only' in p.dash_url: p.dash_url = p.dash_url.replace('&rule=sd-only', '') if p.hls_url: if '&rule=sd-only' in p.hls_url: p.hls_url = p.hls_url.replace('&rule=sd-only', '') listitem = xbmcgui.ListItem(label=p.get_title(), path=p.get_url()) listitem.setInfo('video', p.get_kodi_list_item()) if hasattr(listitem, 'addStreamInfo'): listitem.addStreamInfo('audio', p.get_kodi_audio_stream_info()) listitem.addStreamInfo('video', p.get_kodi_video_stream_info()) if (p.dash_preferred and p.dash_url) or not p.hls_url: import drmhelper drm = p.drm_key is not None if drmhelper.check_inputstream(drm=drm): listitem.setProperty('inputstreamaddon', 'inputstream.adaptive') listitem.setProperty('inputstream.adaptive.manifest_type', 'mpd') if drm: listitem.setProperty('inputstream.adaptive.license_type', 'com.widevine.alpha') listitem.setProperty( 'inputstream.adaptive.license_key', p.drm_key+'|Content-Type=application%2F' 'x-www-form-urlencoded|A{SSM}|') else: if drm: xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, xbmcgui.ListItem(path=None)) return else: pass # let's try to play hls if available # Pull subtitles if available if p.subtitle: utils.log('Enabling subtitles: {0}'.format(p.subtitle)) profile = ADDON.getAddonInfo('profile') subfilename = xbmc.translatePath(os.path.join(profile, 'subtitle.srt')) profiledir = xbmc.translatePath(os.path.join(profile)) if not os.path.isdir(profiledir): os.makedirs(profiledir) webvtt_data = session.Session().get( p.subtitle).text if webvtt_data: with open(subfilename, 'w') as f: webvtt_subtitle = WebVTTReader().read(webvtt_data) srt_subtitle = SRTWriter().write(webvtt_subtitle) srt_unicode = srt_subtitle.encode('utf-8') f.write(srt_unicode) if hasattr(listitem, 'setSubtitles'): # This function only supported from Kodi v14+ listitem.setSubtitles([subfilename]) # Play video utils.log('Attempting to play: {0} : {1}'.format(p.get_title(), p.get_url())) xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem=listitem) except Exception: utils.handle_error('Unable to play video')