🎥 Make videos programmatically with React
code improvement and readability update
Alex Fernandez6038f47a5df3
diff --git a/packages/lambda-php-example/src/render.php b/packages/lambda-php-example/src/render.phpindex df4812f9d9..bccd16f339 100644--- a/packages/lambda-php-example/src/render.php+++ b/packages/lambda-php-example/src/render.php@@ -1,44 +1,45 @@<?phprequire_once dirname(__DIR__) . '/vendor/autoload.php';+use Dotenv\Dotenv;use Remotion\LambdaPhp\PHPClient;use Remotion\LambdaPhp\RenderParams;-$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);+// Load environment variables+$dotenv = Dotenv::createImmutable(__DIR__);$dotenv->load();-// get all params+// Get environment variables$region = $_ENV['REMOTION_APP_REGION'];$functionName = $_ENV['REMOTION_APP_FUNCTION_NAME'];-$serveUrl = $_ENV["REMOTION_APP_SERVE_URL"];+$serveUrl = $_ENV['REMOTION_APP_SERVE_URL'];-// instantiate the client-$client = new PHPClient(- $region,- $serveUrl,- $functionName,- null);+// Instantiate the client+$client = new PHPClient($region, $serveUrl, $functionName, null);-// initiate the param object, customize as needed+// Initiate the param object and customize as needed$params = new RenderParams();+$params->setComposition('react-svg');-// define the composition to render-$params->setComposition("react-svg");+// Execute the render and get the response+$renderResponse = $client->renderMediaOnLambda($params);++// Output render response+print_r($renderResponse);-/* execute the render- ***+/****Response([bucketName] => remotionlambda-apsoutheast2-xxxx[renderId] => xxxxxxx) */-$renderResponse = $client->renderMediaOnLambda($params);--// Get the proggress-print_r($renderResponse);-$renderProgressResponse = $client->getRenderProgress($renderResponse->renderId, $renderResponse->bucketName);+// Get render progress+$renderId = $renderResponse->renderId;+$bucketName = $renderResponse->bucketName;+$renderProgressResponse = $client->getRenderProgress($renderId, $bucketName);+// Output render progress responseprint_r($renderProgressResponse);/**diff --git a/packages/lambda-php/src/PHPClient.php b/packages/lambda-php/src/PHPClient.phpindex 08ae741c59..d2e461984b 100644--- a/packages/lambda-php/src/PHPClient.php+++ b/packages/lambda-php/src/PHPClient.php@@ -1,8 +1,6 @@<?php-namespace Remotion\LambdaPhp;-require_once dirname(__DIR__) . '/vendor/autoload.php';use Aws\Lambda\LambdaClient;use Exception;use stdClass;@@ -14,11 +12,8 @@ class PHPClientprivate $serveUrl;private $functionName;- public function __construct(- string $region,- string $serveUrl,- string $functionName,- ? array $credential) {+ public function __construct(string $region, string $serveUrl, string $functionName, ? array $credential)+ {$this->client = LambdaClient::factory(['version' => 'latest','region' => $region,@@ -29,12 +24,12 @@ class PHPClient$this->setFunctionName($functionName);}- public function constructInternals(- RenderParams $render- ) {+ public function constructInternals(RenderParams $render)+ {if (empty($render->getComposition())) {- throw new ValidationException("'compostion' is required.");+ throw new ValidationException("'composition' is required.");}+$input = $this->serializeInputProps($render->getData(),$this->getRegion(),@@ -45,40 +40,20 @@ class PHPClient$render->setInputProps($input);$render->setServerUrl($this->getServeUrl());$render->setRegion($this->getRegion());- return ($render->serializeParams());-+ return $render->serializeParams();}public function renderMediaOnLambda(RenderParams $render) : ? stdClass{$params = $this->constructInternals($render);$params['type'] = 'start';- $result = $this->client->invoke([- 'InvocationType' => 'RequestResponse',- 'FunctionName' => $this->getFunctionName(),- 'Payload' => json_encode($params),- ]);- // Check if the invocation was successful- if ($result['StatusCode'] == 200) {- // Get the response from the invocation- $response = ($result['Payload']->getContents());-- if (isset($response->errorMessage)) {- // The Lambda function encountered an error- throw new Exception($response->errorMessage);- } else {- // The Lambda function was invoked successfully- return json_decode($response);- }- } else {- // The Lambda function encountered an error- throw new Exception("Failed to invoke Lambda function");- }+ $result = $this->invokeLambdaFunction(json_encode($params));+ return $this->handleLambdaResponse($result);}- public function getRenderProgress(string $renderId,- string $bucketName) : ? stdClass{+ public function getRenderProgress(string $renderId, string $bucketName) : ? stdClass+ {$params = array('renderId' => $renderId,'bucketName' => $bucketName,@@ -86,48 +61,50 @@ class PHPClient"version" => VERSION,);+ $result = $this->invokeLambdaFunction(json_encode($params));+ return $this->handleLambdaResponse($result);+ }++ private function invokeLambdaFunction(string $payload)+ {$result = $this->client->invoke(['InvocationType' => 'RequestResponse','FunctionName' => $this->getFunctionName(),- 'Payload' => json_encode($params),+ 'Payload' => $payload,]);- // Check if the invocation was successful- if ($result['StatusCode'] == 200) {- // Get the response from the invocation- $response = ($result['Payload']->getContents());-- if (isset($response->errorMessage)) {- // The Lambda function encountered an error- throw new Exception($response->errorMessage);- } else {- // The Lambda function was invoked successfully- return json_decode($response);- }- } else {- // The Lambda function encountered an error+ if ($result['StatusCode'] !== 200) {throw new Exception("Failed to invoke Lambda function");}++ return $result['Payload']->getContents();+ }++ private function handleLambdaResponse(string $response) : ? stdClass+ {+ $response = json_decode($response);++ if (isset($response->errorMessage)) {+ throw new Exception($response->errorMessage);+ }++ return $response;}private function serializeInputProps($inputProps, string $region, string $type, ? string $userSpecifiedBucketName) : array{try {$payload = json_encode($inputProps);-$MAX_INLINE_PAYLOAD_SIZE = $type === 'still' ? 5000000 : 200000;if (strlen($payload) > $MAX_INLINE_PAYLOAD_SIZE) {-throw new Exception(sprintf("Warning: inputProps are over %dKB (%dKB) in size.\n This is not currently supported.",round($MAX_INLINE_PAYLOAD_SIZE / 1000),ceil(strlen($payload) / 1024))-);-}return [@@ -171,5 +148,4 @@ class PHPClient{$this->functionName = $functionName;}-}diff --git a/packages/lambda-php/src/PHPClientTest.php b/packages/lambda-php/src/PHPClientTest.phpindex f9b7ad3ed7..6f8437f90b 100644--- a/packages/lambda-php/src/PHPClientTest.php+++ b/packages/lambda-php/src/PHPClientTest.php@@ -4,26 +4,30 @@ require_once dirname(__DIR__) . '/vendor/autoload.php';require_once __DIR__ . '/PHPClient.php';require_once __DIR__ . '/RenderParams.php';+use PHPUnit\Framework\TestCase;use Remotion\LambdaPhp\PHPClient;use Remotion\LambdaPhp\RenderParams;-class PHPClientTest extends PHPUnit\Framework\TestCase-+class PHPClientTest extends TestCase{public function testClient(){-$client = new PHPClient("us-east-1","testbed","remotion-render",- null);+ null+ );$params = new RenderParams();$params->setComposition("react-svg");+$internalParams = $client->constructInternals($params);+$this->assertEquals($client->getRegion(), "us-east-1");- print(json_encode($internalParams));+ $this->assertIsArray($internalParams);+ $this->assertNotEmpty($internalParams);+ print(json_encode($internalParams));}}diff --git a/packages/lambda-php/src/RenderParams.php b/packages/lambda-php/src/RenderParams.phpindex c45e8607f5..64fdda685a 100644--- a/packages/lambda-php/src/RenderParams.php+++ b/packages/lambda-php/src/RenderParams.php@@ -8,7 +8,7 @@ use VERSION;class RenderParams{- private $data = array();+ private $data = null;private $bucketName = null;private $region = null;private $outName = null;@@ -27,7 +27,7 @@ class RenderParamsprivate $logLevel = 'info';private $frameRange = null;private $timeoutInMilliseconds = 30000;- private $chromiumOptions;+ private $chromiumOptions = null;private $scale = 1;private $everyNthFrame = 1;private $numberOfGifLoops = 0;@@ -52,10 +52,10 @@ class RenderParams? array $data = null,? string $composition = 'main',string $codec = 'h264',- string $version = null,+ ? string $version = null,string $imageFormat = 'jpeg',- int $crf = null,- array $envVariables = null,+ ? int $crf = null,+ ? array $envVariables = null,? int $quality = null,int $maxRetries = 1,string $privacy = 'public',@@ -68,9 +68,7 @@ class RenderParams? int $everyNthFrame = 1,? int $numberOfGifLoops = 0,? int $concurrencyPerLambda = 1,- ? array $downloadBehavior = [- 'type' => 'play-in-browser',- ],+ ? array $downloadBehavior = null,? bool $muted = false,? bool $overwrite = false,? int $audioBitrate = null,@@ -104,7 +102,7 @@ class RenderParams$this->everyNthFrame = $everyNthFrame;$this->numberOfGifLoops = $numberOfGifLoops;$this->concurrencyPerLambda = $concurrencyPerLambda;- $this->downloadBehavior = $downloadBehavior;+ $this->downloadBehavior = $downloadBehavior ?? ['type' => 'play-in-browser'];$this->muted = $muted;$this->overwrite = $overwrite;$this->audioBitrate = $audioBitrate;@@ -114,7 +112,8 @@ class RenderParams$this->forceWidth = $forceWidth;$this->audioCodec = $audioCodec;$this->dumpBrowserLogs = $dumpBrowserLogs;- $this->outName = $outName;+ $this->framesPerLambda = $framesPerLambda;+ $this->rendererFunctionName = $rendererFunctionName;$this->proResProfile = $proResProfile;$this->pixelFormat = $pixelFormat;}@@ -155,23 +154,24 @@ class RenderParams'dumpBrowserLogs' => $this->getDumpBrowserLogs(),];- if (!empty($this->getCrf())) {- $json['crf'] = $this->getCrf();+ if ($this->getCrf() !== null) {+ $parameters['crf'] = $this->getCrf();}- if (!empty($this->getEnvVariables())) {- $json['envVariables'] = $this->getEnvVariables();+ if ($this->getEnvVariables() !== null) {+ $parameters['envVariables'] = $this->getEnvVariables();}- if (!empty($this->getPixelFormat())) {- $json['pixelFormat'] = $this->getPixelFormat();+ if ($this->getPixelFormat() !== null) {+ $parameters['pixelFormat'] = $this->getPixelFormat();}- if (!empty($this->getProResProfile())) {- $json['proResProfile'] = $this->getProResProfile();+ if ($this->getProResProfile() !== null) {+ $parameters['proResProfile'] = $this->getProResProfile();}- if (!empty($this->getQuality())) {- $json['quality'] = $this->getQuality();++ if ($this->getQuality() !== null) {+ $parameters['quality'] = $this->getQuality();}return $parameters;