Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active December 13, 2023 02:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blacktwin/0e6207346acfaaca602eb7dce80226a0 to your computer and use it in GitHub Desktop.
Save blacktwin/0e6207346acfaaca602eb7dce80226a0 to your computer and use it in GitHub Desktop.
Kill Plex video transcoding streams only. All audio streams are left alone. Kill message based on platform.
"""
Kill Plex video transcoding streams only. All audio streams are left alone.
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on playback start
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback Start: kill_trans_exp_audio.py
"""
import requests
import platform
from uuid import getnode
## EDIT THESE SETTINGS ##
PLEX_HOST = ''
PLEX_PORT = 32400
PLEX_SSL = '' # s or ''
PLEX_TOKEN = 'xxxxx'
DEFAULT_REASON = 'This stream has ended due to requiring video transcoding. ' \
'Please raise your Remote Quality to Original to play this content.'
# Find platforms that have history in PlexPy in Play count by platform and stream type Graph
DEVICES = {'Android': 'Andriod message',
'Chrome': 'Chrome message',
'Plex Media Player': 'PMP message',
'Chromecast': 'Chromecast message'}
USER_IGNORE = ('') # ('Username','User2')
##
def fetch(path, t='GET'):
url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT)
headers = {'X-Plex-Token': PLEX_TOKEN,
'Accept': 'application/json',
'X-Plex-Provides': 'controller',
'X-Plex-Platform': platform.uname()[0],
'X-Plex-Platform-Version': platform.uname()[2],
'X-Plex-Product': 'Plexpy script',
'X-Plex-Version': '0.9.5',
'X-Plex-Device': platform.platform(),
'X-Plex-Client-Identifier': str(hex(getnode()))
}
try:
if t == 'GET':
r = requests.get(url + path, headers=headers, verify=False)
elif t == 'POST':
r = requests.post(url + path, headers=headers, verify=False)
elif t == 'DELETE':
r = requests.delete(url + path, headers=headers, verify=False)
if r and len(r.content): # incase it dont return anything
return r.json()
else:
return r.content
except Exception as e:
print e
def kill_stream(sessionId, message):
headers = {'X-Plex-Token': PLEX_TOKEN}
params = {'sessionId': sessionId,
'reason': message}
requests.get('http://{}:{}/status/sessions/terminate'.format(PLEX_HOST, PLEX_PORT),
headers=headers, params=params)
if __name__ == '__main__':
response = fetch('status/sessions')
if 'Video' in response['MediaContainer']:
for s in response['MediaContainer']['Video']:
if s['TranscodeSession']['videoDecision'] == 'transcode' and s['User']['title'] not in USER_IGNORE:
MESSAGE = DEVICES.get(s['Player']['platform'], DEFAULT_REASON)
print("Killing {}'s stream for transcoding video".format(s['User']['title']))
kill_stream(s['Session']['id'], MESSAGE)
elif 'Track' in response['MediaContainer']:
for s in response['MediaContainer']['Track']:
print("{} is streaming audio, let them pass!".format(s['User']['title']))
exit()
else:
exit()
@ptowns188
Copy link

Hi, i get this error in the logs of Plexpy

PlexPy Notifiers :: Script error:
Traceback (most recent call last):
File "/config/kill_trans_exp_audio.py", line 74, in
if 'Video' in response['MediaContainer']:
KeyError: 'MediaContainer'

Thanks for any help.

@blacktwin
Copy link
Author

blacktwin commented Sep 18, 2017

@ptowns188 need a little more than that. How were you running it? What do the rest of the logs say? I've also moved all these gist to a repo on github. This script can be found here. Please use the github repo to report any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment