-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathugoira.py
138 lines (115 loc) · 4.92 KB
/
ugoira.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
# Copyright 2018 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Convert pixiv ugoira to webm"""
from .common import PostProcessor
from .. import util
import collections
import subprocess
import tempfile
import zipfile
import os
class UgoiraPP(PostProcessor):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
self.extension = options.get("extension") or "webm"
self.args = options.get("ffmpeg-args") or ()
self.twopass = options.get("ffmpeg-twopass", False)
self.output = options.get("ffmpeg-output", True)
self.delete = not options.get("keep-files", False)
ffmpeg = options.get("ffmpeg-location")
self.ffmpeg = util.expand_path(ffmpeg) if ffmpeg else "ffmpeg"
rate = options.get("framerate", "auto")
if rate != "auto":
self.calculate_framerate = lambda _: (None, rate)
if options.get("libx264-prevent-odd", True):
# get last video-codec argument
vcodec = None
for index, arg in enumerate(self.args):
arg, _, stream = arg.partition(":")
if arg == "-vcodec" or arg in ("-c", "-codec") and (
not stream or stream.partition(":")[0] in ("v", "V")):
vcodec = self.args[index + 1]
# use filter if libx264/5 is explicitly or implicitly used
self.prevent_odd = (
vcodec in ("libx264", "libx265") or
not vcodec and self.extension.lower() in ("mp4", "mkv"))
else:
self.prevent_odd = False
def prepare(self, pathfmt):
self._frames = None
if pathfmt.extension != "zip":
return
if "frames" in pathfmt.kwdict:
self._frames = pathfmt.kwdict["frames"]
elif "pixiv_ugoira_frame_data" in pathfmt.kwdict:
self._frames = pathfmt.kwdict["pixiv_ugoira_frame_data"]["data"]
else:
return
if self.delete:
pathfmt.set_extension(self.extension)
def run(self, pathfmt):
if not self._frames:
return
rate_in, rate_out = self.calculate_framerate(self._frames)
with tempfile.TemporaryDirectory() as tempdir:
# extract frames
with zipfile.ZipFile(pathfmt.temppath) as zfile:
zfile.extractall(tempdir)
# write ffconcat file
ffconcat = tempdir + "/ffconcat.txt"
with open(ffconcat, "w") as file:
file.write("ffconcat version 1.0\n")
for frame in self._frames:
file.write("file '{}'\n".format(frame["file"]))
file.write("duration {}\n".format(frame["delay"] / 1000))
if self.extension != "gif":
# repeat the last frame to prevent it from only being
# displayed for a very short amount of time
file.write("file '{}'\n".format(self._frames[-1]["file"]))
# collect command-line arguments
args = [self.ffmpeg]
if rate_in:
args += ["-r", str(rate_in)]
args += ["-i", ffconcat]
if rate_out:
args += ["-r", str(rate_out)]
if self.prevent_odd:
args += ["-vf", "crop=iw-mod(iw\\,2):ih-mod(ih\\,2)"]
if self.args:
args += self.args
self.log.debug("ffmpeg args: %s", args)
# invoke ffmpeg
pathfmt.set_extension(self.extension)
try:
if self.twopass:
if "-f" not in args:
args += ["-f", self.extension]
args += ["-passlogfile", tempdir + "/ffmpeg2pass", "-pass"]
self._exec(args + ["1", "-y", os.devnull])
self._exec(args + ["2", pathfmt.realpath])
else:
args.append(pathfmt.realpath)
self._exec(args)
except OSError as exc:
print()
self.log.error("Unable to invoke FFmpeg (%s: %s)",
exc.__class__.__name__, exc)
pathfmt.realpath = pathfmt.temppath
else:
if self.delete:
pathfmt.delete = True
else:
pathfmt.set_extension("zip")
def _exec(self, args):
out = None if self.output else subprocess.DEVNULL
return subprocess.Popen(args, stdout=out, stderr=out).wait()
@staticmethod
def calculate_framerate(framelist):
counter = collections.Counter(frame["delay"] for frame in framelist)
fps = "1000/{}".format(min(counter))
return (fps, None) if len(counter) == 1 else (None, fps)
__postprocessor__ = UgoiraPP