[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.
This commit is contained in:
@@ -838,7 +838,7 @@ Description The command to run.
|
|||||||
ugoira
|
ugoira
|
||||||
------
|
------
|
||||||
|
|
||||||
Convert Pixiv ugoira to webm.
|
Convert Pixiv Ugoira to WebM (requires `FFmpeg <https://www.ffmpeg.org/>`__).
|
||||||
|
|
||||||
ugoira.extension
|
ugoira.extension
|
||||||
----------------
|
----------------
|
||||||
@@ -865,6 +865,14 @@ Default ``"ffmpeg"``
|
|||||||
Description Location of the ``ffmpeg`` (or ``avconv``) executable to use.
|
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
|
zip
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -53,16 +53,10 @@ class PixivExtractor(Extractor):
|
|||||||
|
|
||||||
url = ugoira["zip_urls"]["medium"].replace(
|
url = ugoira["zip_urls"]["medium"].replace(
|
||||||
"_ugoira600x600", "_ugoira1920x1080")
|
"_ugoira600x600", "_ugoira1920x1080")
|
||||||
|
work["frames"] = ugoira["frames"]
|
||||||
work["extension"] = "zip"
|
work["extension"] = "zip"
|
||||||
yield Message.Url, url, work
|
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:
|
elif work["page_count"] == 1:
|
||||||
url = meta_single_page["original_image_url"]
|
url = meta_single_page["original_image_url"]
|
||||||
work["extension"] = url.rpartition(".")[2]
|
work["extension"] = url.rpartition(".")[2]
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
"""Convert pixiv ugoira to webm"""
|
"""Convert pixiv ugoira to webm"""
|
||||||
|
|
||||||
from .common import PostProcessor
|
from .common import PostProcessor
|
||||||
|
from .. import util
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import zipfile
|
import zipfile
|
||||||
@@ -21,26 +22,25 @@ class UgoiraPP(PostProcessor):
|
|||||||
self.extension = options.get("extension") or "webm"
|
self.extension = options.get("extension") or "webm"
|
||||||
self.ffmpeg = options.get("ffmpeg-location") or "ffmpeg"
|
self.ffmpeg = options.get("ffmpeg-location") or "ffmpeg"
|
||||||
self.args = options.get("ffmpeg-args")
|
self.args = options.get("ffmpeg-args")
|
||||||
|
self.delete = not options.get("keep-files", False)
|
||||||
|
|
||||||
def run(self, pathfmt):
|
def run(self, pathfmt):
|
||||||
if pathfmt.keywords["extension"] != "txt":
|
if (pathfmt.keywords["extension"] != "zip" or
|
||||||
|
"frames" not in pathfmt.keywords):
|
||||||
return
|
return
|
||||||
|
|
||||||
framelist = []
|
framelist = [
|
||||||
|
(frame["file"], frame["delay"] / 1000)
|
||||||
# get frames and their durations
|
for frame in pathfmt.keywords["frames"]
|
||||||
with pathfmt.open("r") as file:
|
]
|
||||||
for line in file:
|
if self.extension != "gif":
|
||||||
name, _, duration = line.partition(" ")
|
# repeat the last frame to prevent it from only being
|
||||||
framelist.append((name, int(duration.rstrip())))
|
|
||||||
# add the last frame twice to prevent it from only being
|
|
||||||
# displayed for a very short amount of time
|
# displayed for a very short amount of time
|
||||||
framelist.append((name, int(duration.rstrip())))
|
framelist.append(framelist[-1])
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
# extract frames
|
# extract frames
|
||||||
pathfmt.set_extension("zip")
|
with zipfile.ZipFile(pathfmt.temppath) as zfile:
|
||||||
with zipfile.ZipFile(pathfmt.realpath) as zfile:
|
|
||||||
zfile.extractall(tempdir)
|
zfile.extractall(tempdir)
|
||||||
|
|
||||||
# write ffconcat file
|
# write ffconcat file
|
||||||
@@ -49,18 +49,20 @@ class UgoiraPP(PostProcessor):
|
|||||||
file.write("ffconcat version 1.0\n")
|
file.write("ffconcat version 1.0\n")
|
||||||
for name, duration in framelist:
|
for name, duration in framelist:
|
||||||
file.write("file '{}'\n".format(name))
|
file.write("file '{}'\n".format(name))
|
||||||
file.write("duration {}\n".format(duration / 1000))
|
file.write("duration {}\n".format(duration))
|
||||||
|
|
||||||
# invoke ffmpeg
|
# invoke ffmpeg
|
||||||
pathfmt.set_extension(self.extension)
|
pathfmt.set_extension(self.extension)
|
||||||
args = [self.ffmpeg, "-i", ffconcat]
|
args = [util.expand_path(self.ffmpeg), "-i", ffconcat]
|
||||||
if self.args:
|
if self.args:
|
||||||
args += self.args
|
args += self.args
|
||||||
args.append(pathfmt.realpath)
|
args.append(pathfmt.realpath)
|
||||||
subprocess.Popen(args).wait()
|
subprocess.Popen(args).wait()
|
||||||
|
|
||||||
# mark framelist file for deletion
|
if self.delete:
|
||||||
pathfmt.delete = True
|
pathfmt.delete = True
|
||||||
|
else:
|
||||||
|
pathfmt.set_extension("zip")
|
||||||
|
|
||||||
|
|
||||||
__postprocessor__ = UgoiraPP
|
__postprocessor__ = UgoiraPP
|
||||||
|
|||||||
Reference in New Issue
Block a user