Streaming Brain isn't so smart when it comes to Versions

I share my Plex with my large family and it’s not uncommon to have 8 of them streaming something from me at a given time. So i was very excited when streaming limits were introduced. Since that time, i have started to buy 4K UHD discs and rip them into my library. At first i just added them as another Version in my Movies library since that’s exactly what Versions are for. Flash forward a few days and i start to get a few phone calls or text messages from family members complaining that Plex is buffering a lot. I have a pretty beefy computer for PMS and a big upload pipe so this was strange. And then i figured it out…

My family members were trying to watch movies that had both a 1080p and a 4K version. Since i limit my remote users stream rate to 4 Mbps, it was of course transcoding the movies to send it to them. This has never been a problem in the past as it just transcoded the 1080p x264 into a 720p 4Mbps version. But now with a 4K version and a 1080p version, it was transcoding the 4K version down to a 720p version. Because my 4K rips are 50+ Mbps x265 with lossless audio, it was bringing my server to its knees when more than one or two people were watching a movie.

I would have thought that the Streaming Brain would be smart enough to look at what limit is set for remote streams and then use a version that is equal two or just one step better than what it needed to send over the wire. Instead it dumbly takes the highest quality version and transcodes it.

For now i’ve had to separate out my 4K movies into a completely separate library to avoid this problem (which leads to other issues with Dashboards and Now Playing) but it’s my only way of ensuring Plex doesn’t unnecessarily transcode 4K media when a 1080p would have sufficed.

Technically, for quality, it makes sense to start with the best version and transcode from there. So 4k > 720p makes more sense than starting with the inferior 1080p version.

BUT I totally know what you mean with regards to why it would makes sense for the transcoder to ignore the 4k versions just for CPU headroom. As far as I know, even when I have a pre-transcoded 720p version on my server, the Streaming Brain will still transcode from the 1080p version. The only way to actually use to 720p version previously made is to manually select it. (I could be wrong on this part in my limited testing).

@cameron said:
Technically, for quality, it makes sense to start with the best version and transcode from there. So 4k > 720p makes more sense than starting with the inferior 1080p version.

BUT I totally know what you mean with regards to why it would makes sense for the transcoder to ignore the 4k versions just for CPU headroom. As far as I know, even when I have a pre-transcoded 720p version on my server, the Streaming Brain will still transcode from the 1080p version. The only way to actually use to 720p version previously made is to manually select it. (I could be wrong on this part in my limited testing).

Been my experience too ever since the streaming brain was introduced… I’ve been told I’m wrong by devs, but for me it doesn’t use the most sensible version by itself.

So i tested a little more but using Optimize to create a version for TV, version for Mobile, and then a custom version set to 720p. Then i had my sister try streaming a movie that had a 4K, 1080p, TV, Mobile and Custom version. It still transcoded the 4K version down to 720p/4Mbps instead of using any of the optimized versions. The only way it didn’t transocde is if she manually selected Play Version.

Again, streaming brain isn’t smart enough to pick the correct version to stream or transcode.

@cameron said:
Technically, for quality, it makes sense to start with the best version and transcode from there. So 4k > 720p makes more sense than starting with the inferior 1080p version.

It does not make sense to transcode 4k HDR as it looks really weird going down to SDR. I think there should be a knob to control maximum quality transcode… you should be able to select highest or HD and below.

@WatchTowerPlex said:

@cameron said:
Technically, for quality, it makes sense to start with the best version and transcode from there. So 4k > 720p makes more sense than starting with the inferior 1080p version.

It does not make sense to transcode 4k HDR as it looks really weird going down to SDR. I think there should be a knob to control maximum quality transcode… you should be able to select highest or HD and below.

That’s kind of the point. If I have a 4k HDR and a regular 1080p file, the “brain” should use the 1080p to stream/transcode, not the 4k.

