[cvs-annodex] commit (/annodex): cmmlwiki/trunk/cmmlwiki/framer.py

j nobody at lists.annodex.net
Mon Nov 28 07:41:55 EST 2005


Update of /annodex (new revision 1665)

Modified files:
   cmmlwiki/trunk/cmmlwiki/framer.py

Log Message:
- a working version of snapshot_gst, still disabled by default.



Modified: cmmlwiki/trunk/cmmlwiki/framer.py
===================================================================
--- cmmlwiki/trunk/cmmlwiki/framer.py	2005-11-24 00:50:54 UTC (rev 1664)
+++ cmmlwiki/trunk/cmmlwiki/framer.py	2005-11-27 20:41:51 UTC (rev 1665)
@@ -21,46 +21,87 @@
 from cmmlwiki.utils import content_type
 from cmmlwiki.time import time2npt
 
-def snapshot_gst(source, png_frame, timestamp):
+#as it depends on gst0.9 disabled for now
+#FIXME: does not return an image if seeking beond eof
+use_gst=False
+if use_gst:
+    import Image
     import pygst
     pygst.require('0.9')
     import gst
-    import gst.interfaces
-    offset = timestamp * gst.SECOND
 
-    s = '''filesrc name=input ! decodebin name=dec ! queue ! ffmpegcolorspace ! videoscale ! video/x-raw-rgb,width=120,height=88 ! pngenc compression-level=6 ! filesink name=output'''
-    pipeline = gst.parse_launch(s)
 
-    input  = pipeline.get_by_name('input')
-    output = pipeline.get_by_name('output')
-    dec    = pipeline.get_by_name('dec')
+class snapshot_gst:
+    getShot = False
+    def __init__ (self, source, png_frame, seconds):
+        self.png_frame = png_frame
 
-    bus = pipeline.get_bus()
+        nseconds = int(seconds * gst.SECOND)
+        s = '''filesrc name=input 
+             ! decodebin ! queue 
+             ! ffmpegcolorspace ! video/x-raw-rgb 
+             ! fakesink name=output signal-handoffs=true'''
+        pipeline = gst.parse_launch (s)
 
-    input.set_property("location", source)
-    output.set_property("location", png_frame)
+        input  = pipeline.get_by_name ('input')
+        input.set_property ("location", source)
 
-    #FIXME this somehow does not seek to place it should
-    pipeline.set_state(gst.STATE_PAUSED)
-    pipeline.get_state()
-    event = gst.event_new_seek(1.0, gst.FORMAT_TIME,
-            gst.SEEK_FLAG_FLUSH,
-            gst.SEEK_TYPE_SET, offset,
-            gst.SEEK_TYPE_NONE, 0)
-
-    res = dec.send_event(event)
-    if res:
-        pipeline.set_new_stream_time(0L)
+        output = pipeline.get_by_name ('output')
+        output.connect ("handoff", self.snapshot_handoff_cb)
     
-    pipeline.set_state(gst.STATE_PLAYING)
-    pipeline.get_state()
+        pipeline.set_state (gst.STATE_PAUSED)
+        pipeline.get_state ()
 
-    while 1:
-        msg = bus.poll(gst.MESSAGE_ANY, gst.SECOND)
-        if not msg:
-            break
-    pipeline.set_state(gst.STATE_NULL)
+        #seek
+        event = gst.event_new_seek(1.0, gst.FORMAT_TIME,
+                gst.SEEK_FLAG_FLUSH,
+                gst.SEEK_TYPE_SET, nseconds,
+                gst.SEEK_TYPE_NONE, 0)
+        res = output.send_event(event)
+        if res:
+            pipeline.set_new_stream_time(0L)
+        #else:
+        #    gst.error("seek to %r failed" % offset)
 
+        pipeline.set_state (gst.STATE_PLAYING)
+        pipeline.get_state ()
+
+        bus = pipeline.get_bus ()
+
+        #get snapshot
+        self.getShot = True
+        while self.getShot:
+            msg = bus.poll (gst.MESSAGE_ANY, gst.SECOND)
+            if not msg:
+                break
+
+        pipeline.set_state (gst.STATE_NULL)
+
+
+    def scaleto (self, size_max, width, height):
+        if width > height:
+            a = float(height)/width
+            width = size_max
+            height = int(width * a)
+        else:
+            a = float(width)/height
+            height = size_max
+            width = int(height * a)
+        return (width, height)
+
+
+    def snapshot_handoff_cb (self, sink, buffer, pad):
+        if  self.getShot:
+            caps = sink.sink_pads().next().get_negotiated_caps()
+            for s in caps:
+                input_d = (s['width'], s['height'])
+                output_d = self.scaleto (120, s['width'], s['height'])
+            b = str(buffer)
+            img = Image.fromstring('RGB',input_d,b).resize(output_d, Image.BICUBIC)
+            img.save(self.png_frame)
+            self.getShot=False
+
+
 def snapshot_mplayer(source, png_frame, timestamp):
     framedir = os.tempnam()
     os.mkdir(framedir)
@@ -86,11 +127,12 @@
     source = inspector.ixi2source(ixi)
 
     if not os.path.exists(png_frame):
-      if not os.path.exists (image_dir):
+        if not os.path.exists (image_dir):
             os.makedirs (image_dir, mode=0755)
-      #as it depends on gst0.9 and seeking still does not work, disabled for now
-      #snapshot_gst(source, png_frame, timestamp)
-      snapshot_mplayer(source, png_frame, timestamp)
+        if use_gst:
+            snapshot_gst (source, png_frame, timestamp)
+        else:
+            snapshot_mplayer (source, png_frame, timestamp)
     f = open(png_frame,'rb')
     print f.read()
     f.close()


-- 
j



More information about the cvs-annodex mailing list