Skip to content

Commit

Permalink
Updated ffmpeg command. Fixed audio sync problem for long videos.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Feb 22, 2017
1 parent b1d284f commit 017ed3c
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions pulverize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
<?php

echo "Pulverize - A multi-process rendering script for Blender VSE.\n";
echo "Version 1.0\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>]\n\n");
die("\n\nUsage: pulverize.php <blender_project_file> [<number_of_processes>] [<options>]\nExample: pulverize project.blend 6 '{\"keepTempFiles\":true,\"displayStdErr\":true}
'\n\n");
}

$lineWidth = (int) exec('tput cols');

$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");
Expand All @@ -38,14 +50,23 @@
$frameLength = $endFrame - $startFrame + 1;
// Use half the number of logical processors reported by the system, with a max of 6.
$processors = (int) shell_exec("cat /proc/cpuinfo | egrep \"^processor\" | wc -l");
$processCountArg = (int) $argv[2];
$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.
Expand Down Expand Up @@ -89,7 +110,7 @@
$e += $remainderFrames;
}

$handle = proc_open("blender -b $shellBlenderFile -s $s -e $e -o {$shellOutputDir}blender_render_frames_####### -a", $descriptorspec, $pipes[$i]);
$handle = proc_open("blender -b $shellBlenderFile -s $s -e $e -o {$shellOutputDir}pulverize_frames_####### -a", $descriptorspec, $pipes[$i]);
$processes[$i] = [
'handle' => $handle,
's' => $s,
Expand All @@ -114,9 +135,9 @@
$curProcess['time'] = time() - $startTime;
}
$stderr = stream_get_contents($pipes[$curI][2]);
if ($stderr) {
//echo "\n\n--------------- StdErr Processs: $curI\n";
//echo $stderr;
if ($stderr && $options['displayStdErr']) {
echo "\n\n--------------- StdErr Processs: $curI\n";
echo $stderr;
}
$stdout = stream_get_contents($pipes[$curI][1]);
if ($stdout) {
Expand All @@ -143,21 +164,28 @@

echo_header("Step 2/2 Concatinating videos with FFMPEG");

$fileLsOutput = shell_exec("cd $shellOutputDir && ls -1 blender_render_frames_*");
$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 = escapeshellarg(implode("|", $files));
$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);
echo "$ ffmpeg -v error -stats -i concat:$fileList -c copy $startFramePadded-$endFramePadded.$ext\n";
passthru("cd $shellOutputDir && ffmpeg -v error -stats -i concat:$fileList -c copy $startFramePadded-$endFramePadded.$ext");

echo "\nRemoving temporary video files...\n";
foreach ($files as $curFile) {
unlink("$outputDir/$curFile");
$ffmpegCommand = "ffmpeg" .
($options['displayStdErr'] ? "" : " -v error") .
" -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");
}
}


Expand Down

0 comments on commit 017ed3c

Please sign in to comment.