Good point about the HDR. When i play any of my 4K HDR on non-HDR displays, the colors are dull and under saturated. I just checked and when i play a 4K HDR movie remotely, it transcodes to a 720p version and the colors are dull.

So color space and the display is another thing the Streaming Brain should take into account when chooosing what to transcode or stream.

For now what I have done is use Tautulli and a script to prevent anyone from transcoding 4k. I have had it implemented for a few weeks and it has always prevented people from transcoding 4k. I also created a guide for users so they can change their settings if they would like to watch 4k.

@WatchTowerPlex said:
For now what I have done is use Tautulli and a script to prevent anyone from transcoding 4k. I have had it implemented for a few weeks and it has always prevented people from transcoding 4k. I also created a guide for users so they can change their settings if they would like to watch 4k.

Would you mind sharing this script with us? It sounds interesting and would be useful as a HOWTO for people interested in implementing such a thing!

Sure here is my setup. Script originally from GitHub - blacktwin/JBOPS: Just a Bunch Of Plex Scripts
I made some minor modifications to it.

In Tautulli




The script

#!/usr/bin/python
"""
Kill Plex transcoding streams from specific libraries
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
        [X] Notify on playback start
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
        Playback Start: kill_trans_library.py
PlexPy > Settings > Notifications > Script > Script Arguments:
        {section_id} {session_key}
"""
import sys
import requests
from plexapi.server import PlexServer

## Implemented a delay because of a race condition that could happen on slow clients##
import time
time.sleep(6) 

## EDIT THESE SETTINGS ##
PLEX_TOKEN = 'XXXXXXXXXXXXXX'
PLEX_URL = 'https://plex.server.net'

TARGET_LIBRARIES = ['1','2'] # Library IDs

DEFAULT_REASON = 'Stream terminated due to video transcoding of 4K content. ' \
                 'Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.'

# Find platforms that have history in PlexPy in Play count by platform and stream type Graph
DEVICES = {'Android': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Chrome': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Plex Media Player': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Chromecast': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.'}

USER_IGNORE = ('') # ('Username','User2')

PLEXPY_LOG = 'Killing {user}\'s stream of {title} due to video transcoding content from section {section}.'
##

sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)

if __name__ == '__main__':

    lib_id = sys.argv[1]
    session_key = int(sys.argv[2])

    for session in plex.sessions():
        username = session.usernames[0]
        media_type = session.type
        section_id = session.librarySectionID
        if username not in USER_IGNORE and media_type != 'track' and lib_id == section_id and session.sessionKey == session_key:
            title = session.title
            if session.transcodeSessions:
                trans_dec = session.transcodeSessions[0].videoDecision
                if trans_dec == 'transcode':
                    reason = DEVICES.get(session.players[0].platform, DEFAULT_REASON)
                    print(PLEXPY_LOG.format(user=username, title=title, section=section_id))
                    session.stop(reason=reason)

I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

1 Like

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

That is just what triggers the script. you can change the trigger to be something like this and that should work… You would just need to make sure that all your libraries were defined in the script. With this condition the script would only be triggered when someone watches the 4k version of a file. The script would kill the stream only if the “video decision” is transcoding.

Edit: wrong screenshot

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

Just include your “regular files” in your 4k library. That’s your full movie library for yourself, and share the non-4k movie library with your remote users.

@KarlDag said:

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

Just include your “regular files” in your 4k library. That’s your full movie library for yourself, and share the non-4k movie library with your remote users.

What that really does is make me maintain a separate library for shared users. Thats not a great solution. I’d just prefer that Plex not use 4k media when a lower quality version will suffice.

1 Like

@rcork said:

@KarlDag said:

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

Just include your “regular files” in your 4k library. That’s your full movie library for yourself, and share the non-4k movie library with your remote users.

What that really does is make me maintain a separate library for shared users. Thats not a great solution. I’d just prefer that Plex not use 4k media when a lower quality version will suffice.

Of course but you already maintain 2 libraries… at least you’d have a complete one for yourself.

