/** * Render the actual video frames * * @param mixed $tests * @param mixed $frameCount * @param mixed $im */ function RenderFrames(&$tests, $frameCount, $im) { global $width, $height, $backgroundColor, $videoPath, $image_bytes, $textColor, $encodeFormat, $encoderSpeed, $fps, $labelHeight, $forceBackgroundColor, $forceTextColor, $combineTimeLabel, $evenTextBackground, $oddTextBackground, $bgEvenText, $bgOddText; // allocate the background and foreground colors $bgcolor = isset($tests[0]['bg']) ? html2rgb($tests[0]['bg']) : html2rgb('000000'); $color = isset($tests[0]['text']) ? html2rgb($tests[0]['text']) : html2rgb('ffffff'); if (isset($forceBackgroundColor)) { $bgcolor = html2rgb($forceBackgroundColor); } if (isset($forceTextColor)) { $color = html2rgb($forceTextColor); } $bgEvenTextColor = isset($evenTextBackground) ? html2rgb($evenTextBackground) : $bgcolor; $bgOddTextColor = isset($oddTextBackground) ? html2rgb($oddTextBackground) : $bgcolor; // prepare the image $backgroundColor = imagecolorallocate($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]); $textColor = imagecolorallocate($im, $color[0], $color[1], $color[2]); $bgEvenText = imagecolorallocate($im, $bgEvenTextColor[0], $bgEvenTextColor[1], $bgEvenTextColor[2]); $bgOddText = imagecolorallocate($im, $bgOddTextColor[0], $bgOddTextColor[1], $bgOddTextColor[2]); imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backgroundColor); // figure out what a good interval for keyframes would be based on the video length $keyInt = min(max(6, $frameCount / 30), 240); // set up ffmpeg $descriptors = array(0 => array("pipe", "r")); $videoFile = realpath($videoPath) . '/render.mp4'; if (is_file($videoFile)) { unlink($videoFile); } $codec = $encodeFormat == 'jpg' ? 'mjpeg' : $encodeFormat; $command = "ffmpeg -f image2pipe -vcodec {$codec} -r {$fps} -i - " . "-vcodec libx264 -r {$fps} -crf 24 -g {$keyInt} " . "-preset {$encoderSpeed} -y \"{$videoFile}\""; $ffmpeg = proc_open($command, $descriptors, $pipes); if (is_resource($ffmpeg)) { if ($labelHeight > 0 || $combineTimeLabel) { DrawLabels($tests, $im); } for ($frame = 0; $frame < $frameCount; $frame++) { RenderFrame($tests, $frame, $im); if (isset($image_bytes)) { fwrite($pipes[0], $image_bytes); } } fclose($pipes[0]); proc_close($ffmpeg); } }
/** * Render the actual video frames * * @param mixed $tests * @param mixed $frameCount * @param mixed $im */ function RenderFrames(&$tests, $frameCount, $im) { global $width, $height, $black, $videoPath, $image_bytes, $textColor, $encodeFormat, $encoderSpeed, $fps, $labelHeight; // prepare the image (black background) $black = imagecolorallocate($im, 0, 0, 0); $textColor = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $black); // figure out what a good interval for keyframes would be based on the video length $keyInt = min(max(6, $frameCount / 30), 240); // set up ffmpeg $descriptors = array(0 => array("pipe", "r")); $videoFile = realpath($videoPath) . '/render.mp4'; if (is_file($videoFile)) { unlink($videoFile); } $codec = $encodeFormat == 'jpg' ? 'mjpeg' : $encodeFormat; $command = "ffmpeg -f image2pipe -vcodec {$codec} -r {$fps} -i - " . "-vcodec libx264 -r {$fps} -crf 24 -g {$keyInt} " . "-preset {$encoderSpeed} -y \"{$videoFile}\""; $ffmpeg = proc_open($command, $descriptors, $pipes); if (is_resource($ffmpeg)) { if ($labelHeight > 0) { DrawLabels($tests, $im); } for ($frame = 0; $frame < $frameCount; $frame++) { RenderFrame($tests, $frame, $im); if (isset($image_bytes)) { fwrite($pipes[0], $image_bytes); } } fclose($pipes[0]); proc_close($ffmpeg); } }