diff --git a/pandora/archive/extract.py b/pandora/archive/extract.py index 8cc32465..db85c495 100644 --- a/pandora/archive/extract.py +++ b/pandora/archive/extract.py @@ -351,6 +351,22 @@ def frame(video, frame, position, height=128, redo=False): '-p', str(position), '-y', str(height)] run_command(cmd) +def frame_direct(video, target, position): + fdir = os.path.dirname(target) + if fdir and not os.path.exists(fdir): + os.makedirs(fdir) + + pre = position - 2 + if pre < 0: + pre = 0 + else: + position = 2 + cmd = [ox.file.cmd('ffmpeg'), '-y', '-ss', str(pre), '-i', video, '-ss', str(position), + '-vf', 'scale=iw*sar:ih', + '-an', '-vframes', '1', target] + r = run_command(cmd) + return r == 0 + def resize_image(image_source, image_output, width=None, size=None): if exists(image_source): diff --git a/pandora/archive/models.py b/pandora/archive/models.py index 3962e4bb..a6a0ff40 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -400,6 +400,30 @@ class File(models.Model): def all_paths(self): return [self.path] + [i.path for i in self.instances.all()] + def extract_frames(self): + def video_frame_positions(duration): + pos = duration / 2 + return map(int, [pos/2, pos, pos+pos/2]) + if settings.CONFIG['media'].get('importFrames') and self.data: + filename = self.data.path + prefix = os.path.dirname(filename) + info = self.info + oshash = info['oshash'] + + for pos in video_frame_positions(info['duration']): + position = float(pos) + name = "%s.png" % position + fr, created = Frame.objects.get_or_create(file=self, position=position) + if fr.frame: + fr.frame.delete() + fr.frame.name = self.get_path(name) + if not os.path.exists(fr.frame.path): + extract.frame_direct(filename, fr.frame.path, pos) + if os.path.exists(fr.frame.path): + fr.save() + os.chmod(fr.frame.path, 0644) + self.item.select_frame() + def extract_stream(self): ''' extract stream from direct upload diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index 3c89873c..8947177d 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -129,6 +129,7 @@ def extract_stream(fileId): file=file, resolution=resolution, format=config['formats'][0] ) if created: + file.extract_frames() stream.media.name = stream.path(stream.name()) stream = stream.encode() if stream.available: