[pitivi: 4/13] Cache thumbnails in XDG_CACHE_HOME/pitivi.



commit dd202aa941a58d55fa1ca57ac349f0cde6a5477c
Author: Alessandro Decina <alessandro d gmail com>
Date:   Mon Mar 1 14:09:19 2010 +0100

    Cache thumbnails in XDG_CACHE_HOME/pitivi.
    
    This speeds up importing a project significantly until we get a
    project format that bundles thumbnails.

 pitivi/discoverer.py |   30 +++++++++++++++++++++++++++---
 pitivi/settings.py   |    4 ++++
 2 files changed, 31 insertions(+), 3 deletions(-)
---
diff --git a/pitivi/discoverer.py b/pitivi/discoverer.py
index a5e850a..3d44924 100644
--- a/pitivi/discoverer.py
+++ b/pitivi/discoverer.py
@@ -42,6 +42,7 @@ from pitivi.factories.file import FileSourceFactory, PictureFileSourceFactory
 from pitivi.stream import get_stream_for_pad
 from pitivi.signalinterface import Signallable
 from pitivi.stream import VideoStream
+from pitivi.settings import xdg_cache_home
 
 # FIXME: We need to store more information regarding streams
 # i.e. remember the path took to get to a raw stream, and figure out
@@ -471,10 +472,17 @@ class Discoverer(Signallable, Loggable):
         return tmp
 
     def _getThumbnailFilenameFromPad(self, pad):
-        tmp = self._gettempdir()
+        base = xdg_cache_home()
         name = '%s.%s' % (self.current_uri, pad.get_name())
         name = urlsafe_b64encode(name) + '.png'
-        filename = os.path.join(tmp, name)
+        directory = os.path.join(base, "pitivi")
+        try:
+            os.makedirs(directory)
+        except OSError, e:
+            # 17 = file exists
+            if e.errno != 17:
+                raise
+        filename = os.path.join(base, "pitivi", name)
 
         return filename
 
@@ -484,6 +492,8 @@ class Discoverer(Signallable, Loggable):
         except gst.QueryError:
             duration = 0
 
+        self.debug("doing thumbnail seek at %s", gst.TIME_ARGS(duration))
+
         if duration:
             self.pipeline.seek_simple(gst.FORMAT_TIME,
                     gst.SEEK_FLAG_FLUSH, duration / 3)
@@ -499,6 +509,21 @@ class Discoverer(Signallable, Loggable):
         """ a new video pad was found """
         self.debug("pad %r", pad)
 
+        thumbnail = self._getThumbnailFilenameFromPad(pad)
+        self.thumbnails[pad] = thumbnail
+
+        if os.path.exists(thumbnail):
+            self.debug("we already have a thumbnail %s for %s", thumbnail, pad)
+            sink = gst.element_factory_make("fakesink")
+            sink.props.num_buffers = 1
+            self.dynamic_elements.append(sink)
+            self.pipeline.add(sink)
+            sink.set_state(gst.STATE_PLAYING)
+
+            pad.link(sink.get_pad("sink"))
+
+            return
+
         pad.set_blocked_async(True, self._videoPadBlockCb)
 
         queue = gst.element_factory_make("queue")
@@ -510,7 +535,6 @@ class Discoverer(Signallable, Loggable):
         pngenc = gst.element_factory_make("pngenc")
         pngenc.props.snapshot = True
         pngsink = gst.element_factory_make("filesink")
-        self.thumbnails[pad] = thumbnail = self._getThumbnailFilenameFromPad(pad)
         pngsink.props.location = thumbnail
 
         self.dynamic_elements.extend([queue, vscale, csp, pngenc, pngsink])
diff --git a/pitivi/settings.py b/pitivi/settings.py
index 20c768f..406b43b 100644
--- a/pitivi/settings.py
+++ b/pitivi/settings.py
@@ -96,6 +96,10 @@ def xdg_data_home(autocreate=True):
     return get_dir(get_env_default("XDG_DATA_HOME",
         os.path.join(os.getenv("HOME"), ".local", "share")), autocreate)
 
+def xdg_cache_home(autocreate=True):
+    return get_dir(get_env_default("XDG_CACHE_HOME",
+        os.path.join(os.getenv("HOME"), ".cache")), autocreate)
+
 def xdg_data_dirs():
     return get_env_dirs("XDG_DATA_DIRS",
         os.path.pathsep.join((os.path.join("usr", "local", "share"),



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]