Skip to content

Commit f34efc2

Browse files
committed
Update (see description)
- Updated all.php - Updated README.md
1 parent 08c94f1 commit f34efc2

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ You can use JSON2Video PHP SDK as a Composer package or with a simple require_on
2828
### Using require_once
2929
The simplest way :-)
3030

31-
1) Download all.php from the /bundled folder into your project directory
31+
1) Download [all.php](https://github.com/JSON2Video/json2video-php-sdk/blob/main/bundled/all.php) from the /bundled folder into your project directory
3232
2) Import the library:
3333

3434
```php
3535
<?php
3636
require_once 'path/to/the/sdk/all.php';
3737

38+
use JSON2Video\Movie;
39+
use JSON2Video\Scene;
40+
3841
```
3942

4043
### Using Composer
@@ -44,14 +47,20 @@ The SDK has no external dependencies on other packages.
4447
2) Use composer:
4548

4649
```
47-
$ composer require json2video/json2video-php-sdk
50+
composer require json2video/json2video-php-sdk
4851
```
4952

5053
## Hello world
5154
JSON2Video makes video creation easy as a piece of cake:
5255

5356
```php
5457
<?php
58+
59+
require 'vendor/autoload.php';
60+
61+
use JSON2Video\Movie;
62+
use JSON2Video\Scene;
63+
5564
// Create a new movie
5665
$movie = new Movie;
5766

@@ -88,8 +97,11 @@ JSON2Video makes video creation easy as a piece of cake:
8897
// Add the scene to the movie
8998
$movie->addScene($scene);
9099

91-
// Call the API and render the movie
100+
// Call the API and start rendering the movie
92101
$movie->render();
102+
103+
// Wait for the render to finish
104+
$movie->waitToFinish();
93105
```
94106

95107
This is the resulting video:

bundled/all.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,8 @@ public function getObject() {
5757

5858

5959
class Scene extends Base {
60-
protected $properties = ['comment', 'background_color', 'duration', 'cache'];
61-
60+
protected $properties = ['comment', 'background_color', 'transition', 'duration', 'cache'];
6261
protected $object = [];
63-
64-
public function setTransition($style=null, $duration=null, $type=null) {
65-
if ($style || $duration || $type) {
66-
if (!isset($this->object['transition'])) $this->object['transition'] = [];
67-
if (!is_null($style)) $this->object['transition']['style'] = $style;
68-
if (!is_null($duration)) $this->object['transition']['duration'] = $duration;
69-
if (!is_null($type)) $this->object['transition']['type'] = $type;
70-
}
71-
}
72-
7362
}
7463

7564
class Movie extends Base {
@@ -170,4 +159,39 @@ public function getStatus() {
170159

171160
return false;
172161
}
162+
163+
public function waitToFinish($delay=5, $callback=null) {
164+
165+
$max_loops = 100;
166+
$loops = 0;
167+
168+
while ($loops<$max_loops) {
169+
$response = $this->getStatus();
170+
171+
if ($response && ($response['success']??false) && isset($response['movies']) && count($response['movies'])==1) {
172+
if (isset($response['movies'][0]['status']) && $response['movies'][0]['status']=='done') {
173+
if (is_callable($callback)) $callback($response['movies'][0]);
174+
else $this->printStatus($response['movies'][0]);
175+
176+
return $response['movies'][0];
177+
}
178+
}
179+
else {
180+
throw new \Error('Invalid API response');
181+
}
182+
183+
if (is_callable($callback)) $callback($response['movies'][0]);
184+
else $this->printStatus($response['movies'][0]);
185+
186+
sleep($delay);
187+
$loops++;
188+
}
189+
}
190+
191+
public function printStatus($response) {
192+
echo 'Status: ', $response['status'], ' / ', $response['task'], PHP_EOL;
193+
if ($response['status']=='done') {
194+
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL, PHP_EOL;
195+
}
196+
}
173197
}

0 commit comments

Comments
 (0)