def mainLoop (): """ """ cycletime = 21600 # 6 hours, 4 cycles a day # cycletime = 3600 # cycletime = 30 signal.signal (signal.SIGTERM, interrupthandler) signal.signal (signal.SIGINT, interrupthandler) signal.signal (signal.SIGHUP, interrupthandler) savedbotfile = os.path.join (rundir, 'savedbot.txt') if os.path.isfile (pidfilename): writelog ("[composter]: stale (?) pidfile found. Exiting.") print "[composter]: stale (?) pidfile found. Exiting." return 1; pidfile = open (pidfilename, "w") pidfile.write ("{0}".format (os.getpid ())) pidfile.close () writelog ("[composter]: started with pid {0}".format (os.getpid ())) while True: botlist = BotList () numbots = len (botlist.bots) writelog ("[composter]: started cycle with {0} seconds per bot".format (cycletime / numbots)) for b in botlist.getList (): if os.path.isfile (savedbotfile): f = open (savedbotfile, 'r') s = f.readline () f.close () if b == s: writelog ("[composter]: continuing with saved bot {0}".format (b)) os.remove (savedbotfile) else: continue try: bot = __import__ (b) reload (bot) pid = os.fork () if pid == 0: return bot.runMe () else: signal.signal (signal.SIGALRM, alarmhandler) signal.alarm (cycletime / numbots) try: writelog ("[composter]: waiting for {0}[.py] with pid {1}".format (b, pid)) rv = os.waitpid (pid, 0) writelog ("[composter]: {0}[.py] with pid {1} returned {2}".format (b, pid, rv)) except VCTimeout: os.kill (pid, signal.SIGHUP) rv = os.waitpid (pid, 0) writelog ("[composter]: {0} with pid {1} returned {2} on SIGHUP after timeout".format (b, pid, rv)) except ImportError: writelog ("[composter]: Error importing {0}[.py]".format (b)) except SyntaxError: writelog ("[composter]: Bot {0} has Syntax Error".format (b)) except VCInterrupt: os.kill (pid, signal.SIGHUP) rv = os.waitpid (pid, 0) # save the running bot f = open (savedbotfile, 'w') f.write (b) f.close () writelog ("[composter]: Exiting. Saved bot '{0}' for next run.".format (b)) os.remove (pidfilename) return 0 compost = Compost () compost.dropFrames () writelog ('[composter]: {0}'.format (compost.stats ())) del compost writelog ("[composter]: Loop terminated abnormally.") os.remove (pidfilename) return 1
""" custom classes to access video compost data """ from Compost import Compost from VCLogger import writelog from vcconfig import * """ Adapt config to your needs. Use loadConfig () and saveConfig () to store data across runs. """ config = {} config["myname"] = __name__ """ create an instance of Compost to access video data """ compost = Compost() class BotError(Exception): """ custom exception """ def __init__(self, msg): self.msg = msg def __str__(self): return repr(self.msg) def signalhandler(signum, frame): """
class GTK_Main: def __init__(self, directoryname="/tmp"): self.savePid () self.blocksize = WIDTH * HEIGHT * 4 self.dir = directoryname self.compost_init() ## liste mit verfuegbaren chunks # self.compost._chunkss self.chunkindex=0 window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Video-Player") window.set_default_size(500, 400) window.fullscreen() # make the cursor disappear on realize window.connect("realize", self.realize_cb) window.connect("destroy", gtk.main_quit, "WM destroy") vbox = gtk.VBox() window.add(vbox) self.movie_window = gtk.DrawingArea() vbox.add(self.movie_window) window.show_all() self.pipeline = gst.Pipeline() # open a file self.fileplayer = gst.element_factory_make("filesrc") self.pipeline.add(self.fileplayer) self.fileplayer.set_property("blocksize", self.blocksize) # force caps self.capsstring = "video/x-raw-rgb, bpp=(int)32, endianness=(int)4321, depth=(int)32, red_mask=(int)16711680, green_mask=(int)65280, blue_mask=(int)255, alpha_mask=(int)-16777216, width=(int)%d, height=(int)%d, framerate=%s, pixel-aspect-ratio=(fraction)1/1" % (WIDTH, HEIGHT, FRAMERATE) caps = gst.Caps(self.capsstring) self.capsfilter = gst.element_factory_make("capsfilter") self.pipeline.add(self.capsfilter) self.capsfilter.set_property("caps", caps) # colorspace converter self.colorspace = gst.element_factory_make("ffmpegcolorspace") self.pipeline.add(self.colorspace) # videorate converter self.videorate = gst.element_factory_make("videorate") self.pipeline.add(self.videorate) # output window self.sink = gst.element_factory_make("xvimagesink") self.pipeline.add(self.sink) self.sink.set_xwindow_id(self.movie_window.window.xid) gst.element_link_many(self.fileplayer, self.capsfilter, self.colorspace, self.videorate, self.sink) # bus magic bus = self.pipeline.get_bus() bus.add_signal_watch() bus.enable_sync_message_emission() bus.connect("message", self.on_message) bus.connect("sync-message::element", self.on_sync_message) self.fileplayer.set_property("location", self.get_next_file()) self.pipeline.set_state(gst.STATE_PLAYING) # def start_stop(self, w): # if self.button.get_label() == "Start": # filepath = self.entry.get_text() # if os.path.isfile(filepath): # self.button.set_label("Stop") # self.fileplayer.set_property("location", filepath) # self.pipeline.set_state(gst.STATE_PLAYING) # else: # self.pipeline.set_state(gst.STATE_NULL) # self.button.set_label("Start") def on_message(self, bus, message): t = message.type if t == gst.MESSAGE_EOS: self.pipeline.set_state(gst.STATE_NULL) self.fileplayer.set_property("location", self.get_next_file()) self.pipeline.set_state(gst.STATE_PLAYING) #self.pipeline.set_state(gst.STATE_NULL) #self.button.set_label("Start") elif t == gst.MESSAGE_ERROR: self.pipeline.set_state(gst.STATE_NULL) err, debug = message.parse_error() print "Error: %s" % err, debug # self.button.set_label("Start") def get_next_file(self): chunk = self.get_next_chunk() if chunk != None: return chunk._filename return None def get_next_chunk(self): if self.chunkindex < len(self.compost._chunks): i=self.chunkindex self.chunkindex+=1 return self.compost._chunks[i] return self.compost_init() def compost_init(self): self.compost = Compost() self.compost.load() self.chunkindex=1 return self.compost._chunks[0] def on_sync_message(self, bus, message): if message.structure is None: return message_name = message.structure.get_name() if message_name == "prepare-xwindow-id": imagesink = message.src imagesink.set_property("force-aspect-ratio", False) gtk.gdk.threads_enter() imagesink.set_xwindow_id(self.movie_window.window.xid) gtk.gdk.threads_leave() def savePid (self): pidfilename = os.path.join (rundir, 'playraw.pid') pidfile = open (pidfilename, "w") pidfile.write ("{0}".format (os.getpid ())) pidfile.close () def realize_cb(self, widget): pixmap = gtk.gdk.Pixmap(None, 1, 1, 1) color = gtk.gdk.Color() cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0) widget.window.set_cursor(cursor)
#!/usr/bin/env python import time from Compost import Compost compost = Compost() compost.delete() # for chunk in compost.chunks: # print compost.chunks.index (chunk) # compost.chunks.remove (chunk) # vim: tw=0 ts=2 expandtab # EOF
def compost_init(self): self.compost = Compost() self.compost.load() self.chunkindex=1 return self.compost._chunks[0]
from VCLogger import writelog if os.path.isfile (infilename): infile = open (infilename, "r") else: writelog ("[cutvideo]: {0} not found".format (infilename)) print "[cutvideo]: {0} not found".format (infilename) sys.exit (1) # pixels_per_frame * bytes_per_pixel * frames # will be changed to aprox. 2GB chunksize = 320 * 240 * 4 * 68 chunk = True chunknum = 0 compost = Compost () while True: infile.seek (chunksize * chunknum) chunk = infile.read (chunksize) if not chunk: break hash = hashlib.md5 (str (time.time ())) chunkfilename = os.path.join (compostdir, "%s.raw" % hash.hexdigest ()) chunkfile = open (chunkfilename, "w") chunkfile.write (chunk) chunkfile.close () compost.addChunk (chunkfilename) chunknum += 1 os.remove (infilename)
#!/usr/bin/env python import time from Compost import Compost compost = Compost () compost.delete () # for chunk in compost.chunks: # print compost.chunks.index (chunk) # compost.chunks.remove (chunk) # vim: tw=0 ts=2 expandtab # EOF
class GTK_Main: def __init__(self, directoryname="/tmp"): self.savePid() self.blocksize = WIDTH * HEIGHT * 4 self.dir = directoryname self.compost_init() ## liste mit verfuegbaren chunks # self.compost._chunkss self.chunkindex = 0 window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Video-Player") window.set_default_size(500, 400) window.fullscreen() # make the cursor disappear on realize window.connect("realize", self.realize_cb) window.connect("destroy", gtk.main_quit, "WM destroy") vbox = gtk.VBox() window.add(vbox) self.movie_window = gtk.DrawingArea() vbox.add(self.movie_window) window.show_all() self.pipeline = gst.Pipeline() # open a file self.fileplayer = gst.element_factory_make("filesrc") self.pipeline.add(self.fileplayer) self.fileplayer.set_property("blocksize", self.blocksize) # force caps self.capsstring = "video/x-raw-rgb, bpp=(int)32, endianness=(int)4321, depth=(int)32, red_mask=(int)16711680, green_mask=(int)65280, blue_mask=(int)255, alpha_mask=(int)-16777216, width=(int)%d, height=(int)%d, framerate=%s, pixel-aspect-ratio=(fraction)1/1" % ( WIDTH, HEIGHT, FRAMERATE) caps = gst.Caps(self.capsstring) self.capsfilter = gst.element_factory_make("capsfilter") self.pipeline.add(self.capsfilter) self.capsfilter.set_property("caps", caps) # colorspace converter self.colorspace = gst.element_factory_make("ffmpegcolorspace") self.pipeline.add(self.colorspace) # videorate converter self.videorate = gst.element_factory_make("videorate") self.pipeline.add(self.videorate) # output window self.sink = gst.element_factory_make("xvimagesink") self.pipeline.add(self.sink) self.sink.set_xwindow_id(self.movie_window.window.xid) gst.element_link_many(self.fileplayer, self.capsfilter, self.colorspace, self.videorate, self.sink) # bus magic bus = self.pipeline.get_bus() bus.add_signal_watch() bus.enable_sync_message_emission() bus.connect("message", self.on_message) bus.connect("sync-message::element", self.on_sync_message) self.fileplayer.set_property("location", self.get_next_file()) self.pipeline.set_state(gst.STATE_PLAYING) # def start_stop(self, w): # if self.button.get_label() == "Start": # filepath = self.entry.get_text() # if os.path.isfile(filepath): # self.button.set_label("Stop") # self.fileplayer.set_property("location", filepath) # self.pipeline.set_state(gst.STATE_PLAYING) # else: # self.pipeline.set_state(gst.STATE_NULL) # self.button.set_label("Start") def on_message(self, bus, message): t = message.type if t == gst.MESSAGE_EOS: self.pipeline.set_state(gst.STATE_NULL) self.fileplayer.set_property("location", self.get_next_file()) self.pipeline.set_state(gst.STATE_PLAYING) #self.pipeline.set_state(gst.STATE_NULL) #self.button.set_label("Start") elif t == gst.MESSAGE_ERROR: self.pipeline.set_state(gst.STATE_NULL) err, debug = message.parse_error() print "Error: %s" % err, debug # self.button.set_label("Start") def get_next_file(self): chunk = self.get_next_chunk() if chunk != None: return chunk._filename return None def get_next_chunk(self): if self.chunkindex < len(self.compost._chunks): i = self.chunkindex self.chunkindex += 1 return self.compost._chunks[i] return self.compost_init() def compost_init(self): self.compost = Compost() self.compost.load() self.chunkindex = 1 return self.compost._chunks[0] def on_sync_message(self, bus, message): if message.structure is None: return message_name = message.structure.get_name() if message_name == "prepare-xwindow-id": imagesink = message.src imagesink.set_property("force-aspect-ratio", False) gtk.gdk.threads_enter() imagesink.set_xwindow_id(self.movie_window.window.xid) gtk.gdk.threads_leave() def savePid(self): pidfilename = os.path.join(rundir, 'playraw.pid') pidfile = open(pidfilename, "w") pidfile.write("{0}".format(os.getpid())) pidfile.close() def realize_cb(self, widget): pixmap = gtk.gdk.Pixmap(None, 1, 1, 1) color = gtk.gdk.Color() cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0) widget.window.set_cursor(cursor)
def compost_init(self): self.compost = Compost() self.compost.load() self.chunkindex = 1 return self.compost._chunks[0]