Cleanded up video imports, removed PyGTK dependency
authorJoar Wandborg <git@wandborg.com>
Thu, 29 Mar 2012 20:52:39 +0000 (22:52 +0200)
committerJoar Wandborg <git@wandborg.com>
Thu, 29 Mar 2012 20:52:39 +0000 (22:52 +0200)
mediagoblin/media_types/video/processing.py
mediagoblin/media_types/video/transcoders.py

index 6a5ce3646668573097faf0dd8ebfcabbfa3e31e9..d4b4e983775787b10dac2d687b4c3ae3b9f59134 100644 (file)
@@ -89,7 +89,7 @@ def process_video(entry):
             height=transcoder.dst_data.videoheight)
 
     # Create a temporary file for the video thumbnail
-    tmp_thumb = tempfile.NamedTemporaryFile()
+    tmp_thumb = tempfile.NamedTemporaryFile(suffix='.jpg')
 
     with tmp_thumb:
         # Create a thumbnail.jpg that fits in a 180x180 square
index e0bd0d3d012cd7045390826451c771b19158a34e..088f6e1ec3b2c1cc2c94ef8c641af53301db4158 100644 (file)
 from __future__ import division
 
 import os
-os.putenv('GST_DEBUG_DUMP_DOT_DIR', '/tmp')
-
 import sys
 import logging
 import urllib
+import multiprocessing
+import gobject
+import gst
+import struct
+import Image
 
-_log = logging.getLogger(__name__)
+from gst.extend import discoverer
+
+gobject.threads_init()
 
 CPU_COUNT = 2
-try:
-    import multiprocessing
-    try:
-        CPU_COUNT = multiprocessing.cpu_count()
-    except NotImplementedError:
-        _log.warning('multiprocessing.cpu_count not implemented')
-        pass
-except ImportError:
-    _log.warning('Could not import multiprocessing, defaulting to 2 CPU cores')
 
 try:
-    import gtk
-except ImportError:
-    raise Exception('Could not find pygtk')
+    CPU_COUNT = multiprocessing.cpu_count()
+except NotImplementedError:
+    _log.warning('multiprocessing.cpu_count not implemented')
 
-try:
-    import gobject
-    gobject.threads_init()
-except ImportError:
-    raise Exception('gobject could not be found')
+_log = logging.getLogger(__name__)
 
-try:
-    import pygst
-    pygst.require('0.10')
-    import gst
-    from gst.extend import discoverer
-except ImportError:
-    raise Exception('gst/pygst 0.10 could not be found')
+os.putenv('GST_DEBUG_DUMP_DOT_DIR', '/tmp')
+
+
+def pixbuf_to_pilbuf(buf):
+    data = list()
+    for i in range(0, len(buf), 3):
+        r, g, b = [i for i in struct.unpack('BBB', buf[i:i + 3])]
+        data.append((r, g, b))
+
+    return data
 
 
 class VideoThumbnailer:
@@ -228,21 +223,18 @@ class VideoThumbnailer:
             width = filters["width"]
             height = filters["height"]
 
-            pixbuf = gtk.gdk.pixbuf_new_from_data(
-                buff.data, gtk.gdk.COLORSPACE_RGB, False, 8,
-                width, height, width * 3)
+            im = Image.new('RGB', (width, height))
 
-            # NOTE: 200x136 is sort of arbitrary.  it's larger than what
-            # the ui uses at the time of this writing.
-            # new_width, new_height = scaled_size((width, height), (200, 136))
+            data = pixbuf_to_pilbuf(buff.data)
 
-            #pixbuf = pixbuf.scale_simple(
-            #new_width, new_height, gtk.gdk.INTERP_BILINEAR)
+            im.putdata(data)
+
+            im.save(self.dest_path);
 
-            pixbuf.save(self.dest_path, 'jpeg')
             _log.info('Saved thumbnail')
-            del pixbuf
+
             self.shutdown()
+
         except gst.QueryError:
             pass
         return False