@KarlDag said:

@rcork said:

@KarlDag said:

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

Just include your “regular files” in your 4k library. That’s your full movie library for yourself, and share the non-4k movie library with your remote users.

What that really does is make me maintain a separate library for shared users. Thats not a great solution. I’d just prefer that Plex not use 4k media when a lower quality version will suffice.

Of course but you already maintain 2 libraries… at least you’d have a complete one for yourself.

But i only maintain two libraries because of this issue. :smiley:

@rcork said:

@KarlDag said:

@rcork said:

@KarlDag said:

@rcork said:
I already have my 4k separated into a separate library and i just dont give access to that library to anyone but myself. I would prefer to keep the 4k content in my main movie library and prevent people from streaming the 4k versions. Is there a script that would deny streaming of a particular version, not a specific library?

Just include your “regular files” in your 4k library. That’s your full movie library for yourself, and share the non-4k movie library with your remote users.

What that really does is make me maintain a separate library for shared users. Thats not a great solution. I’d just prefer that Plex not use 4k media when a lower quality version will suffice.

Of course but you already maintain 2 libraries… at least you’d have a complete one for yourself.

But i only maintain two libraries because of this issue. :smiley:

I agree… maintaining 2 libraries is not ideal. There should be some logic in Plex.

I use 2 libraries and the script mentioned just to make sure users do not waste server resources.

@Samwiseg0 said:
Sure here is my setup. Script originally from GitHub - blacktwin/JBOPS: Just a Bunch Of Plex Scripts
I made some minor modifications to it.

In Tautulli




The script

#!/usr/bin/python
"""
Kill Plex transcoding streams from specific libraries
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
        [X] Notify on playback start
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
        Playback Start: kill_trans_library.py
PlexPy > Settings > Notifications > Script > Script Arguments:
        {section_id} {session_key}
"""
import sys
import requests
from plexapi.server import PlexServer

## Implemented a delay because of a race condition that could happen on slow clients##
import time
time.sleep(6) 

## EDIT THESE SETTINGS ##
PLEX_TOKEN = 'XXXXXXXXXXXXXX'
PLEX_URL = 'https://plex.server.net'

TARGET_LIBRARIES = ['1','2'] # Library IDs

DEFAULT_REASON = 'Stream terminated due to video transcoding of 4K content. ' \
                 'Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.'

# Find platforms that have history in PlexPy in Play count by platform and stream type Graph
DEVICES = {'Android': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Chrome': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Plex Media Player': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.',
           'Chromecast': 'Stream terminated due to video transcoding of 4K content. Please set your device to use "Original/Maximum" quality. If it is set to "Original/Maximum" your deivce is not capable of 4k.'}

USER_IGNORE = ('') # ('Username','User2')

PLEXPY_LOG = 'Killing {user}\'s stream of {title} due to video transcoding content from section {section}.'
##

sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)

if __name__ == '__main__':

    lib_id = sys.argv[1]
    session_key = int(sys.argv[2])

    for session in plex.sessions():
        username = session.usernames[0]
        media_type = session.type
        section_id = session.librarySectionID
        if username not in USER_IGNORE and media_type != 'track' and lib_id == section_id and session.sessionKey == session_key:
            title = session.title
            if session.transcodeSessions:
                trans_dec = session.transcodeSessions[0].videoDecision
                if trans_dec == 'transcode':
                    reason = DEVICES.get(session.players[0].platform, DEFAULT_REASON)
                    print(PLEXPY_LOG.format(user=username, title=title, section=section_id))
                    session.stop(reason=reason)

Hi, this looks like the perfect solution to my old i5 machine, however I am a complete noob when it comes to PlexPy, would you be able to do a walkthrough of what I need to do to set this up.

@richarddc79 if you follow the screenshots and edit the settings in the script that say edit you should be able to make it work.

I guess I don’t understand what more of a walkthrough you would like.