def extract_thumbnail_wrapper(file_object, node=None, preset_id=None): ext = file_formats.PNG with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() extract_thumbnail_from_video(str(file_object.file_on_disk), tempf.name, overwrite=True) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id)
def test_returns_an_image(self, low_res_video): with TempFile(suffix=".png") as pngf: videos.extract_thumbnail_from_video(low_res_video.name, pngf.name, overwrite=True) with open(pngf.name, "rb") as f: f.seek(0) assert f.read(2) == PNG_MAGIC_NUMBER
def extract_thumbnail_wrapper(file_object, node=None, preset_id=None): ext = file_formats.PNG with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf, \ tempfile.NamedTemporaryFile(suffix=".{}".format(file_object.file_format.extension)) as localtempf: shutil.copyfileobj(file_object.file_on_disk, localtempf) localtempf.flush() tempf.close() extract_thumbnail_from_video(localtempf.name, tempf.name, overwrite=True) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id, uploaded_by=file_object.uploaded_by)
def derive_thumbnail(self): key = "EXTRACTED: {}".format(self.path) if not config.UPDATE and FILECACHE.get(key): return FILECACHE.get(key).decode('utf-8') config.LOGGER.info("\t--- Extracting thumbnail from {}".format(self.path)) tempf = tempfile.NamedTemporaryFile(suffix=".{}".format(file_formats.PNG), delete=False) tempf.close() extract_thumbnail_from_video(self.path, tempf.name, overwrite=True) filename = "{}.{}".format(get_hash(tempf.name), file_formats.PNG) copy_file_to_storage(filename, tempf.name) os.unlink(tempf.name) FILECACHE.set(key, bytes(filename, "utf-8")) return filename
def test_returns_an_image(self, low_res_video): with tempfile.NamedTemporaryFile(suffix=".jpg") as f: videos.extract_thumbnail_from_video(low_res_video.name, f.name) f.seek(0) assert f.read(2) == PNG_MAGIC_NUMBER
def extractor_fun(self, fpath_in, thumbpath_out, **kwargs): extract_thumbnail_from_video(fpath_in, thumbpath_out, **kwargs)
def test_bad_video_raises(self, tmpdir, bad_video): input_file = bad_video.name output_file = tmpdir.join('bad_video_thumbnail.png').strpath with pytest.raises(images.ThumbnailGenerationError): videos.extract_thumbnail_from_video(input_file, output_file, overwrite=True)
def test_raises_for_missing_file(self, tmpdir): input_file = os.path.join(files_dir, 'file_that_does_not_exist.mp4') assert not os.path.exists(input_file) output_file = tmpdir.join('thumbnail.png').strpath with pytest.raises(images.ThumbnailGenerationError): videos.extract_thumbnail_from_video(input_file, output_file, overwrite=True)
def test_generates_16_9_thumbnail_from_high_res(self, tmpdir, high_res_video): input_file = high_res_video.name output_file = tmpdir.join('high_res_video_thumbnail.png').strpath videos.extract_thumbnail_from_video(input_file, output_file, overwrite=True) self.check_16_9_format(output_file) self.check_is_png_file(output_file)