From eb7a1f3b98058fe3b0f83921a933ef22bad451fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 18 Jun 2018 17:25:52 +0200 Subject: [PATCH] [pixiv] rework ugoira handling Frame information now gets attached to the ZIP file's keyword dict instead of being written to a separate text file. --- docs/configuration.rst | 10 ++++++++- gallery_dl/extractor/pixiv.py | 8 +------ gallery_dl/postprocessor/ugoira.py | 34 ++++++++++++++++-------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index cd8de38f..cd32826f 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -838,7 +838,7 @@ Description The command to run. ugoira ------ -Convert Pixiv ugoira to webm. +Convert Pixiv Ugoira to WebM (requires `FFmpeg `__). ugoira.extension ---------------- @@ -865,6 +865,14 @@ Default ``"ffmpeg"`` Description Location of the ``ffmpeg`` (or ``avconv``) executable to use. =========== ===== +ugoira.keep-files +----------------- +=========== ===== +Type ``bool`` +Default ``false`` +Description Controls whether to keep the ZIP archives or to delete them. +=========== ===== + zip --- diff --git a/gallery_dl/extractor/pixiv.py b/gallery_dl/extractor/pixiv.py index 48acc628..6d90e141 100644 --- a/gallery_dl/extractor/pixiv.py +++ b/gallery_dl/extractor/pixiv.py @@ -53,16 +53,10 @@ class PixivExtractor(Extractor): url = ugoira["zip_urls"]["medium"].replace( "_ugoira600x600", "_ugoira1920x1080") + work["frames"] = ugoira["frames"] work["extension"] = "zip" yield Message.Url, url, work - framelist = "".join( - "{file} {delay}\n".format_map(frame) - for frame in ugoira["frames"] - ) - work["extension"] = "txt" - yield Message.Url, "text:" + framelist, work - elif work["page_count"] == 1: url = meta_single_page["original_image_url"] work["extension"] = url.rpartition(".")[2] diff --git a/gallery_dl/postprocessor/ugoira.py b/gallery_dl/postprocessor/ugoira.py index adda63d4..eef63883 100644 --- a/gallery_dl/postprocessor/ugoira.py +++ b/gallery_dl/postprocessor/ugoira.py @@ -9,6 +9,7 @@ """Convert pixiv ugoira to webm""" from .common import PostProcessor +from .. import util import subprocess import tempfile import zipfile @@ -21,26 +22,25 @@ class UgoiraPP(PostProcessor): self.extension = options.get("extension") or "webm" self.ffmpeg = options.get("ffmpeg-location") or "ffmpeg" self.args = options.get("ffmpeg-args") + self.delete = not options.get("keep-files", False) def run(self, pathfmt): - if pathfmt.keywords["extension"] != "txt": + if (pathfmt.keywords["extension"] != "zip" or + "frames" not in pathfmt.keywords): return - framelist = [] - - # get frames and their durations - with pathfmt.open("r") as file: - for line in file: - name, _, duration = line.partition(" ") - framelist.append((name, int(duration.rstrip()))) - # add the last frame twice to prevent it from only being + framelist = [ + (frame["file"], frame["delay"] / 1000) + for frame in pathfmt.keywords["frames"] + ] + if self.extension != "gif": + # repeat the last frame to prevent it from only being # displayed for a very short amount of time - framelist.append((name, int(duration.rstrip()))) + framelist.append(framelist[-1]) with tempfile.TemporaryDirectory() as tempdir: # extract frames - pathfmt.set_extension("zip") - with zipfile.ZipFile(pathfmt.realpath) as zfile: + with zipfile.ZipFile(pathfmt.temppath) as zfile: zfile.extractall(tempdir) # write ffconcat file @@ -49,18 +49,20 @@ class UgoiraPP(PostProcessor): file.write("ffconcat version 1.0\n") for name, duration in framelist: file.write("file '{}'\n".format(name)) - file.write("duration {}\n".format(duration / 1000)) + file.write("duration {}\n".format(duration)) # invoke ffmpeg pathfmt.set_extension(self.extension) - args = [self.ffmpeg, "-i", ffconcat] + args = [util.expand_path(self.ffmpeg), "-i", ffconcat] if self.args: args += self.args args.append(pathfmt.realpath) subprocess.Popen(args).wait() - # mark framelist file for deletion - pathfmt.delete = True + if self.delete: + pathfmt.delete = True + else: + pathfmt.set_extension("zip") __postprocessor__ = UgoiraPP