From fd93723968782430703f4c4d4c608a9f523db66c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Chv=C3=ADla?= <miso.chvila@gmail.com>
Date: Wed, 28 Oct 2020 11:43:47 +0100
Subject: [PATCH 1/2] playutils: Honor EnableSubtitleExtraction server option

* fix bug where subtitle streams that don't support external streaming
  would show up in video player OSD selection as external when transcoding
  (e.g. embedded PGSSUB)
* fix bug where EnableSubtitleExtraction server setting wasn't taken
  into consideration when transcoding
---
 jellyfin_kodi/helper/playutils.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/jellyfin_kodi/helper/playutils.py b/jellyfin_kodi/helper/playutils.py
index 4b85bc37..2f0e2a58 100644
--- a/jellyfin_kodi/helper/playutils.py
+++ b/jellyfin_kodi/helper/playutils.py
@@ -509,8 +509,13 @@ class PlayUtils(object):
         mapping = {}
         kodi = 0
 
+        server_settings = TheVoid('GetTranscodeOptions', {'ServerId': self.info['ServerId']}).get()
+
         for stream in source['MediaStreams']:
-            if stream['Type'] == 'Subtitle' and stream['DeliveryMethod'] == 'External':
+            if stream['SupportsExternalStream'] and stream['Type'] == 'Subtitle' and stream['DeliveryMethod'] == 'External':
+                if not stream['IsExternal'] and not server_settings['EnableSubtitleExtraction']:
+                    continue
+
                 index = stream['Index']
                 url = self.get_subtitles(source, stream, index)
 

From 65b17043657210a315455f4a41a1ee928be2d00a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Chv=C3=ADla?= <miso.chvila@gmail.com>
Date: Wed, 28 Oct 2020 12:11:33 +0100
Subject: [PATCH 2/2] playutils: Show embedded subs in sub track selection
 dialog

* show embedded subtitles in subtitle track selection dialog when transcoding
  if server subtitle extraction is enabled
---
 jellyfin_kodi/helper/playutils.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/jellyfin_kodi/helper/playutils.py b/jellyfin_kodi/helper/playutils.py
index 2f0e2a58..71df7b00 100644
--- a/jellyfin_kodi/helper/playutils.py
+++ b/jellyfin_kodi/helper/playutils.py
@@ -598,6 +598,7 @@ class PlayUtils(object):
         subs_streams = collections.OrderedDict()
         streams = source['MediaStreams']
 
+        server_settings = TheVoid('GetTranscodeOptions', {'ServerId': self.info['ServerId']}).get()
         allow_burned_subs = settings('allowBurnedSubs.bool')
 
         for stream in streams:
@@ -619,9 +620,13 @@ class PlayUtils(object):
                 audio_streams[track] = index
 
             elif stream_type == 'Subtitle':
-                downloadable = stream['IsTextSubtitleStream'] and stream['IsExternal'] and stream['SupportsExternalStream']
-                if not downloadable and not allow_burned_subs:
-                    continue
+                if stream['IsExternal']:
+                    if not stream['SupportsExternalStream'] and not allow_burned_subs:
+                        continue
+                else:
+                    avail_for_extraction = stream['SupportsExternalStream'] and server_settings['EnableSubtitleExtraction']
+                    if not avail_for_extraction and not allow_burned_subs:
+                        continue
 
                 codec = self.get_commercial_codec_name(stream['Codec'], None)
 
@@ -660,7 +665,6 @@ class PlayUtils(object):
         if subtitle:
 
             index = subtitle
-            server_settings = TheVoid('GetTranscodeOptions', {'ServerId': self.info['ServerId']}).get()
             stream = streams[index]
 
             if server_settings['EnableSubtitleExtraction'] and stream['SupportsExternalStream']:
@@ -679,8 +683,6 @@ class PlayUtils(object):
                 index = subs_streams[selection[resp]] if resp > -1 else source.get('DefaultSubtitleStreamIndex')
 
                 if index is not None:
-
-                    server_settings = TheVoid('GetTranscodeOptions', {'ServerId': self.info['ServerId']}).get()
                     stream = streams[index]
 
                     if server_settings['EnableSubtitleExtraction'] and stream['SupportsExternalStream']: