diff options
author | Shaun <shaun@bluebit.com.au> | 2017-11-25 15:04:26 +1100 |
---|---|---|
committer | Shaun <shaun@bluebit.com.au> | 2017-11-25 15:04:26 +1100 |
commit | 362d0e0c342f9e17734dab8b4d102d55677ec412 (patch) | |
tree | 5db442ecb767b9426a11de518e14e0838f063bf8 /plugin.video.embycon/resources/lib/play_utils.py | |
parent | c2d65f62c77f819face074a31be24f782e15f8fa (diff) |
[plugin.video.embycon] 1.3.47
Diffstat (limited to 'plugin.video.embycon/resources/lib/play_utils.py')
-rw-r--r-- | plugin.video.embycon/resources/lib/play_utils.py | 151 |
1 files changed, 148 insertions, 3 deletions
diff --git a/plugin.video.embycon/resources/lib/play_utils.py b/plugin.video.embycon/resources/lib/play_utils.py index 8e579f1..e6455c5 100644 --- a/plugin.video.embycon/resources/lib/play_utils.py +++ b/plugin.video.embycon/resources/lib/play_utils.py @@ -7,11 +7,12 @@ import xbmcaddon from datetime import timedelta import time import json +import hashlib from simple_logging import SimpleLogging from downloadutils import DownloadUtils from resume_dialog import ResumeDialog -from utils import PlayUtils, getArt +from utils import PlayUtils, getArt, id_generator from kodi_utils import HomeWindow from translation import i18n from json_rpc import json_rpc @@ -37,6 +38,7 @@ def playFile(play_info): jsonData = downloadUtils.downloadUrl("{server}/emby/Users/{userid}/Items/" + id + "?format=json", suppress=False, popup=1) result = json.loads(jsonData) + log.debug("Playfile item info: " + str(result)) seekTime = 0 auto_resume = int(auto_resume) @@ -80,6 +82,8 @@ def playFile(play_info): listitem_props = [] playurl = None + play_session_id = id_generator() + log.debug("play_session_id: %s" % play_session_id) # check if strm file, path will contain contain strm contents if result.get('MediaSources'): @@ -88,7 +92,7 @@ def playFile(play_info): playurl, listitem_props = PlayUtils().getStrmDetails(result) if not playurl: - playurl, playback_type = PlayUtils().getPlayUrl(id, result, force_transcode) + playurl, playback_type = PlayUtils().getPlayUrl(id, result, force_transcode, play_session_id) log.debug("Play URL: " + playurl + " ListItem Properties: " + str(listitem_props)) @@ -100,6 +104,7 @@ def playFile(play_info): home_window = HomeWindow() home_window.setProperty("PlaybackType_" + id, playback_type_string) + home_window.setProperty("PlaySessionId_" + id, play_session_id) # add the playback type into the overview if result.get("Overview", None) is not None: @@ -120,8 +125,16 @@ def playFile(play_info): episode_num = "" item_title = episode_num + " - " + item_title - list_item = xbmcgui.ListItem(label=item_title, path=playurl) + list_item = xbmcgui.ListItem(label=item_title) + # if transcoding then prompt for audio and subtitle + if playback_type == "2": + playurl = audioSubsPref(playurl, list_item, result) + log.debug("New playurl for transcoding : " + playurl) + elif playback_type == "1": + externalSubs(result, list_item) + + list_item.setPath(playurl) list_item = setListItemProps(id, list_item, result, server, listitem_props, item_title) playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) @@ -184,3 +197,135 @@ def setListItemProps(id, listItem, result, server, extra_props, title): listItem.setInfo("Video", infoLabels=details) return listItem + +# For transcoding only +# Present the list of audio and subtitles to select from +# for external streamable subtitles add the URL to the Kodi item and let Kodi handle it +# else ask for the subtitles to be burnt in when transcoding +def audioSubsPref(url, list_item, emby_item): + + dialog = xbmcgui.Dialog() + audioStreamsList = {} + audioStreams = [] + audioStreamsChannelsList = {} + subtitleStreamsList = {} + subtitleStreams = ['No subtitles'] + downloadableStreams = [] + selectAudioIndex = "" + selectSubsIndex = "" + playurlprefs = "%s" % url + + try: + mediasources = emby_item['MediaSources'][0] + mediastreams = mediasources['MediaStreams'] + except (TypeError, KeyError, IndexError): + return + + for stream in mediastreams: + # Since Emby returns all possible tracks together, have to sort them. + index = stream['Index'] + + if 'Audio' in stream['Type']: + codec = stream['Codec'] + channelLayout = stream.get('ChannelLayout', "") + + try: + track = "%s - %s - %s %s" % (index, stream['Language'], codec, channelLayout) + except: + track = "%s - %s %s" % (index, codec, channelLayout) + + audioStreamsChannelsList[index] = stream['Channels'] + audioStreamsList[track] = index + audioStreams.append(track) + + elif 'Subtitle' in stream['Type']: + try: + track = "%s - %s" % (index, stream['Language']) + except: + track = "%s - %s" % (index, stream['Codec']) + + default = stream['IsDefault'] + forced = stream['IsForced'] + downloadable = stream['IsTextSubtitleStream'] and stream['IsExternal'] and stream['SupportsExternalStream'] + + if default: + track = "%s - Default" % track + if forced: + track = "%s - Forced" % track + if downloadable: + downloadableStreams.append(index) + + subtitleStreamsList[track] = index + subtitleStreams.append(track) + + if len(audioStreams) > 1: + resp = dialog.select(i18n('select_audio_stream'), audioStreams) + if resp > -1: + # User selected audio + selected = audioStreams[resp] + selectAudioIndex = audioStreamsList[selected] + playurlprefs += "&AudioStreamIndex=%s" % selectAudioIndex + else: # User backed out of selection + playurlprefs += "&AudioStreamIndex=%s" % mediasources['DefaultAudioStreamIndex'] + else: # There's only one audiotrack. + selectAudioIndex = audioStreamsList[audioStreams[0]] + playurlprefs += "&AudioStreamIndex=%s" % selectAudioIndex + + if len(subtitleStreams) > 1: + resp = dialog.select(i18n('select_subtitle'), subtitleStreams) + if resp == 0: + # User selected no subtitles + pass + elif resp > -1: + # User selected subtitles + selected = subtitleStreams[resp] + selectSubsIndex = subtitleStreamsList[selected] + + # Load subtitles in the listitem if downloadable + if selectSubsIndex in downloadableStreams: + + itemid = emby_item['Id'] + url = [("%s/Videos/%s/%s/Subtitles/%s/Stream.srt" + % (downloadUtils.getServer(), itemid, itemid, selectSubsIndex))] + log.debug("Streaming subtitles url: %s %s" % (selectSubsIndex, url)) + list_item.setSubtitles(url) + else: + # Burn subtitles + playurlprefs += "&SubtitleStreamIndex=%s" % selectSubsIndex + + else: # User backed out of selection + playurlprefs += "&SubtitleStreamIndex=%s" % mediasources.get('DefaultSubtitleStreamIndex', "") + + # Get number of channels for selected audio track + audioChannels = audioStreamsChannelsList.get(selectAudioIndex, 0) + if audioChannels > 2: + playurlprefs += "&AudioBitrate=384000" + else: + playurlprefs += "&AudioBitrate=192000" + + return playurlprefs + +# direct stream, set any available subtitle streams +def externalSubs(emby_item, list_item): + + externalsubs = [] + itemid = emby_item['Id'] + try: + mediastreams = emby_item['MediaSources'][0]['MediaStreams'] + except (TypeError, KeyError, IndexError): + return + + for stream in mediastreams: + + if (stream['Type'] == "Subtitle" + and stream['IsExternal'] + and stream['IsTextSubtitleStream'] + and stream['SupportsExternalStream']): + + index = stream['Index'] + url = ("%s/Videos/%s/%s/Subtitles/%s/Stream.%s" + % (downloadUtils.getServer(), itemid, itemid, index, stream['Codec'])) + + externalsubs.append(url) + + list_item.setSubtitles(externalsubs)
\ No newline at end of file |