Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
imdavidbosch committed Aug 11, 2022
1 parent 5c5954d commit d2650b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 69 deletions.
102 changes: 55 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Note: Updated for API v2.0

# Create videos programmatically in Node JS
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.

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

```javascript
const {Movie, Scene} = require("json2video-sdk");

// Create a new movie
let movie = new Movie;

// Set your API key
// Get your free API key at https://json2video.com
movie.setAPIKey(YOUR_API_KEY);

// Set a project ID
movie.set("project", "myproj");

// Set movie quality: low, medium, high
movie.set("quality", "high");

// Create a new scene
let scene = new Scene;

// Set the scene background color
scene.set("background-color", "#4392F1");

// Add a text element printing "Hello world" in a fancy way (basic/006)
// The element is 10 seconds long and starts 2 seconds from the scene start
// Element's vertical position is 50 pixels from the top
scene.addElement({
type: "text",
template: "basic/006",
items: [
{ text: "Hello world" }
],
y: 50,
duration: 10,
start: 2
});

// Add the scene to the movie
movie.addScene(scene);

// Call the API and render the movie
movie.render();

// Wait for the movie to finish rendering
movie
.waitToFinish((status) => {
console.log("Rendering: ", status.movies[0].status, " / ", status.movies[0].task);
})
.then((status) => {
console.log("Movie is ready: ", status.movies[0].url);

async function main() {
// Create a new movie
let movie = new Movie;

// Set your API key
// Get your free API key at https://json2video.com
movie.setAPIKey(YOUR_API_KEY);

// Set movie quality: low, medium, high
movie.set("quality", "high");

// Generate a video draft
movie.set("draft", true);

// Create a new scene
let scene = new Scene;

// Set the scene background color
scene.set("background-color", "#4392F1");

// Add a text element printing "Hello world" in a fancy way (style 003)
// The element is 10 seconds long and starts 2 seconds from the scene start
scene.addElement({
type: "text",
style: "003",
text: "Hello world",
duration: 10,
start: 2
});

// Add the scene to the movie
movie.addScene(scene);

// Call the API and render the movie
let render = await movie.render();
console.log(render);

// Wait for the movie to finish rendering
await movie
.waitToFinish((status) => {
console.log("Rendering: ", status.movie.status, " / ", status.movie.message);
})
.then((status) => {
console.log("Movie is ready: ", status.movie.url);
console.log("Remaining final movies: ", status.remaining_quota.movies);
console.log("Remaining drafts: ", status.remaining_quota.drafts);
})
.catch((err) => {
console.log("Error: ", err);
});
}

main();
```

This is the resulting video:
Expand Down
48 changes: 27 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
JSON2Video SDK v1.0
JSON2Video SDK v2.0
Author: JSON2Video.com
Description: SDK for creating videos programmatically using JSON2Video API
Expand Down Expand Up @@ -66,21 +66,21 @@ class Scene extends Base {
if (duration !== null) this.object.transition.duration = duration;
if (type !== null) this.object.transition.type = type;
}
}
};
}

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

// setAPIKey(): Sets your API Key
setAPIKey = function (apikey) {
this.apikey = apikey;
}
};

// addScene(): Adds a new scene in the Movie
addScene = function (scene = null) {
Expand All @@ -90,7 +90,7 @@ class Movie extends Base {
return true;
}
else throw "Invalid scene";
}
};

// fetch(): Encapsulates API calls
fetch = async function (method = "GET", url = "", body = null, headers = {}) {
Expand Down Expand Up @@ -132,52 +132,58 @@ class Movie extends Base {
if (data) req.write(data);
req.end();
});
}
};

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

return this.fetch("POST", this.api_url, this.object, {
let response = await this.fetch("POST", this.api_url, this.object, {
"Content-Type": "application/json",
"x-api-key": this.apikey
});
}

if (response && response.success && response.project) this.object.project = response.project;

return response;
};

// getStatus(): Gets the current project rendering status
getStatus = async function() {
if (!this.apikey) throw "Invalid API Key";
if (!("project" in this.object)) throw "Project ID not set";
getStatus = async function(project=null) {
if (!project) project = this.object.project??null;
if (!this.apikey) throw("Invalid API Key");
if (!("project" in this.object)) throw("Project ID not set");

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

return this.fetch("GET", url, null, {
"x-api-key": this.apikey
});
}
};

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

if (response && response.success && ("movies" in response) && response.movies.length==1) {
if (response.movies[0].status=="done") {
if (response && response.success && ("movie" in response)) {
if (response.movie.status=="done") {
clearInterval(interval_id);
resolve(response);
}
if (typeof callback == "function") callback(response);
}
else {
console.log("Error");
clearInterval(interval_id);
resolve(response);
}

if (typeof callback == "function") callback(response);
}).bind(this), 1000);

}).bind(this), 5000);
});
}
};
}

// Export Scene and Movie objects
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json2video-sdk",
"version": "1.0.0",
"version": "2.0.0",
"description": "SDK for creating videos programmatically using JSON2Video API",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit d2650b9

Please sign in to comment.