Make things work. Make the orchestrator work.
Chris Pressey
7 years ago
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | |
4 | import yaml | |
5 | try: | |
6 | from yaml import CLoader as Loader | |
7 | except ImportError: | |
8 | from yaml import Loader | |
9 | ||
10 | from kinoje.utils import LoggingExecutor | |
11 | from kinoje.expander import load_config_file | |
4 | from kinoje.utils import LoggingExecutor, load_config_file | |
12 | 5 | |
13 | 6 | |
14 | 7 | SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif') |
98 | 91 | |
99 | 92 | config = load_config_file(options.configfile) |
100 | 93 | |
101 | exe = LoggingExecutor(os.path.join(options.framesdir, 'movie.log')) | |
94 | exe = LoggingExecutor('compiler.log') | |
102 | 95 | |
103 | 96 | compiler = { |
104 | 97 | '.gif': GifCompiler, |
5 | 5 | |
6 | 6 | from jinja2 import Template |
7 | 7 | |
8 | from kinoje.utils import LoggingExecutor, fmod, tween | |
9 | ||
10 | ||
11 | def load_config_file(filename): | |
12 | import yaml | |
13 | try: | |
14 | from yaml import CLoader as Loader | |
15 | except ImportError: | |
16 | from yaml import Loader | |
17 | ||
18 | with open(filename, 'r') as file_: | |
19 | config = yaml.load(file_, Loader=Loader) | |
20 | ||
21 | template = Template(config['template']) | |
22 | config['start'] = float(config.get('start', 0.0)) | |
23 | config['stop'] = float(config.get('stop', 1.0)) | |
24 | config['fps'] = float(config.get('fps', 25.0)) | |
25 | config['width'] = float(config.get('width', 320.0)) | |
26 | config['height'] = float(config.get('height', 200.0)) | |
27 | ||
28 | duration = config['duration'] | |
29 | start = config['start'] | |
30 | stop = config['stop'] | |
31 | fps = config['fps'] | |
32 | ||
33 | config['start_time'] = start * duration | |
34 | config['stop_time'] = stop * duration | |
35 | config['requested_duration'] = config['stop_time'] - config['start_time'] | |
36 | config['num_frames'] = int(config['requested_duration'] * fps) | |
37 | config['t_step'] = 1.0 / (duration * fps) | |
38 | ||
39 | return config | |
8 | from kinoje.utils import LoggingExecutor, fmod, tween, load_config_file | |
40 | 9 | |
41 | 10 | |
42 | 11 | class Expander(object): |
81 | 50 | options = argparser.parse_args(sys.argv[1:]) |
82 | 51 | |
83 | 52 | config = load_config_file(options.configfile) |
53 | template = Template(config['template']) | |
84 | 54 | |
85 | 55 | exe = LoggingExecutor('movie.log') |
86 | 56 | |
88 | 58 | |
89 | 59 | t = config['start'] |
90 | 60 | t_step = config['t_step'] |
91 | for frame in xrange(num_frames): | |
61 | for frame in xrange(config['num_frames']): | |
92 | 62 | expander.fillout_template(frame, t) |
93 | 63 | t += t_step |
94 | 64 |
0 | """\ | |
1 | kinoje {options} input-file.yaml output-dir/ | |
2 | ||
3 | Create a sequence of text file descriptions of frames of a movie from the | |
4 | configuration (which incl. a master template) in the given YAML file.""" | |
5 | ||
6 | # Note: just about everything here is subject to change! | |
7 | ||
8 | 0 | from datetime import datetime, timedelta |
9 | 1 | from argparse import ArgumentParser |
10 | 2 | import os |
12 | 4 | import sys |
13 | 5 | from tempfile import mkdtemp |
14 | 6 | |
15 | from jinja2 import Template | |
16 | import yaml | |
17 | try: | |
18 | from yaml import CLoader as Loader | |
19 | except ImportError: | |
20 | from yaml import Loader | |
7 | from kinoje.utils import Executor, load_config_file | |
21 | 8 | |
22 | from kinoje.utils import LoggingExecutor | |
9 | ||
10 | SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif') | |
23 | 11 | |
24 | 12 | |
25 | 13 | def main(): |
29 | 17 | help='A YAML file containing the template to render for each frame, ' |
30 | 18 | 'as well as configuration for rendering the template.' |
31 | 19 | ) |
20 | argparser.add_argument('output', metavar='FILENAME', type=str, | |
21 | help='The movie file to create. The extension of this filename ' | |
22 | 'determines the output format and must be one of %r. ' | |
23 | 'If not given, a default name will be chosen based on the ' | |
24 | 'configuration filename with a .mp4 extension added.' % (SUPPORTED_OUTPUT_FORMATS,) | |
25 | ) | |
32 | 26 | |
33 | infilename = options.configfile | |
34 | outfilename = options.output | |
35 | if options.output is None: | |
36 | (inbase, inext) = os.path.splitext(os.path.basename(infilename)) | |
37 | outfilename = inbase + '.mp4' | |
38 | (whatever, outext) = os.path.splitext(outfilename) | |
39 | if outext not in SUPPORTED_OUTPUT_FORMATS: | |
40 | raise ValueError("%s not a supported output format (%r)" % (outext, SUPPORTED_OUTPUT_FORMATS)) | |
27 | options = argparser.parse_args(sys.argv[1:]) | |
41 | 28 | |
42 | with open(infilename, 'r') as file_: | |
43 | config = yaml.load(file_, Loader=Loader) | |
29 | exe = Executor() | |
44 | 30 | |
45 | if options.config is not None: | |
46 | settings = {} | |
47 | for setting_string in options.config.split(','): | |
48 | key, value = setting_string.split(':') | |
49 | if re.match(r'^\d*\.?\d*$', value): | |
50 | value = float(value) | |
51 | settings[key] = value | |
52 | config.update(settings) | |
31 | instants_dir = mkdtemp() | |
32 | frames_dir = mkdtemp() | |
53 | 33 | |
54 | template = Template(config['template']) | |
55 | ||
56 | text_files_dir = mkdtemp() | |
57 | images_dir = mkdtemp() | |
58 | ||
59 | duration = options.duration | |
60 | if duration is None: | |
61 | duration = config['duration'] | |
62 | ||
63 | if 'render_command_template' not in config: | |
64 | render_type = config.get('type', 'povray') | |
65 | if render_type == 'povray': | |
66 | config['render_command_template'] = "povray +L{indir} -D +I{infile} +O{outfile} +W{width} +H{height} +A" | |
67 | elif render_type == 'svg': | |
68 | config['render_command_template'] = "inkscape -z -e {outfile} -w {width} -h {height} {infile}" | |
69 | else: | |
70 | raise NotImplementedError | |
71 | ||
72 | start_time = options.start * duration | |
73 | stop_time = options.stop * duration | |
74 | requested_duration = stop_time - start_time | |
75 | num_frames = int(requested_duration * options.fps) | |
76 | t_step = 1.0 / (duration * options.fps) | |
77 | ||
78 | print "Start time: t=%s, %s seconds" % (options.start, start_time) | |
79 | print "Stop time: t=%s, %s seconds" % (options.stop, stop_time) | |
80 | print "Requested duration: %s seconds" % requested_duration | |
81 | print "Frame rate: %s fps" % options.fps | |
82 | print "Number of frames: %s (rounded to %s)" % (requested_duration * options.fps, num_frames) | |
83 | print "t-Step: %s" % t_step | |
84 | ||
85 | exe = LoggingExecutor(os.path.join(text_files_dir, 'movie.log')) | |
86 | t = options.start | |
87 | ||
88 | started_at = datetime.now() | |
89 | ||
90 | expander = TemplateExpander(text_files_dir, template, config, options, exe) | |
91 | for frame in xrange(num_frames): | |
92 | expander.fillout_template(frame, t) | |
93 | t += t_step | |
34 | exe.do_it("kinoje-expand {} {}".format(options.configfile, instants_dir)) | |
35 | exe.do_it("kinoje-render {} {} {}".format(options.configfile, instants_dir, frames_dir)) | |
36 | exe.do_it("kinoje-compile {} {} {}".format(options.configfile, frames_dir, options.output)) | |
94 | 37 | |
95 | 38 | exe.close() |
96 | ||
97 | run_duration = finished_at - started_at | |
98 | print "Finished, took %s seconds" % run_duration.total_seconds() |
8 | 8 | except ImportError: |
9 | 9 | from yaml import Loader |
10 | 10 | |
11 | from kinoje.utils import LoggingExecutor | |
12 | ||
13 | ||
14 | SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif') | |
11 | from kinoje.utils import LoggingExecutor, load_config_file | |
15 | 12 | |
16 | 13 | |
17 | 14 | class Renderer(object): |
65 | 62 | |
66 | 63 | exe = LoggingExecutor('movie.log') |
67 | 64 | |
68 | command_template = '???' | |
69 | 65 | renderer = Renderer(config['command_template'], options.instantsdir, options.framesdir, exe) |
70 | 66 | renderer.render_all() |
71 | 67 | |
72 | 68 | exe.close() |
73 | ||
74 | #run_duration = finished_at - started_at | |
75 | #print "Finished, took %s seconds" % run_duration.total_seconds() |
0 | 0 | import sys |
1 | 1 | from subprocess import check_call |
2 | ||
3 | ||
4 | def load_config_file(filename): | |
5 | import yaml | |
6 | try: | |
7 | from yaml import CLoader as Loader | |
8 | except ImportError: | |
9 | from yaml import Loader | |
10 | ||
11 | with open(filename, 'r') as file_: | |
12 | config = yaml.load(file_, Loader=Loader) | |
13 | ||
14 | config['start'] = float(config.get('start', 0.0)) | |
15 | config['stop'] = float(config.get('stop', 1.0)) | |
16 | config['fps'] = float(config.get('fps', 25.0)) | |
17 | config['width'] = float(config.get('width', 320.0)) | |
18 | config['height'] = float(config.get('height', 200.0)) | |
19 | ||
20 | duration = config['duration'] | |
21 | start = config['start'] | |
22 | stop = config['stop'] | |
23 | fps = config['fps'] | |
24 | ||
25 | config['start_time'] = start * duration | |
26 | config['stop_time'] = stop * duration | |
27 | config['requested_duration'] = config['stop_time'] - config['start_time'] | |
28 | config['num_frames'] = int(config['requested_duration'] * fps) | |
29 | config['t_step'] = 1.0 / (duration * fps) | |
30 | ||
31 | return config | |
2 | 32 | |
3 | 33 | |
4 | 34 | class LoggingExecutor(object): |
18 | 48 | |
19 | 49 | def close(self): |
20 | 50 | self.log.close() |
51 | ||
52 | ||
53 | class Executor(object): | |
54 | def do_it(self, cmd, **kwargs): | |
55 | print cmd | |
56 | check_call(cmd, shell=True, **kwargs) | |
57 | ||
58 | def close(self): | |
59 | pass | |
21 | 60 | |
22 | 61 | |
23 | 62 | def fmod(n, d): |