Skip to content

Commit f012b93

Browse files
committed
Initial code
1 parent d32c74c commit f012b93

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

index.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
3+
JSON2Video SDK v1.0
4+
5+
Author: JSON2Video.com
6+
Description: SDK for creating videos programmatically using JSON2Video API
7+
8+
GET YOUR FREE APIKey at https://json2video.com/get-api-key/
9+
10+
CHECK DOCUMENTATION at: https://json2video.com/docs/
11+
12+
*/
13+
14+
class Base {
15+
16+
constructor() {
17+
this.object = {};
18+
this.properties = [];
19+
}
20+
21+
// set(): Sets a property for the Scene or Movie
22+
set(property, value) {
23+
property = property.toLowerCase();
24+
property = property.replace(/_/g, '-');
25+
26+
if (this.properties.indexOf(property) > -1) {
27+
this.object[property] = value;
28+
}
29+
else throw `Property ${property} does not exist`;
30+
}
31+
32+
// addElement(): Adds a new element in the Scene or Movie
33+
addElement(element = null) {
34+
if (element && typeof element == "object") {
35+
if (!("elements" in this.object)) {
36+
this.object.elements = [];
37+
}
38+
this.object.elements.push(element);
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
// getJSON(): Returns the data object as a JSON string
45+
getJSON() {
46+
return JSON.stringify(this.object, null, 2);
47+
}
48+
49+
// getObject(): Returns the data object
50+
getObject() {
51+
return this.object;
52+
}
53+
}
54+
55+
class Scene extends Base {
56+
constructor(...a) {
57+
super(...a);
58+
this.properties = ['comment', 'background-color', 'duration', 'cache'];
59+
}
60+
61+
// setTransition(): Sets the transition style for this scene
62+
setTransition(style = null, duration = null, type = null) {
63+
if (style || duration || type) {
64+
if (!("transition" in this.object)) this.object.transition = {};
65+
if (style !== null) this.object.transition.style = style;
66+
if (duration !== null) this.object.transition.duration = duration;
67+
if (type !== null) this.object.transition.type = type;
68+
}
69+
}
70+
}
71+
72+
class Movie extends Base {
73+
constructor(...a) {
74+
super(...a);
75+
this.properties = ['comment', 'project', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
76+
this.api_url = 'https://api.json2video.com/v1/movies';
77+
this.apikey = null;
78+
}
79+
80+
// setAPIKey(): Sets your API Key
81+
setAPIKey = function (apikey) {
82+
this.apikey = apikey;
83+
}
84+
85+
// addScene(): Adds a new scene in the Movie
86+
addScene = function (scene = null) {
87+
if (scene) {
88+
if (!("scenes" in this.object)) this.object.scenes = [];
89+
this.object.scenes.push(scene.getObject());
90+
return true;
91+
}
92+
else throw "Invalid scene";
93+
}
94+
95+
// fetch(): Encapsulates API calls
96+
fetch = async function (method = "GET", url = "", body = null, headers = {}) {
97+
const https = require('https');
98+
const endpoint = new URL(url);
99+
let data = null;
100+
101+
if (body) {
102+
data = JSON.stringify(body);
103+
headers['Content-Length'] = data.length;
104+
}
105+
106+
const options = {
107+
hostname: endpoint.hostname,
108+
port: 443,
109+
path: endpoint.pathname + endpoint.search,
110+
method: method,
111+
headers: headers
112+
};
113+
114+
return new Promise((resolve, reject) => {
115+
const req = https.request(options, res => {
116+
res.on('data', response => {
117+
response.status = res.statusCode;
118+
resolve(JSON.parse(response.toString()));
119+
});
120+
});
121+
122+
req.on('error', error => {
123+
console.error(error);
124+
reject({
125+
status: 500,
126+
success:false,
127+
message: "Unknown SDK error",
128+
error: error
129+
});
130+
});
131+
132+
if (data) req.write(data);
133+
req.end();
134+
});
135+
}
136+
137+
// render(): Starts a new rendering job
138+
render = async function() {
139+
if (!this.apikey) throw "Invalid API Key";
140+
141+
return this.fetch("POST", this.api_url, this.object, {
142+
"Content-Type": "application/json",
143+
"x-api-key": this.apikey
144+
});
145+
}
146+
147+
// 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";
151+
152+
let url = this.api_url + "?project=" + this.object.project;
153+
154+
return this.fetch("GET", url, null, {
155+
"x-api-key": this.apikey
156+
});
157+
}
158+
159+
// waitToFinish(): Waits the current project to finish rendering by checking status every 1 second
160+
waitToFinish = async function(callback=null) {
161+
return new Promise((resolve, reject) => {
162+
const interval_id = setInterval((async function() {
163+
let response = await this.getStatus();
164+
165+
if (response && response.success && ("movies" in response) && response.movies.length==1) {
166+
if (response.movies[0].status=="done") {
167+
clearInterval(interval_id);
168+
resolve(response);
169+
}
170+
}
171+
else {
172+
console.log("Error");
173+
clearInterval(interval_id);
174+
resolve(response);
175+
}
176+
177+
if (typeof callback == "function") callback(response);
178+
}).bind(this), 1000);
179+
});
180+
}
181+
}
182+
183+
// Export Scene and Movie objects
184+
exports.Scene = Scene;
185+
exports.Movie = Movie;

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "json2video-sdk",
3+
"version": "1.0.0",
4+
"description": "SDK for creating videos programmatically using JSON2Video API",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/JSON2Video/json2video-nodejs-sdk.git"
12+
},
13+
"keywords": [
14+
"video",
15+
"slideshow",
16+
"text-to-speech",
17+
"video-editing",
18+
"video-processing",
19+
"watermarking"
20+
],
21+
"author": "JSON2Video.com",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/JSON2Video/json2video-nodejs-sdk/issues"
25+
},
26+
"homepage": "https://github.com/JSON2Video/json2video-nodejs-sdk#readme"
27+
}

0 commit comments

Comments
 (0)