Skip to content

Commit d2650b9

Browse files
committed
v2.0
1 parent 5c5954d commit d2650b9

File tree

3 files changed

+83
-69
lines changed

3 files changed

+83
-69
lines changed

README.md

Lines changed: 55 additions & 47 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 Node JS
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

@@ -38,54 +40,60 @@ JSON2Video makes video creation easy as a piece of cake:
3840

3941
```javascript
4042
const {Movie, Scene} = require("json2video-sdk");
41-
42-
// Create a new movie
43-
let movie = new Movie;
44-
45-
// Set your API key
46-
// Get your free API key at https://json2video.com
47-
movie.setAPIKey(YOUR_API_KEY);
48-
49-
// Set a project ID
50-
movie.set("project", "myproj");
51-
52-
// Set movie quality: low, medium, high
53-
movie.set("quality", "high");
54-
55-
// Create a new scene
56-
let scene = new Scene;
57-
58-
// Set the scene background color
59-
scene.set("background-color", "#4392F1");
60-
61-
// Add a text element printing "Hello world" in a fancy way (basic/006)
62-
// The element is 10 seconds long and starts 2 seconds from the scene start
63-
// Element's vertical position is 50 pixels from the top
64-
scene.addElement({
65-
type: "text",
66-
template: "basic/006",
67-
items: [
68-
{ text: "Hello world" }
69-
],
70-
y: 50,
71-
duration: 10,
72-
start: 2
73-
});
74-
75-
// Add the scene to the movie
76-
movie.addScene(scene);
77-
78-
// Call the API and render the movie
79-
movie.render();
80-
81-
// Wait for the movie to finish rendering
82-
movie
83-
.waitToFinish((status) => {
84-
console.log("Rendering: ", status.movies[0].status, " / ", status.movies[0].task);
85-
})
86-
.then((status) => {
87-
console.log("Movie is ready: ", status.movies[0].url);
43+
44+
async function main() {
45+
// Create a new movie
46+
let movie = new Movie;
47+
48+
// Set your API key
49+
// Get your free API key at https://json2video.com
50+
movie.setAPIKey(YOUR_API_KEY);
51+
52+
// Set movie quality: low, medium, high
53+
movie.set("quality", "high");
54+
55+
// Generate a video draft
56+
movie.set("draft", true);
57+
58+
// Create a new scene
59+
let scene = new Scene;
60+
61+
// Set the scene background color
62+
scene.set("background-color", "#4392F1");
63+
64+
// Add a text element printing "Hello world" in a fancy way (style 003)
65+
// The element is 10 seconds long and starts 2 seconds from the scene start
66+
scene.addElement({
67+
type: "text",
68+
style: "003",
69+
text: "Hello world",
70+
duration: 10,
71+
start: 2
8872
});
73+
74+
// Add the scene to the movie
75+
movie.addScene(scene);
76+
77+
// Call the API and render the movie
78+
let render = await movie.render();
79+
console.log(render);
80+
81+
// Wait for the movie to finish rendering
82+
await movie
83+
.waitToFinish((status) => {
84+
console.log("Rendering: ", status.movie.status, " / ", status.movie.message);
85+
})
86+
.then((status) => {
87+
console.log("Movie is ready: ", status.movie.url);
88+
console.log("Remaining final movies: ", status.remaining_quota.movies);
89+
console.log("Remaining drafts: ", status.remaining_quota.drafts);
90+
})
91+
.catch((err) => {
92+
console.log("Error: ", err);
93+
});
94+
}
95+
96+
main();
8997
```
9098

9199
This is the resulting video:

index.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
3-
JSON2Video SDK v1.0
3+
JSON2Video SDK v2.0
44
55
Author: JSON2Video.com
66
Description: SDK for creating videos programmatically using JSON2Video API
@@ -66,21 +66,21 @@ class Scene extends Base {
6666
if (duration !== null) this.object.transition.duration = duration;
6767
if (type !== null) this.object.transition.type = type;
6868
}
69-
}
69+
};
7070
}
7171

7272
class Movie extends Base {
7373
constructor(...a) {
7474
super(...a);
75-
this.properties = ['comment', 'project', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
76-
this.api_url = 'https://api.json2video.com/v1/movies';
75+
this.properties = ['comment', 'draft', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
76+
this.api_url = 'https://api.json2video.com/v2/movies';
7777
this.apikey = null;
7878
}
7979

8080
// setAPIKey(): Sets your API Key
8181
setAPIKey = function (apikey) {
8282
this.apikey = apikey;
83-
}
83+
};
8484

8585
// addScene(): Adds a new scene in the Movie
8686
addScene = function (scene = null) {
@@ -90,7 +90,7 @@ class Movie extends Base {
9090
return true;
9191
}
9292
else throw "Invalid scene";
93-
}
93+
};
9494

9595
// fetch(): Encapsulates API calls
9696
fetch = async function (method = "GET", url = "", body = null, headers = {}) {
@@ -132,52 +132,58 @@ class Movie extends Base {
132132
if (data) req.write(data);
133133
req.end();
134134
});
135-
}
135+
};
136136

137137
// render(): Starts a new rendering job
138138
render = async function() {
139139
if (!this.apikey) throw "Invalid API Key";
140140

141-
return this.fetch("POST", this.api_url, this.object, {
141+
let response = await this.fetch("POST", this.api_url, this.object, {
142142
"Content-Type": "application/json",
143143
"x-api-key": this.apikey
144144
});
145-
}
145+
146+
if (response && response.success && response.project) this.object.project = response.project;
147+
148+
return response;
149+
};
146150

147151
// getStatus(): Gets the current project rendering status
148-
getStatus = async function() {
149-
if (!this.apikey) throw "Invalid API Key";
150-
if (!("project" in this.object)) throw "Project ID not set";
152+
getStatus = async function(project=null) {
153+
if (!project) project = this.object.project??null;
154+
if (!this.apikey) throw("Invalid API Key");
155+
if (!("project" in this.object)) throw("Project ID not set");
151156

152157
let url = this.api_url + "?project=" + this.object.project;
153158

154159
return this.fetch("GET", url, null, {
155160
"x-api-key": this.apikey
156161
});
157-
}
162+
};
158163

159164
// waitToFinish(): Waits the current project to finish rendering by checking status every 1 second
160165
waitToFinish = async function(callback=null) {
161166
return new Promise((resolve, reject) => {
162167
const interval_id = setInterval((async function() {
163-
let response = await this.getStatus();
168+
let response = await this.getStatus().catch((err) => {
169+
reject(err);
170+
});
164171

165-
if (response && response.success && ("movies" in response) && response.movies.length==1) {
166-
if (response.movies[0].status=="done") {
172+
if (response && response.success && ("movie" in response)) {
173+
if (response.movie.status=="done") {
167174
clearInterval(interval_id);
168175
resolve(response);
169176
}
177+
if (typeof callback == "function") callback(response);
170178
}
171179
else {
172-
console.log("Error");
173180
clearInterval(interval_id);
174181
resolve(response);
175182
}
176-
177-
if (typeof callback == "function") callback(response);
178-
}).bind(this), 1000);
183+
184+
}).bind(this), 5000);
179185
});
180-
}
186+
};
181187
}
182188

183189
// Export Scene and Movie objects

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json2video-sdk",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "SDK for creating videos programmatically using JSON2Video API",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)