├── package.json ├── LICENSE ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json2video-sdk", 3 | "version": "2.0.4", 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 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 JSON2Video 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Note: Updated for API v2.0 2 | 3 | # Create videos programmatically in Node JS 4 | 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. 5 | 6 | [JSON2Video is a video editing API](https://json2video.com) that simplifies creating, editing and customising videos programmatically. Its dead simple approach, close to the web development mindset, makes it the ultimate solution for developers that want to create or customise videos in an automated way. 7 | 8 | Additionally, the simple integration of real HTML5+CSS elements, the already built-in text animations and voice generation (TTS) converts JSON2Video in the best solution in its category. 9 | 10 | Use cases 11 | * Automate the production of promotional videos for your e-commerce products 12 | * Automate publication of social media videos created directly from your news feed 13 | * Customize your advertising campaigns with different images, videos, texts and create tens or hundreds of different options 14 | * From weather forecasts to traffic bulletins or financial reports, if you have a data source you can create an audiovisual experience 15 | * Convert your text, pictures and information into engaging videos of your real estate properties 16 | * Add watermarks, bumpers, titles; Concatenate different videos into one; Add voice-over or music; Create photo slideshows; … 17 | 18 | 19 | ## Get your FREE API Key 20 | JSON2Video is free to use. Get your API Key at [JSON2Video.com](https://json2video.com) 21 | 22 | ## Documentation 23 | The [API Specification](https://json2video.com/docs/api/) will provide you with all the details of the JSON payload and the endpoints. 24 | 25 | For a step by step guide, read the [Tutorial](https://json2video.com/docs/tutorial/) that will introduce you through all features with code examples. 26 | 27 | ## NodeJS SDK installation 28 | 29 | The SDK has no external dependencies on other packages. 30 | 31 | 1) Open the terminal and cd to your project directory 32 | 2) Use npm: 33 | 34 | ``` 35 | npm install json2video-sdk 36 | ``` 37 | 38 | ## Hello world 39 | JSON2Video makes video creation easy as a piece of cake: 40 | 41 | ```javascript 42 | const {Movie, Scene} = require("json2video-sdk"); 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 72 | }); 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("Response: ", status); 88 | console.log("Movie is ready: ", status.movie.url); 89 | }) 90 | .catch((err) => { 91 | console.log("Error: ", err); 92 | }); 93 | } 94 | 95 | main(); 96 | ``` 97 | 98 | This is the resulting video: 99 | 100 | https://assets.json2video.com/sites/github/hello-world.mp4 101 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | JSON2Video SDK v2.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', 'draft', 'width', 'height', 'resolution', 'exports', 'quality', 'fps', 'cache', 'template', 'variables', 'id']; 76 | this.api_url = 'https://api.json2video.com/v2/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'] = Buffer.byteLength(data); 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 | let response = await this.fetch("POST", this.api_url, this.object, { 142 | "Content-Type": "application/json", 143 | "x-api-key": this.apikey 144 | }); 145 | 146 | if (response && response.success && response.project) this.object.project = response.project; 147 | 148 | return response; 149 | }; 150 | 151 | // getStatus(): Gets the current project rendering status 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"); 156 | 157 | let url = this.api_url + "?project=" + this.object.project; 158 | 159 | return this.fetch("GET", url, null, { 160 | "x-api-key": this.apikey 161 | }); 162 | }; 163 | 164 | // waitToFinish(): Waits the current project to finish rendering by checking status every 1 second 165 | waitToFinish = async function(callback=null) { 166 | return new Promise((resolve, reject) => { 167 | const interval_id = setInterval((async function() { 168 | let response = await this.getStatus().catch((err) => { 169 | reject(err); 170 | }); 171 | 172 | if (response && response.success && ("movie" in response)) { 173 | if (response.movie.status=="done") { 174 | clearInterval(interval_id); 175 | resolve(response); 176 | } 177 | if (typeof callback == "function") callback(response); 178 | } 179 | else { 180 | clearInterval(interval_id); 181 | resolve(response); 182 | } 183 | 184 | }).bind(this), 5000); 185 | }); 186 | }; 187 | } 188 | 189 | // Export Scene and Movie objects 190 | exports.Scene = Scene; 191 | exports.Movie = Movie; 192 | --------------------------------------------------------------------------------