diff --git a/oxdbarchive/extract.py b/oxdbarchive/extract.py index 5739b07..85115da 100644 --- a/oxdbarchive/extract.py +++ b/oxdbarchive/extract.py @@ -141,7 +141,9 @@ def extract_frame(movie_file, timestamp, img_folder, width=128, offset = 0, redo mplayer_options = '' mplayer_options += " '%s'" % movie_file mplayer_options += " -ss '%s' -frames 2" % (timestamp_in_file) - mplayer_options += " -vo jpeg:quality=90 -vf scale -zoom -xy %d " % width + mplayer_options += " -vo jpeg:quality=90 " + if width > 0: + mplayer_options += " -vf scale -zoom -xy %d " % width mplayer_options += " -ao null" mplayer = "mplayer %s >/dev/null 2>&1" % mplayer_options frame = os.path.join(img_folder, "%s.%s" % (timestamp.replace(':', '.'), img_extension)) @@ -161,15 +163,46 @@ def extract_frame(movie_file, timestamp, img_folder, width=128, offset = 0, redo print "update the cache %s missing" % movie_file shutil.rmtree(framedir) -def extract_still(movie_file, png_file, inpoint): +def extract_still(movie_file, timestamp, img_folder, width=128, offset = 0, redo = False): + import warnings + warnings.filterwarnings("ignore", "tempnam") ext = movie_file.split('.')[-1] if ext in ('sub', 'srt'): print "this is not a movie file, will not try to extract frames" return - inpoint = time2ms(inpoint) - extractClipScript = abspath(join(dirname(__file__), "tools/extract_frame.py")) - cmd = '''%s "%s" "%s" %s 0 -1''' % (extractClipScript, movie_file, png_file, inpoint) - run_command(cmd.encode('utf-8'), 100) + framedir = os.tempnam() + + os.mkdir(framedir) + os.chdir(framedir) + if offset: + timestamp_in_file = shift_time(-offset, timestamp) + else: + timestamp_in_file = timestamp + if os.path.exists(movie_file): + mplayer_options = '' + mplayer_options += " '%s'" % movie_file + mplayer_options += " -ss '%s' -frames 2" % (timestamp_in_file) + mplayer_options += " -vo png " + if width > 0: + mplayer_options += " -vf scale -zoom -xy %d " % width + mplayer_options += " -ao null" + mplayer = "mplayer %s >/dev/null 2>&1" % mplayer_options + frame = os.path.join(img_folder, "%s.%s" % (timestamp.replace(':', '.'), 'png')) + if redo or not os.path.exists(frame): + print mplayer.encode('utf-8') + os.system (mplayer.encode('utf-8')) + files = os.listdir(framedir) + if files: + print "creating frame ", frame + shutil.move(os.path.join(framedir,files[-1]), frame) + if len(files)>1: + for f in files[:-2]: + print "unlink", f + os.unlink(f) + time.sleep(0.1) + else: + print "update the cache %s missing" % movie_file + shutil.rmtree(framedir) def extract_poster_still(movie_file, png_file, inpoint): ext = movie_file.split('.')[-1] diff --git a/oxdbarchive/model.py b/oxdbarchive/model.py index 18a1473..fd1fc5e 100644 --- a/oxdbarchive/model.py +++ b/oxdbarchive/model.py @@ -431,6 +431,11 @@ class ArchiveFile(SQLObject): f = join(cache.cache_root, 'frame', self.md5sum[:4], self.md5sum) oxdb_makedir(f) return f + + def _get_stillFolder(self): + f = join(cache.cache_root, 'still', self.md5sum[:4], self.md5sum) + oxdb_makedir(f) + return f def _get_timelineFile(self): return join(cache.cache_root, 'timeline', self.md5sum[:4], "%s.png" % self.md5sum) @@ -438,10 +443,6 @@ class ArchiveFile(SQLObject): def _get_posterStillFile(self): return join(cache.cache_root, 'posterStill', self.md5sum[:4], "%s.png" % self.md5sum) - def stillFile(self, position): - position = oxlib.getValidFilename(position) - return join(cache.cache_root, 'still', self.md5sum[:4], "%s_%s.png" % (self.md5sum, position)) - def removeMiniMovie(self): if exists(self.mini_movie_file): os.remove(self.mini_movie_file) @@ -740,13 +741,13 @@ class ArchiveFile(SQLObject): return cache.loadPosterStill(self, position) def still(self, position): - still = self.stillFile(position) + still = os.path.join(self.stillFolder, "%s.%s" % (position.replace(':', '.'), 'png')) if not exists(still): oxdb_makedir(dirname(still)) movieFile = self.absolutePath if os.path.splitext(movieFile)[-1] in ('.mov', '.mpg', '.mpeg'): movieFile = self.mini_movie_file - extract_still(movieFile, still, position) + extract_still(movieFile, position, self.stillFolder, -1) if exists(still): return cache.loadFile(still) return ''