from Plugins.Plugin import PluginDescriptor from Screens.Screen import Screen from Screens.MessageBox import MessageBox from Screens.ChoiceBox import ChoiceBox from Components.config import * from Components.ActionMap import ActionMap, NumberActionMap from Components.ConfigList import ConfigList, ConfigListScreen from Components.Button import Button from Components.Label import Label from Components.Pixmap import Pixmap from enigma import eTimer, eServiceReference, eServiceCenter, iServiceInformation import os def main(session, service, **kwargs): MovieCut(session, service, **kwargs) def Plugins(**kwargs): return PluginDescriptor(name="MovieCut", description="Execute cuts...", where = PluginDescriptor.WHERE_MOVIELIST, fnc=main) class MovieCut(Screen): def __init__(self, session, service): Screen.__init__(self, session) self.session = session self.service = service serviceHandler = eServiceCenter.getInstance() path = self.service.getPath() info = serviceHandler.info(self.service) if not info: self.name = path else: self.name = info.getName(self.service) tlist = [] tlist.append(("Don't cut", 0)) tlist.append(("Replace the original movie with the cut movie", 1)) tlist.append(("Place the cut movie in a new file ending with \" cut\"", 2)) tlist.append(("Advanced cut parameter settings...", 3)) self.session.openWithCallback(self.cutConfirmed, ChoiceBox, _("How would you like to cut \"%s\"?") % (self.name), list = tlist, selection = 0) def cutConfirmed(self, confirmed): if not confirmed or confirmed[1] == 0: return self.close() elif confirmed[1] == 1: MovieCutSpawn(self, ["/usr/bin/mcut", "-r", self.service.getPath()], self.name) elif confirmed[1] == 2: MovieCutSpawn(self, ["/usr/bin/mcut", self.service.getPath()], self.name) elif confirmed[1] == 3: serviceHandler = eServiceCenter.getInstance() info = serviceHandler.info(self.service) self.path = self.service.getPath() self.name = info.getName(self.service) self.descr = info.getInfoString(self.service, iServiceInformation.sDescription) self.session.openWithCallback(self.advcutConfirmed, AdvancedCutInput, self.name, self.path, self.descr) def advcutConfirmed(self, ret): if len(ret) <= 1 or not ret[0]: return self.close() clist = ["/usr/bin/mcut"] if ret[1] == True: clist.append("-r") clist.append(self.service.getPath()) if ret[2] != False: clist += ["-o", ret[2]] if ret[3] != False: clist += ["-n", ret[3]] if ret[4] != False: clist += ["-d", ret[4]] if ret[5] != False: clist.append("-c") clist += ret[5] MovieCutSpawn(self, clist, self.name) class AdvancedCutInput(Screen, ConfigListScreen): skin = """ """ def __init__(self, session, name, path, descr): self.skin = AdvancedCutInput.skin Screen.__init__(self, session) self["oktext"] = Label(_("OK")) self["canceltext"] = Label(_("Cancel")) self["ok"] = Pixmap() self["cancel"] = Pixmap() if self.baseName(path) == self.baseName(name): self.title = "" else: self.title = name self.dir = self.dirName(path) self.path = self.baseName(path) + " cut" self.descr = descr self.input_replace = ConfigSelection(choices = [("no", _("No")), ("yes", _("Yes"))], default = "no") self.input_path = ConfigText(default = self.path, fixed_size = False, visible_width = 45) self.input_title = ConfigText(default = self.title, fixed_size = False, visible_width = 45) self.input_descr = ConfigText(default = self.descr, fixed_size = False, visible_width = 45) self.input_manual = ConfigSelection(choices = [("no", _("Cutlist")), ("yes", _("Manual specification"))], default = "no") self.input_space = ConfigNothing() self.input_manualcuts = ConfigText(default = "", fixed_size = False) self.input_manualcuts.setUseableChars(" 0123456789:.") self["actions"] = NumberActionMap(["SetupActions"], { "ok": self.keyGo, "save": self.keyGo, "cancel": self.keyCancel, }, -2) self.list = [] ConfigListScreen.__init__(self, self.list) self.entry_replace = getConfigListEntry(_("Replace original:"), self.input_replace) self.entry_path = getConfigListEntry(_("New filename:"), self.input_path) self.entry_title = getConfigListEntry(_("New title:"), self.input_title) self.entry_descr = getConfigListEntry(_("New description:"), self.input_descr) self.entry_manual = getConfigListEntry(_("Cut source:"), self.input_manual) self.entry_space = getConfigListEntry(_("Cuts (an IN OUT IN OUT ... sequence of hour:min:sec)"), self.input_space) self.entry_manualcuts = getConfigListEntry(_(":"), self.input_manualcuts) self.createSetup(self["config"]) def createSetup(self, configlist): self.list = [] self.list.append(self.entry_replace) if self.input_replace.value == "no": self.list.append(self.entry_path) self.list.append(self.entry_title) self.list.append(self.entry_descr) self.list.append(self.entry_manual) if self.input_manual.value == "yes": self.list.append(self.entry_space) self.list.append(self.entry_manualcuts) configlist.list = self.list configlist.l.setList(self.list) def keyLeft(self): ConfigListScreen.keyLeft(self) cc = self["config"].getCurrent() if cc is self.entry_replace or cc is self.entry_manual: self.createSetup(self["config"]) def keyRight(self): ConfigListScreen.keyRight(self) cc = self["config"].getCurrent() if cc is self.entry_replace or cc is self.entry_manual: self.createSetup(self["config"]) def keyGo(self): if self.input_replace.value == "yes": path = False else: path = self.rejoinName(self.dir, self.input_path.value) if self.input_manual.value == "no": cuts = False else: cuts = self.input_manualcuts.value.split(' ') while "" in cuts: cuts.remove("") self.close((True, self.input_replace.value, path, self.input_title.value, self.input_descr.value, cuts)) def keyCancel(self): self.close((False,)) def baseName(self, str): name = str.split('/')[-1] if name.endswith(".ts") is True: return name[:-3] else: return name def dirName(self, str): return '/'.join(str.split('/')[:-1]) + '/' def rejoinName(self, dir, name): name = name.strip() if name.endswith(".ts") is True: return dir + name[:-3] else: return dir + name global_mcut_errors = ["The movie \"%s\" is successfully cut", "Cutting failed for movie \"%s\":\nBad arguments", "Cutting failed for movie \"%s\":\nCouldn't open input .ts file", "Cutting failed for movie \"%s\":\nCouldn't open input .cuts file", "Cutting failed for movie \"%s\":\nCouldn't open input .ap file", "Cutting failed for movie \"%s\":\nCouldn't open output .ts file", "Cutting failed for movie \"%s\":\nCouldn't open output .cuts file", "Cutting failed for movie \"%s\":\nCouldn't open output .ap file", "Cutting failed for movie \"%s\":\nEmpty .ap file", "Cutting failed for movie \"%s\":\nNo cuts specified", "Cutting failed for movie \"%s\":\nRead/write error (disk full?)", "Cutting was aborted for movie \"%s\""] global_mcut_queue = [] global_mcut_block = False class MovieCutSpawn: def __init__(self, screen, clist, name): global global_mcut_queue global global_mcut_block self.screen = screen self.session = screen.session self.name = name self.clist = clist self.res = False self.dialog = False self.waitTimer = eTimer() self.waitTimer.callback.append(self.doWait) self.spawnTimer = eTimer() self.spawnTimer.callback.append(self.doSpawn) if not global_mcut_queue: self.mcut_id = 1 global_mcut_block = True self.dialog = self.session.openWithCallback(self.endc, MessageBox, _("The movie \"%s\" is cut in the background.") % (self.name), MessageBox.TYPE_INFO) else: self.mcut_id = global_mcut_queue[-1] + 1 global_mcut_block = True self.dialog = self.session.openWithCallback(self.endc, MessageBox, _("Another movie is currently cut.\nThe movie \"%s\" will be cut in the background after it.") % (self.name), MessageBox.TYPE_INFO) global_mcut_queue.append(self.mcut_id) self.doSpawn() def doSpawn(self): global global_mcut_queue print "MovieCut: Waiting to spawn", self.mcut_id if global_mcut_queue[0] != self.mcut_id: self.spawnTimer.start(2000, True) else: self.pid = os.spawnv(os.P_NOWAIT, self.clist[0], self.clist) print "MovieCut: Spawning", self.mcut_id, ", pid =", self.pid self.doWait() def doWait(self): global global_mcut_queue global global_mcut_errors global global_mcut_block if self.res: res = self.res else: res = os.waitpid(self.pid, os.WNOHANG) print "MovieCut: Waiting for process", self.pid, ", got", res if not res[0] == self.pid: self.waitTimer.start(2000, True) else: print "MovieCut: Finished process", self.pid, ", waiting to announce it" if not self.res: global_mcut_queue = global_mcut_queue[1:] if os.WIFEXITED(res[1]): mess = global_mcut_errors[os.WEXITSTATUS(res[1])] else: mess = global_mcut_errors[-1] if not self.session.in_exec or (global_mcut_block and not self.dialog): self.res = res self.waitTimer.start(2000, True) else: global_mcut_block = True print "MovieCut: Announcing it, all done with", self.mcut_id, self.pid self.session.openWithCallback(self.endw, MessageBox, mess % (self.name), MessageBox.TYPE_INFO) def endw(self, arg = 0): global global_mcut_block global_mcut_block = False if self.session.current_dialog == self.dialog: self.session.current_dialog.close() self.endc(arg) def endc(self, arg = 0): global global_mcut_block global_mcut_block = False self.dialog = False self.session.current_dialog.close()