Skip to content

Commit 5f99b6f

Browse files
committed
v2.0
1 parent f34efc2 commit 5f99b6f

File tree

4 files changed

+75
-102
lines changed

4 files changed

+75
-102
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Note: Updated for API v2.0
2+
13
# Create videos programmatically in PHP
24
Create and edit videos: add watermarks, resize videos, create slideshows, add soundtrack, automate the creation of videos in multiple languages, add voice-over, add text animations.
35

@@ -66,13 +68,11 @@ JSON2Video makes video creation easy as a piece of cake:
6668

6769
// Set your API key
6870
// Get your free API key at https://json2video.com
69-
$movie->setAPIKey(YOUR_API_KEY);
70-
71-
// Set a project ID
72-
$movie->project = 'myproj';
71+
$movie->setAPIKey('7SsNlCEwaB6uZoz0jpZnI3D0b0EZyW1KASxQjYm1');
7372

7473
// Set movie quality: low, medium, high
7574
$movie->quality = 'high';
75+
$movie->draft = true;
7676

7777
// Create a new scene
7878
$scene = new Scene;
@@ -85,11 +85,8 @@ JSON2Video makes video creation easy as a piece of cake:
8585
// Element's vertical position is 50 pixels from the top
8686
$scene->addElement([
8787
'type' => 'text',
88-
'template' => 'basic/006',
89-
'items' => [
90-
[ 'text' => 'Hello world' ]
91-
],
92-
'y' => 50,
88+
'style' => '003',
89+
'text' => 'Hello world',
9390
'duration' => 10,
9491
'start' => 2
9592
]);
@@ -98,10 +95,15 @@ JSON2Video makes video creation easy as a piece of cake:
9895
$movie->addScene($scene);
9996

10097
// Call the API and start rendering the movie
101-
$movie->render();
98+
$result = $movie->render();
99+
var_dump($result);
100+
101+
//$result = $movie->getStatus('cLiLZ7fKeMvjb4b8');
102+
//var_dump($result);
102103

103104
// Wait for the render to finish
104105
$movie->waitToFinish();
106+
?>
105107
```
106108

107109
This is the resulting video:

bundled/all.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class Scene extends Base {
6262
}
6363

6464
class Movie extends Base {
65-
private $api_url = 'https://api.json2video.com/v1/movies';
66-
protected $properties = ['comment', 'project', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
65+
private $api_url = 'https://api.json2video.com/v2/movies';
66+
protected $properties = ['comment', 'draft', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
6767

6868
private $apikey = null;
6969

@@ -123,7 +123,12 @@ public function render() {
123123
]);
124124

125125
if ($response) {
126-
if ($response['status']=='200') return json_decode($response['response'], true);
126+
if ($response['status']=='200') {
127+
$render = json_decode($response['response'], true);
128+
if ($render['success']??false && !empty($render['project'])) $this->object['project'] = $render['project'];
129+
else throw new \Exception("Render didn't return a project ID");
130+
return $render;
131+
}
127132
elseif ($response['status']=='400') {
128133
$api_response = json_decode($response['response'], true);
129134
throw new \Exception('JSON Syntax error: ' . ($api_response['message'] ?? 'Unknown error'));
@@ -138,12 +143,14 @@ public function render() {
138143
return false;
139144
}
140145

141-
public function getStatus() {
146+
public function getStatus($project=null) {
147+
148+
if (!$project) $project = $this->object['project'] ?? null;
142149

143150
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
144-
if (empty($this->object['project'])) throw new \Exception('Project ID not set');
151+
if (!$project) throw new \Exception('Project ID not set');
145152

146-
$url = $this->api_url . '?project=' . $this->object['project'];
153+
$url = $this->api_url . '?project=' . $project;
147154

148155
$status = $this->fetch('GET', $url, '', [
149156
"x-api-key: {$this->apikey}"
@@ -162,36 +169,35 @@ public function getStatus() {
162169

163170
public function waitToFinish($delay=5, $callback=null) {
164171

165-
$max_loops = 100;
172+
$max_loops = 60;
166173
$loops = 0;
167174

168175
while ($loops<$max_loops) {
169176
$response = $this->getStatus();
170177

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]);
178+
if ($response && ($response['success']??false) && !empty($response['movie'])) {
175179

176-
return $response['movies'][0];
180+
if (is_callable($callback)) $callback($response['movie'], $response['remaining_quota']);
181+
else $this->printStatus($response['movie'], $response['remaining_quota']);
182+
183+
if (!empty($response['movie']['status']) && $response['movie']['status']=='done') {
184+
return $response;
177185
}
178186
}
179187
else {
180188
throw new \Error('Invalid API response');
181189
}
182190

183-
if (is_callable($callback)) $callback($response['movies'][0]);
184-
else $this->printStatus($response['movies'][0]);
185-
186191
sleep($delay);
187192
$loops++;
188193
}
189194
}
190195

191-
public function printStatus($response) {
192-
echo 'Status: ', $response['status'], ' / ', $response['task'], PHP_EOL;
196+
public function printStatus($response, $quota) {
197+
echo 'Status: ', $response['status'], ' / ', $response['message'], PHP_EOL;
193198
if ($response['status']=='done') {
194-
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL, PHP_EOL;
199+
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL;
200+
echo 'Remaining quota: movies(', $quota['movies'], ') and drafts(', $quota['drafts'], ')', PHP_EOL, PHP_EOL;
195201
}
196202
}
197203
}

examples/hello-world.php

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,44 @@
88
99
*/
1010

11-
// Import the Movie and Scene classes
11+
require 'vendor/autoload.php';
12+
1213
use JSON2Video\Movie;
1314
use JSON2Video\Scene;
1415

15-
// Import the SDK
16-
include "../bundled/all.php";
17-
18-
// Get your free API key at https://json2video.com
19-
define ('YOUR_API_KEY', '');
20-
2116
// Create a new movie
2217
$movie = new Movie;
2318

2419
// Set your API key
20+
// Get your free API key at https://json2video.com
2521
$movie->setAPIKey(YOUR_API_KEY);
2622

27-
// Set a project ID
28-
$movie->project = "myproj";
29-
30-
// Set movie quality
31-
$movie->quality = "high";
23+
// Set movie quality: low, medium, high
24+
$movie->quality = 'high';
25+
$movie->draft = true;
3226

3327
// Create a new scene
3428
$scene = new Scene;
3529

3630
// Set the scene background color
37-
$scene->background_color = "#4392F1";
31+
$scene->background_color = '#4392F1';
3832

3933
// Add a text element printing "Hello world" in a fancy way (basic/006)
4034
// The element is 10 seconds long and starts 2 seconds from the scene start
4135
// Element's vertical position is 50 pixels from the top
4236
$scene->addElement([
4337
'type' => 'text',
44-
'template' => 'basic/006',
45-
'items' => [
46-
[ 'text' => 'Hello world' ]
47-
],
48-
'y' => 50,
38+
'style' => '003',
39+
'text' => 'Hello world',
4940
'duration' => 10,
5041
'start' => 2
5142
]);
5243

44+
// Add the scene to the movie
45+
$movie->addScene($scene);
5346

54-
try {
55-
// Add the scene to the movie
56-
$movie->addScene($scene);
57-
58-
// Call the API and render the movie
59-
$movie->render();
60-
61-
// Check every 2 seconds for the render status
62-
for ($i=0; $i<60; $i++) {
63-
64-
// Get the movie status
65-
$response = $movie->getStatus();
66-
67-
if ($response['success'] && $response['movies'][0]) {
68-
// Print the status and the last task
69-
echo '>>> Status: ', $response['movies'][0]['status'], ' --> ', $response['movies'][0]['task'], PHP_EOL;
70-
71-
// If the render is done, print the video URL and break the loop
72-
if ($response['movies'][0]['status']=='done') {
73-
echo PHP_EOL, '>>> The movie is ready at: ', $response['movies'][0]['url'], PHP_EOL, PHP_EOL;
74-
break;
75-
}
76-
}
77-
else {
78-
// If there was an invalid API response
79-
echo '--- ERROR ---', PHP_EOL, $response['message'] ?? 'Unknown error', PHP_EOL, PHP_EOL;
80-
}
81-
82-
// Sleep for 2 seconds in every loop
83-
sleep(2);
84-
}
85-
}
86-
catch(Exception $error) {
87-
// Print the error message
88-
echo $error->getMessage(), PHP_EOL;
89-
}
90-
91-
47+
// Call the API and start rendering the movie
48+
$result = $movie->render();
9249

50+
// Wait for the render to finish
51+
$movie->waitToFinish();

src/Movie.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace JSON2Video;
44

55
class Movie extends Base {
6-
private $api_url = 'https://api.json2video.com/v1/movies';
7-
protected $properties = ['comment', 'project', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
6+
private $api_url = 'https://api.json2video.com/v2/movies';
7+
protected $properties = ['comment', 'draft', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
88

99
private $apikey = null;
1010

@@ -64,7 +64,12 @@ public function render() {
6464
]);
6565

6666
if ($response) {
67-
if ($response['status']=='200') return json_decode($response['response'], true);
67+
if ($response['status']=='200') {
68+
$render = json_decode($response['response'], true);
69+
if ($render['success']??false && !empty($render['project'])) $this->object['project'] = $render['project'];
70+
else throw new \Exception("Render didn't return a project ID");
71+
return $render;
72+
}
6873
elseif ($response['status']=='400') {
6974
$api_response = json_decode($response['response'], true);
7075
throw new \Exception('JSON Syntax error: ' . ($api_response['message'] ?? 'Unknown error'));
@@ -79,12 +84,14 @@ public function render() {
7984
return false;
8085
}
8186

82-
public function getStatus() {
87+
public function getStatus($project=null) {
88+
89+
if (!$project) $project = $this->object['project'] ?? null;
8390

8491
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
85-
if (empty($this->object['project'])) throw new \Exception('Project ID not set');
92+
if (!$project) throw new \Exception('Project ID not set');
8693

87-
$url = $this->api_url . '?project=' . $this->object['project'];
94+
$url = $this->api_url . '?project=' . $project;
8895

8996
$status = $this->fetch('GET', $url, '', [
9097
"x-api-key: {$this->apikey}"
@@ -103,36 +110,35 @@ public function getStatus() {
103110

104111
public function waitToFinish($delay=5, $callback=null) {
105112

106-
$max_loops = 100;
113+
$max_loops = 60;
107114
$loops = 0;
108115

109116
while ($loops<$max_loops) {
110117
$response = $this->getStatus();
111118

112-
if ($response && ($response['success']??false) && isset($response['movies']) && count($response['movies'])==1) {
113-
if (isset($response['movies'][0]['status']) && $response['movies'][0]['status']=='done') {
114-
if (is_callable($callback)) $callback($response['movies'][0]);
115-
else $this->printStatus($response['movies'][0]);
119+
if ($response && ($response['success']??false) && !empty($response['movie'])) {
116120

117-
return $response['movies'][0];
121+
if (is_callable($callback)) $callback($response['movie'], $response['remaining_quota']);
122+
else $this->printStatus($response['movie'], $response['remaining_quota']);
123+
124+
if (!empty($response['movie']['status']) && $response['movie']['status']=='done') {
125+
return $response;
118126
}
119127
}
120128
else {
121129
throw new \Error('Invalid API response');
122130
}
123131

124-
if (is_callable($callback)) $callback($response['movies'][0]);
125-
else $this->printStatus($response['movies'][0]);
126-
127132
sleep($delay);
128133
$loops++;
129134
}
130135
}
131136

132-
public function printStatus($response) {
133-
echo 'Status: ', $response['status'], ' / ', $response['task'], PHP_EOL;
137+
public function printStatus($response, $quota) {
138+
echo 'Status: ', $response['status'], ' / ', $response['message'], PHP_EOL;
134139
if ($response['status']=='done') {
135-
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL, PHP_EOL;
140+
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL;
141+
echo 'Remaining quota: movies(', $quota['movies'], ') and drafts(', $quota['drafts'], ')', PHP_EOL, PHP_EOL;
136142
}
137143
}
138144
}

0 commit comments

Comments
 (0)