I feel like a bit of an idiot not being able to figure this out. Maybe someone here can help.
I wrote a short python script that, when executed, will call up every active stream on my plex server, it will find where the viewer currently is using -viewOffset and save an MP4 file with 30 second handles (before and after the current moment). The idea is that, sometimes when I’m watching movies, there’s a moment that I love and want to maybe use in a video later. So it’s essentially a “snag this moment and save it as an mp4 for later” button.
I thought getting that to work was going to be the hard part. Turns out that was easy, and I’m really struggling with how to execute it remotely.
So I’m wondering if there’s a way, within Plex possibly, to remotely trigger a pice of python code on my server like that. Any thoughts?
You could possible use PlexPy’s custom script notifications with the “On Pause” event. This will trigger your python script when you pause a video. You can pass some custom script arguments from PlexPy to your script to only have it trigger for certain users (yourself) and stuff like that.
That’s one way to do it, although I may end up with a lot more clips than I need haha
Is there any way to remote-execute scripts that’s simple? Other than creating a webserver, cgi-bin, and executing it that (which seems really expansive for such a small task).
There’s no way to do this directly from within Plex. Personally I would be too lazy to pull out my phone or get up and walk to my computer to execute a script lol. But simply pressing pause on my remote takes no effort at all.
Maybe you can figure out a way to use both the “on pause” and “on resume” events like “save the video clip only if I pause and resume within 5 seconds”?
You could create a channel and have it run python code directly or have it execute external scripts.
then you can make http requests to server:32400/video/mychannel/whatever.
@coryo123 thanks, I was thinking something like this but wasn’t sure if a basic single-button channel was difficult to get up and running. I’ll look into that. Thanks!
I was shocked to find difficult it is to execute code remotely, just in general, not even with Plex. I guess its a security thing. I just figured there was something like Launcher or Alfred or anything to trigger an event on a machine over the internet.
I’ll look into Channels.
So I tried working on a channel, using Vimeo’s as a template, and i can’t figure out this one thing. I’m not sure if maybe I’m missing something basic about python programming, but I can’t seem to execute anything.
@route('/video/clipvideo/videos', page=int, cacheTime=int, allow_sync=True)
def GetVideos(title, url, page=1, cacheTime=CACHE_1HOUR):
subprocess.check_output("mkdir ~/Desktop/working1", shell=True)
logging.basicConfig(filename='/Users/Plex/Desktop/logfile.log',level=logging.DEBUG)
output = subprocess.check_output("ls -l", shell=True)
#output = subprocess.check_output("python /Users/Plex/Desktop/getRequestTest.py", shell=True)
logging.debug(output)
subprocess.check_output("mkdir ~/Desktop/working3", shell=True)
return ObjectContainer(header = "No More Videos", message = "No more videos are available...")
So basically, it makes all of those directories on my desktop. It does NOT create a log file, and it does not (when uncommented) run the python script from the command line.
Ultimately, I just want to execute the code inside the getRequestedTest.py file. I’ve tried putting it in the same folder as the the init.py and I’ve tried copying and pasting the code directly into the init file. Neither works.
plex channel code runs in a sandboxed python environment so things don’t always work the way you expect.
I got external python code running via channel with
@route(PREFIX + '/run')
def Run():
subprocess.call(['python', 'C:/Tools/test.py'])
return 'Hello World'
that python file contents:
import os
import sys
cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open('test.txt', 'w') as file:
file.write('%s
' % cwd)
file.write('%s
' % os.getcwd())
file.write(sys.version)
which creates a txt file in the same dir as the py file with contents
\\?\E:\Library\Plex Media Server\Plug-in Support\Data\com.plexapp.plugins.script
C:\Tools
3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)]
Man, I really thought that was working! I tried it, and what you’ve done works correctly. But when I swap out your test.py for my python file, it doesn’t work. I just cannot for the life of me figure this out. It just stops as soon as it hits my file. I wonder if the problem is I’m calling a ton of things:
import os
import urllib
import urllib2
import xml.etree.ElementTree as etree
from moviepy.editor import *
from moviepy.video.tools.cuts import find_video_period
from moviepy.audio.tools.cuts import find_audio_period
import random
import string
I think we need to clean up the sys.path before imports to get rid of Plex folders so your script doesn’t import plex’s version of things. on windows these folders are
'C:\\Program Files (x86)\\Plex\\Plex Media Server\\DLLs',
'C:\\Program Files (x86)\\Plex\\Plex Media Server\\Exts',
'C:\\Program Files (x86)\\Plex\\Plex Media Server\\Resources\\Plug-ins-26325ea\\Framework.bundle\\Contents\\Resources\\Platforms\\Shared\\Libraries',
import sys
sys.path = [x for x in sys.path if 'Plex' not in x] # maybe 'plexmediaserver' on linux
or do something like this to see what the error is.
try:
import ...
import ...
except Exception as e:
with open('/home/user/log.txt', 'w') as file:
file.write(str(e))
@mxisaac said:
I wrote a short python script that, when executed, will call up every active stream on my plex server, it will find where the viewer currently is using -viewOffset and save an MP4 file with 30 second handles (before and after the current moment). The idea is that, sometimes when I’m watching movies, there’s a moment that I love and want to maybe use in a video later. So it’s essentially a “snag this moment and save it as an mp4 for later” button.
This sounds awesome. Could you post this to a gist or pastebin or something?
Vain hopes that @mxisaac may notice this this time… 
hey @deepseth ! Sorry! wasn’t on here for a while! Sure, I can post it. Lemme get it together…
@mxisaac You’re a beautiful person, thank you!
@deepseth you’re welcome! And definitely let me know if you make any improvements to it! I’m starting to figure out ways to wrap this into my OpenHab home automation setup… weeeee