Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commands.py « bpsrender « BPSRender « scripts « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 910ff021213b7a7ba5853f46e6b8ba603c68ede5 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#
# Copyright (C) 2016-2019 by Razvan Radulescu, Nathan Lovato, and contributors
#
# This file is part of Power Sequencer.
#
# Power Sequencer is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Power Sequencer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Power Sequencer. If
# not, see <https://www.gnu.org/licenses/>.
#
import math as m
from collections import OrderedDict
from itertools import chain, islice

from .config import LOGGER


def get_commands_probe(cfg, clargs, **kwargs):
    """
    Create the command for probing the `clargs.blendfile`.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(list)
    An iterator for which each element is a list to be sent to functions like
    `subprocess.run`.
    """
    out = (
        "blender",
        "--background",
        clargs.blendfile,
        "--python",
        kwargs["probe_py_normalized"],
        "--disable-autoexec",
    )
    LOGGER.debug("CMD-PROBE: {cmd}".format(cmd=" ".join(out)))
    return iter((out,))


def get_commands_chunk(cfg, clargs, **kwargs):
    """
    Create the command for rendering a (video) chunk from `clargs.blendfile`.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY render_chunk_path, w_frame_start, w_frame_end
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(list)
    An iterator for which each element is a list to be sent to functions like
    `subprocess.run`.
    """
    out = (
        "blender",
        "--background",
        clargs.blendfile,
        "--python",
        kwargs["video_py_normalized"],
        "--disable-autoexec",
        "--render-output",
        kwargs["render_chunk_path"],
        "-s",
        str(kwargs["w_frame_start"]),
        "-e",
        str(kwargs["w_frame_end"]),
        "--render-anim",
    )
    LOGGER.debug(
        "CMD-CHUNK({w_frame_start}-{w_frame_end}): {cmd}".format(cmd=" ".join(out), **kwargs)
    )
    return iter((out,))


def get_commands_video(cfg, clargs, **kwargs):
    """
    Create the list of commands (one command per chunk) for rendering a video
    from `clargs.blendfile`.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY chunk_file_path, frame_start, frame_end, frames_total
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(tuple)
    An iterator for which each element is a tuple to be sent to functions like
    `subprocess.run`.
    """
    LOGGER.debug("CMD-VIDEO:")
    chunk_length = int(m.floor(kwargs["frames_total"] / clargs.workers))
    out = map(lambda w: (w, kwargs["frame_start"] + w * chunk_length), range(clargs.workers))
    out = map(
        lambda x: (
            x[1],
            x[1] + chunk_length - 1 if x[0] != clargs.workers - 1 else kwargs["frame_end"],
        ),
        out,
    )
    out = map(
        lambda x: get_commands(
            cfg, clargs, "chunk", w_frame_start=x[0], w_frame_end=x[1], **kwargs
        ),
        out,
    )
    out = map(lambda x: x[1], out)
    out = chain(*out)
    return tuple(out)


def get_commands_mixdown(cfg, clargs, **kwargs):
    """
    Create the command to render the mixdown from `clargs.blendfile`.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY render_mixdown_path
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(tuple)
    An iterator for which each element is a tuple to be sent to functions like
    `subprocess.run`.
    """
    out = (
        "blender --background {blendfile} --python {mixdown_py_normalized}"
        " --disable-autoexec -- {render_mixdown_path}".format(**cfg, **vars(clargs), **kwargs)
    )
    out = (
        "blender",
        "--background",
        clargs.blendfile,
        "--python",
        kwargs["mixdown_py_normalized"],
        "--disable-autoexec",
        "--",
        kwargs["render_mixdown_path"],
    )
    LOGGER.debug("CMD-MIXDOWN: {cmd}".format(cmd=" ".join(out)))
    return iter((out,))


def get_commands_concatenate(cfg, clargs, **kwargs):
    """
    Create the command to concatenate the available video chunks generated
    beforehand.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY chunks_file_path, render_video_path
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(tuple)
    An iterator for which each element is a tuple to be sent to functions like
    `subprocess.run`.
    """
    out = (
        "ffmpeg",
        "-stats",
        "-f",
        "concat",
        "-safe",
        "-0",
        "-i",
        kwargs["chunks_file_path"],
        "-c",
        "copy",
        "-y",
        kwargs["render_video_path"],
    )
    LOGGER.debug("CMD-CONCATENATE: {cmd}".format(cmd=" ".join(out)))
    return iter((out,))


def get_commands_join(cfg, clargs, **kwargs):
    """
    Create the command to join the available audio mixdown and video generated
    beforehand.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY chunks_file_path, render_video_path
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter(tuple)
    An iterator for which each element is a tuple to be sent to functions like
    `subprocess.run`.
    """
    out = (
        "ffmpeg",
        "-stats",
        "-i",
        kwargs["render_video_path"],
        "-i",
        kwargs["render_mixdown_path"],
        "-map",
        "0:v:0",
        "-c:v",
        "copy",
        "-map",
        "1:a:0",
        "-c:a",
        "aac",
        "-b:a",
        "320k",
        "-y",
        kwargs["render_audiovideo_path"],
    )
    LOGGER.debug("CMD-JOIN: {cmd}".format(cmd=" ".join(out)))
    return iter((out,))


def get_commands(cfg, clargs, what="", **kwargs):
    """
    Delegates the creation of commands lists to appropriate functions based on
    `what` parameter.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    what: str (default = '')
    Determines the returned value (see: Returns[out]).
    kwargs: dict
    MANDATORY -- see individual functions for the list of mandatory keys
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter or (str, iter)
    |- what == '' is True
    An iterator with elements of the type (str) for determining the order in
    which to call the functions in the setup step.
    NOTE: it skipps the "internal use only" functions.
    |- else
    A tuple with the 1st element as a tag (the `what` parameter) and the 2nd
    element as the iterator of the actual commands.
    """
    get_commands_f = OrderedDict(
        (
            # internal use only
            ("probe", get_commands_probe),
            ("chunk", get_commands_chunk),
            # direct connection to command line arguments - in order of execution
            ("mixdown", get_commands_mixdown),
            ("video", get_commands_video),
            ("concatenate", get_commands_concatenate),
            ("join", get_commands_join),
        )
    )

    return (
        islice(get_commands_f, 2, None)
        if what == ""
        else (what, get_commands_f[what](cfg, clargs, **kwargs))
    )


def get_commands_all(cfg, clargs, **kwargs):
    """
    Prepare the list of commands to be executed depending on the command line
    arguments.

    Parameters
    ----------
    cfg: dict
    Configuration dictionary.
    clargs: Namespace
    Command line arguments (normalized).
    kwargs: dict
    MANDATORY -- see individual functions for the list of mandatory keys
    Dictionary with additional information from the setup step.

    Returns
    -------
    out: iter((str, tuple))
    An iterator for which each element is a (str, iter(tuple)). The string
    value is for tagging the iterator command list (2nd element) for filtering
    later based on the given command line arguments.
    """
    end = "_only"
    out = filter(lambda x: x[0].endswith(end), vars(clargs).items())
    out = map(lambda x: (x[0][: -len(end)], x[1]), out)
    order = list(get_commands(cfg, clargs))
    out = sorted(out, key=lambda x: order.index(x[0]))
    out = (
        map(lambda k: k[0], out)
        if all(map(lambda k: not k[1], out))
        else map(lambda k: k[0], filter(lambda k: k[1], out))
    )
    out = map(lambda k: get_commands(cfg, clargs, k, **kwargs), out)
    return out