View Full Version : Run a script upon starting a video?
torianironfist
2008-12-23, 05:53
Is it possible to run a script, when you chose to play a video.
I read this article here (http://xbmc.org/wiki/?title=HOW-TO:_Catalog_and_use_lookups_on_your_offline_DVD/CD_movie_library_(via_fake_files))about how to make a fake file for your offline DVD collection. I would like to be able to write a script, that ejects the DVD drive, when you play that movie. If this is even possible, if someone could point me in the correct direction, I would appreciate it.
Thanks
rwparris2
2008-12-23, 07:15
use the player class in the xbmc module.
look at my lasttube plugin (link in sig), it has a class that watches for videos to play.
In a script (as opposed to a plugin), you probably won't need the same kind of while loop to keep it alive, but you'll have to play around to figure that out.
also this may not work if you followed the wiki link, since the "video" will not actually play. better thing to do would be to make a real .avi a few seconds long so that the player class can work.
off the top of my head, I think the best thing to do would be put all your short 1-2 second .avi files into one folder called /dvd/ or something like that, then use something like (pseudocode) if self.getTotalTime() == 2 : eject
no idea about ejecting the drive, it seems like there is a builtin or httpapi to do that, but I can't remember.
torianironfist
2008-12-23, 20:37
Thanks for the reply. I did create an .avi to use, it is a 10 sec clip that tells you to insert the DVD.
I also found the section of the script from your plugin, that deals with watching for movies to play. I have to admit though, my python skills are somewhat rudimentary and I have no idea how to interface them with xbmc. I know I can eject the drive, by using the linux command 'eject' so using subprocess, I can run that from a python script. What I cannot figure out is how to find the length of the movie, to see if I need to call the eject command.
If you put all your offline 'videos' in a folder called /dvd/ then could you just check if the source folder for the video is /dvd/ and if so run the eject command?
I don't know too much about python (trying to learn), but since all your offline videos will be in one folder it doesn't matter if you check the video length. There should be a way to check the source path via python. This won't work if you put your offline videos in with your online videos folder.
rwparris2
2008-12-23, 22:50
off the top of my head, I think the best thing to do would be put all your short 1-2 second .avi files into one folder called /dvd/ or something like that, then use something like (pseudocode) if self.getTotalTime() == 2 : eject
I ment OR get total time or check source.
look at the py docs for how to do either
http://xbmc.sourceforge.net/python-docs/xbmc.html
torianironfist
2009-01-05, 21:25
Thanks for the help guys, I am almost there :)
This is the script I have now.
import xbmc,xbmcgui
import subprocess
while 1:
if xbmc.Player().isPlayingVideo()==1:
tag = xbmc.Player().getVideoInfoTag()
path = tag.getPath()
if path.find("DVD Collection") > -1:
subprocess.call("eject", shell=True)
This works, as you might expect ejecting the DVD drive, whenever a video from the "DVD Collection" folder is playing.
So far I have it playing a 10 sec video telling you to insert the DVD. If you are really fast, and insert the DVD while the video is playing it instantly ejects it again. Is there a way to modify this script, so that that it would only happen when a video is started to play, instead of when it is just playing.
torianironfist
2009-01-05, 22:00
I tried this
import xbmc,xbmcgui
import subprocess
class MyPlayer( xbmc.Player ) :
def __init__ ( self ):
xbmc.Player.__init__( self )
def onPlayBackStarted(self):
if xbmc.Player().isPlayingVideo()==1:
tag = xbmc.Player().getVideoInfoTag()
path = tag.getPath()
if path.find("DVD Collection") > -1:
subprocess.call("eject", shell=True)
p=MyPlayer()
while(1):
xbmc.sleep(500)
but although it does not have any syntax errors (the log doesn't say any), it fails to eject the drive. Can't quite figure out what I have done wrong. :|
Let me know if you have any ideas.
torianironfist
2009-01-05, 22:14
w00t! I got it. Spent a little longer checking out rwparris's lasttube code. I found i needed to make xbmc sleep for a couple secs before asking for the video info. Thanks for all the help. Here is the finished script.
# eject.py
# Script to eject DVD drive whenever any video file is played that is located in the "DVD Collection" folder.
# Thanks to rwparris2 for his LastTube code and help on the forums.
import xbmc,xbmcgui
import subprocess
class MyPlayer( xbmc.Player ) :
def __init__ ( self ):
xbmc.Player.__init__( self )
def onPlayBackStarted(self):
xbmc.sleep(2000)
tag = xbmc.Player().getVideoInfoTag()
path = tag.getPath()
if path.find("DVD Collection") > -1:
subprocess.call("eject", shell=True)
p=MyPlayer()
while(1):
xbmc.sleep(500)