git @ Cat's Eye Technologies kinoje / a51cb94
Make things work. Make the orchestrator work. Chris Pressey 7 years ago
5 changed file(s) with 61 addition(s) and 126 deletion(s). Raw diff Collapse all Expand all
11 import os
22 import sys
33
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
125
136
147 SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif')
9891
9992 config = load_config_file(options.configfile)
10093
101 exe = LoggingExecutor(os.path.join(options.framesdir, 'movie.log'))
94 exe = LoggingExecutor('compiler.log')
10295
10396 compiler = {
10497 '.gif': GifCompiler,
55
66 from jinja2 import Template
77
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
409
4110
4211 class Expander(object):
8150 options = argparser.parse_args(sys.argv[1:])
8251
8352 config = load_config_file(options.configfile)
53 template = Template(config['template'])
8454
8555 exe = LoggingExecutor('movie.log')
8656
8858
8959 t = config['start']
9060 t_step = config['t_step']
91 for frame in xrange(num_frames):
61 for frame in xrange(config['num_frames']):
9262 expander.fillout_template(frame, t)
9363 t += t_step
9464
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
80 from datetime import datetime, timedelta
91 from argparse import ArgumentParser
102 import os
124 import sys
135 from tempfile import mkdtemp
146
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
218
22 from kinoje.utils import LoggingExecutor
9
10 SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif')
2311
2412
2513 def main():
2917 help='A YAML file containing the template to render for each frame, '
3018 'as well as configuration for rendering the template.'
3119 )
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 )
3226
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:])
4128
42 with open(infilename, 'r') as file_:
43 config = yaml.load(file_, Loader=Loader)
29 exe = Executor()
4430
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()
5333
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))
9437
9538 exe.close()
96
97 run_duration = finished_at - started_at
98 print "Finished, took %s seconds" % run_duration.total_seconds()
88 except ImportError:
99 from yaml import Loader
1010
11 from kinoje.utils import LoggingExecutor
12
13
14 SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif')
11 from kinoje.utils import LoggingExecutor, load_config_file
1512
1613
1714 class Renderer(object):
6562
6663 exe = LoggingExecutor('movie.log')
6764
68 command_template = '???'
6965 renderer = Renderer(config['command_template'], options.instantsdir, options.framesdir, exe)
7066 renderer.render_all()
7167
7268 exe.close()
73
74 #run_duration = finished_at - started_at
75 #print "Finished, took %s seconds" % run_duration.total_seconds()
00 import sys
11 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
232
333
434 class LoggingExecutor(object):
1848
1949 def close(self):
2050 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
2160
2261
2362 def fmod(n, d):