This repository was archived by the owner on Feb 23, 2021. It is now read-only.
forked from slackhq/magic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommando-runger
151 lines (121 loc) · 4.84 KB
/
commando-runger
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
#!/usr/bin/env ruby
require_relative 'commando-helpers.rb'
program :description, 'Takes a batch of frame sequences and renders them to individual lossless video files'
program :version, '1.0.0'
def filename(id, prefix='', suffix='')
"#{prefix}#{sprintf "%05d", id}#{suffix}"
end
command :sort do |c|
c.description = 'Sorts files in <input> in place'
c.option '--input DIR', String, 'Input directory'
c.option '--match GLOB', String, 'Glob for filename search'
c.action do |args, options|
options.default \
input: "./input",
match: "*.png"
options.input = File.expand_path(options.input)
# TODO: make file pattern an option
prefix = "#{File.basename(options.input)}_"
command_header(c, options)
if ! File.directory?(options.input)
crash "Input directory '#{options.input}' does not exist"
end
frames = Dir.glob("#{options.input}/#{options.match}")
if frames.length == 0
crash "Input directory '#{options.input}' does not contain any '#{options.match}' files"
end
first = File.basename(frames.first).split(".").first
first_match = filename(0, prefix)
last = File.basename(frames.last).split(".").first
last_match = filename(frames.length - 1, prefix)
if first == first_match && last == last_match
success "Input directory #{options.input} does not need sorting"
exit
end
i = 0
Dir.glob("#{options.input}/#{options.match}") do |frame|
File.rename(frame, "#{options.input}/#{filename(i, prefix) + File.extname(frame)}")
i += 1
end
end
end
command :render do |c|
c.description = 'Renders frame sequences in <input> to new file in <output>. Will overwrite existing file with same name.'
c.option '--input DIR', String, 'Input directory'
c.option '--output DIR', String, 'Output directory'
c.option '--outfile FILE', String, 'Output filename, without extension (will be written to output)'
c.option '--fps INT', Integer, 'FPS to use for render'
c.option '--qscale INT', Integer, 'Quality scale. 0 is best, 32 which is worst. Sweetspot is 9 - 13'
c.option '--size STRING', String, 'Frame size'
c.action do |args, options|
options.default \
input: "./input",
output: ".",
fps: 30,
qscale: 13,
size: "1920x1080"
options.input = File.expand_path(options.input)
options.output = File.expand_path(options.output)
options.outfile = "#{options.outfile || File.basename(options.input)}.mov"
command_header(c, options)
if ! File.directory?(options.input)
crash "Input directory '#{options.input}' does not exist"
end
if ! File.directory?(options.output)
crash "Output directory '#{options.output}' does not exist"
end
frames = Dir.glob("#{options.input}/*.png")
if frames.length == 0
crash "#{options.input} does not contain any *.png files"
end
Terrapin::CommandLine.runner = Terrapin::CommandLine::PopenRunner.new
ffmpeg = Terrapin::CommandLine.new("ffmpeg", "-loglevel panic -stats -y -probesize 5000000 -f image2 -r :fps -pattern_type glob -i :input -c:v prores_ks -profile:v 3 -qscale:v :qscale -vendor ap10 -pix_fmt yuv422p10le -s :size -r :fps -force_fps :output")
begin
notice "Rendering #{options.input}/*.png (#{frames.length} frames)..."
ffmpeg.run(
input: "#{options.input}/*.png",
output: "#{options.output}/#{options.outfile}",
fps: options.fps,
qscale: options.qscale,
size: options.size
)
success "Rendered to #{options.output}/#{options.outfile}"
rescue Terrapin::ExitStatusError => e
warning e.message
end
end
end
command :loop do |c|
c.description = 'Sorts sequences in <input> and renders them to <output>'
c.option '--input DIR', String, 'Input directory'
c.option '--output DIR', String, 'Output directory'
c.action do |args, options|
options.default \
input: "./input",
output: "./output"
options.input = File.expand_path(options.input)
options.output = File.expand_path(options.output)
command_header(c, options)
if ! File.directory?(options.input)
crash "Input directory '#{options.input}' does not exist"
end
if ! File.directory?(options.output)
warning "Output directory '#{options.output}' does not exist, creating it..."
Dir.mkdir(options.output)
end
Dir.glob("#{options.input}/*") do |shot|
shot_id = shot.strip.split('/').last
if File.exists?("#{options.output}/#{shot_id}.mov")
puts "Skipping shot #{shot_id}, #{options.output}/#{shot_id}.mov already exists."
next
end
system "commando", "runger", "sort", "--input", shot
if $?.exitstatus == 0
system "commando", "runger", "render", "--input", shot, "--output", options.output
else
warning "`commando runger sort` failed on #{shot}"
end
end
end
end
default_command :loop