-
Notifications
You must be signed in to change notification settings - Fork 13
/
pulverize.php
executable file
·332 lines (267 loc) · 9.91 KB
/
pulverize.php
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
#! /usr/bin/env php
<?php
date_default_timezone_set(@date_default_timezone_get());
echo "Pulverize - A multi-process rendering script for Blender VSE.\n";
echo "Version 1.1\n";
echo "Copyright 2017 Hunter Perrin\n";
echo "Licensed under GPL, just like Blender.\n";
if (!isset($argv[1])) {
die(
"\n" .
"\nUsage: pulverize.php <blender_project_file> [<number_of_processes>] [<options>]" .
"\n" .
"\nExample: pulverize.php project.blend 6 '{\"keepTempFiles\":true,\"displayStdErr\":true}'" .
"\n" .
"\nOptions are given in JSON format as an object. (They should be flags, but that's a TODO for another time.)" .
"\n keepTempFiles defaults to false. When true, the frame range renders and the FFMPEG input script won't be deleted." .
"\n displayStdErr defaults to false. When true, StdErr stream from the blender processes will be displayed along with the Pulverize progress indicator. FFMPEG will also show warnings, not just errors." .
"\n" .
"\n"
);
}
if (PHP_OS == "WINNT") {
$lineWidth = (int) exec('powershell ^(Get-Host^).UI.RawUI.WindowSize.width');
} else {
$lineWidth = (int) exec('tput cols');
}
$processCountArg = null;
$optionsArg = null;
$blenderFile = $argv[1];
if (isset($argv[2])) {
$processCountArg = is_numeric($argv[2]) ? (int) $argv[2] : null;
$optionsArg =
is_object(json_decode($argv[2]))
? $argv[2]
: ((isset($argv[3]) && is_object(json_decode($argv[3])))
? $argv[3]
: 3);
}
$toolScript = __DIR__.'/pulverize_tool.py';
if (!file_exists($blenderFile)) {
die("You didn't give me a valid Blender project file.\n");
}
if (!file_exists($toolScript)) {
die("My tool script 'pulverize_tool.py' is missing.\n");
}
$shellBlenderFile = escapeshellarg($blenderFile);
$shellToolScript = escapeshellarg($toolScript);
if (PHP_OS == "WINNT") {
$projectInfo = shell_exec("blender -b $shellBlenderFile -P $shellToolScript 2> nul");
} else {
$projectInfo = shell_exec("blender -b $shellBlenderFile -P $shellToolScript 2>/dev/null");
}
preg_match('/^FRAMES: (\d+) (\d+)$/m', $projectInfo, $matches);
list($startFrame, $endFrame) = array_slice($matches, 1, 2);
preg_match('/^OUTPUTDIR: (.+)$/m', $projectInfo, $matches);
$outputDir = $matches[1];
// Add support for Blender's blend files path notation
if (substr($outputDir, 0, 2) === "//") {
$path_parts = pathinfo($blenderFile);
$blenderFilePath = realpath($path_parts['dirname']);
$outputDir = str_replace("//", $blenderFilePath . DIRECTORY_SEPARATOR, $outputDir);
}
$shellOutputDir = escapeshellarg($outputDir);
if (!is_dir($outputDir)) {
die("This script only works if your project's output is set to a directory. Please set it to a directory and try again.\n");
}
$frameLength = $endFrame - $startFrame + 1;
// Use half the number of logical processors reported by the system, with a max of 6.
// Added support for Windows 10 and MacOS
if (PHP_OS == "WINNT") {
$processors = (int) shell_exec("echo %NUMBER_OF_PROCESSORS%");
} else if (PHP_OS == "Darwin") {
$processors = (int) shell_exec("sysctl -n hw.ncpu");
} else {
$processors = (int) shell_exec("cat /proc/cpuinfo | egrep \"^processor\" | wc -l");
}
$processCount = min(floor($processors / 2), 6);
if ($processCountArg && $processCountArg <= $processors) {
$processCount = $processCountArg;
}
$processFrameCount = floor($frameLength / $processCount);
$remainderFrames = $frameLength % $processCount;
$options = [
'keepTempFiles' => false,
'displayStdErr' => false,
];
if ($optionsArg) {
foreach (json_decode($optionsArg) as $key => $value) {
$options[$key] = $value;
}
}
echo <<<EOF
It looks like your machine has $processors logical processor(s). The default is to use half the number of logical processors reported by the system, with a max of 6.
Read from Blender file --
startFrame: $startFrame
endFrame: $endFrame
outputDir: $outputDir
Calculated these values for render --
frameLength: $frameLength
processCount: $processCount
processFrameCount: $processFrameCount
remainderFrames: $remainderFrames
Each process will render $processFrameCount frames, except the last will render an extra $remainderFrames frame(s).
EOF;
echo_header("Step 1/2 Rendering with Blender");
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$processes = [];
$pipes = [];
$startTime = time();
// Start all the blender forks.
for ($i = 0; $i < $processCount; $i++) {
$s = $startFrame + ($processFrameCount * $i);
$e = $s + $processFrameCount - 1;
if ($i == $processCount - 1) {
// In the last process, add the remainder frames.
$e += $remainderFrames;
}
//$stdoutLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "pulverize_stdout$i.log";
$stdoutLogFile = tempnam(sys_get_temp_dir(), 'PLV');
//$stderrLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "pulverize_stderr$i.log";
$stderrLogFile = tempnam(sys_get_temp_dir(), 'PLV');
$handle = proc_open("blender -b $shellBlenderFile -s $s -e $e -o {$shellOutputDir}pulverize_frames_####### -a > $stdoutLogFile 2> $stderrLogFile", $descriptorspec, $pipes[$i]);
usleep(250000);
$stdoutHandle = fopen($stdoutLogFile, "r");
$stderrHandle = fopen($stderrLogFile, "r");
$processes[$i] = [
'handle' => $handle,
's' => $s,
'e' => $e,
'frame' => $s,
'stdoutHandle' => $stdoutHandle,
'stderrHandle' => $stderrHandle
];
}
// Monitor them and print a progress bar.
echo "\n\n";
do {
usleep(250000);
$done = true;
foreach ($processes as $curI => &$curProcess) {
$status = proc_get_status($curProcess['handle']);
if ($status['running']) {
$done = false;
} elseif (!isset($curProcess['time'])) {
$curProcess['time'] = time() - $startTime;
}
$stderr = stream_get_contents($curProcess['stderrHandle']);
if ($stderr && $options['displayStdErr']) {
echo "\n\n--------------- StdErr Processs: $curI\n";
echo $stderr;
}
$stdout = stream_get_contents($curProcess['stdoutHandle']);
if ($stdout) {
preg_match('/^Append frame (\d+)/m', $stdout, $matches);
if ($matches) {
$curProcess['frame'] = (int) $matches[1];
}
}
}
$completedFrames = 0;
foreach ($processes as $curProcess) {
$completedFrames += $curProcess['frame'] - $curProcess['s'];
}
// Show a progress indicator.
echo_progress_bar($completedFrames);
} while (!$done);
echo_progress_bar($frameLength);
echo_header("Step 2/2 Concatinating videos with FFMPEG");
if (PHP_OS == "WINNT") {
$fileLsOutput = shell_exec("cd $shellOutputDir && dir /b /a-d pulverize_frames_*");
} else {
$fileLsOutput = shell_exec("cd $shellOutputDir && ls -1 pulverize_frames_*");
}
$files = explode("\n", trim($fileLsOutput));
if (!$files) {
die("Something went wrong, and I can't find the video files. Check to see if the render worked.");
}
$fileList = "file ".implode("\nfile ", $files);
file_put_contents("$outputDir/pulverize_input_files.txt", $fileList);
$ext = explode(".", $files[0], 2)[1];
$startFramePadded = str_pad($startFrame, 7, '0', STR_PAD_LEFT);
$endFramePadded = str_pad($endFrame, 7, '0', STR_PAD_LEFT);
$ffmpegCommand = "ffmpeg" .
($options['displayStdErr'] ? "" : " -v error") .
" -y -stats -f concat -i pulverize_input_files.txt -c copy $startFramePadded-$endFramePadded.$ext";
echo "$ $ffmpegCommand\n";
passthru("cd $shellOutputDir && $ffmpegCommand");
if (!$options['keepTempFiles']) {
echo "\nRemoving temporary video files...\n";
unlink("$outputDir/pulverize_input_files.txt");
foreach ($files as $curFile) {
unlink("$outputDir/$curFile");
}
}
echo_header("All done!");
$totalSeconds = time() - $startTime;
$from = new DateTime;
$to = clone $from;
$to = $to->add(new DateInterval("PT{$totalSeconds}S"));
$diff = $from->diff($to);
$totalTime = $diff->format('%h:%I:%S');
$blenderSeconds = 0;
foreach ($processes as $curProcess) {
$blenderSeconds += $curProcess['time'];
}
$from = new DateTime;
$to = clone $from;
$to = $to->add(new DateInterval("PT{$blenderSeconds}S"));
$diff = $from->diff($to);
$blenderTime = $diff->format('%h:%I:%S');
$savedSeconds = $blenderSeconds - $totalSeconds;
$from = new DateTime;
$to = clone $from;
$to = $to->add(new DateInterval("PT{$savedSeconds}S"));
$diff = $from->diff($to);
$savedTime = $diff->format('%h:%I:%S');
echo <<<EOF
Total time: $totalTime
Blender time: $blenderTime
You saved this much time by running this script instead of rendering directly from Blender's VSE:
$savedTime
EOF;
function echo_header($text) {
global $lineWidth;
echo "\n\n\n";
echo str_repeat("#", $lineWidth)."\n";
echo "# $text".str_repeat(" ", $lineWidth - strlen("# $text") - 1)."#\n";
echo str_repeat("#", $lineWidth)."\n";
echo "\n\n";
}
function echo_progress_bar($completedFrames) {
global $lineWidth, $frameLength, $startTime;
echo "\r";
echo "\033[2K";
echo "\x1b[A";
echo "\033[2K";
echo "\x1b[A";
echo "\033[2K";
$progress = $completedFrames / $frameLength;
$percent = number_format($progress * 100, 2);
echo "Progress: $completedFrames / $frameLength frames, $percent%\n";
$elapsedSeconds = time() - $startTime;
$from = new DateTime;
$to = clone $from;
$to = $to->add(new DateInterval("PT{$elapsedSeconds}S"));
$diff = $from->diff($to);
$elapsedTime = $diff->format('%h:%I:%S');
if ($progress > 0) {
$totalSeconds = floor($elapsedSeconds / $progress);
$remainingSeconds = $totalSeconds - $elapsedSeconds;
$from = new DateTime;
$to = clone $from;
$to = $to->add(new DateInterval("PT{$remainingSeconds}S"));
$diff = $from->diff($to);
$remainingTime = $diff->format('%h:%I:%S');
} else {
$remainingTime = 'Unknown';
}
echo "Elapsed time: $elapsedTime, Remaining time: $remainingTime\n";
$charCount = max(floor($lineWidth * $progress) - 1, 0);
$progressBar = str_repeat("=", $charCount);
echo "{$progressBar}>";
}