Editing video using Node.js

LOVED BY DEVELOPERS

Used every day by developers at businesses large and small

The Shotstack Node.js video SDK makes it quick and easy to set up a Node based video editor that can be integrated in to your applications in days, not months.

The video editing package is open-source, available as an NPM module or directly from GitHub. The SDK works directly with the Shotstack video editing API which is a fully managed, cloud hosted service that removes the need to compile and install software, manage Lambdas, containers or servers.

Shotstack ships with all the essential video editing features you need, including cut and trim, stitching clips, transitions, filters and effects and lets you merge videos, images, text and audio. All accessible using familiar JavaScript, JSON and Node.js.

Installation

The Shotstack Node.js video editing library is available as an NPM module and can be installed directly from NPM or downloaded from GitHub.

npm install shotstack-sdk

Usage

1. Require the library

Require the library in to your script.

const Shotstack = require('shotstack-sdk');
2. Configure the API client

Set the base path of the API client to point to either the stage sandbox or v1 production environment.

const defaultClient = Shotstack.ApiClient.instance;
defaultClient.basePath = 'https://api.shotstack.io/stage';
3. Authenticate

Set the API key using your own key which you will receive when you register. You should load the key using environment variables in a production environment.

const DeveloperKey = defaultClient.authentications['DeveloperKey'];
DeveloperKey.apiKey = 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD';
4. Trim a video clip

The API supports a full range of video editing features but to keep things simple, this example shows how to trim a video clips start and end point. Set the video asset trim to 3 to cut the first 3 seconds off the start and then play the next 8 seconds by setting the length to 8. Setting the start to 0 means the video clip start straight away.

let videoAsset = new Shotstack.VideoAsset;
videoAsset
.setSrc('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4')
.setTrim(3);

let videoClip = new Shotstack.Clip;
videoClip
.setAsset(videoAsset)
.setStart(0)
.setLength(8);
5. Add the clip to a track

A track is like a layer that can contain clips layered on top of each other, for examples; text over a video. This video just has one clip which is added to a single track.

let track = new Shotstack.Track;
track.setClips([videoClip]);
6. Add the track to the timeline

The timeline specifies all the tracks, as an array, that make up the final video edit.

let timeline = new Shotstack.Timeline;
timeline.setTracks([track]);
7. Set the video output specification

Every video needs an output specification, here the format is set to mp4 and the resolution to SD.

let output = new Shotstack.Output;
output
.setFormat('mp4')
.setResolution('sd');
8. Create the video edit

The final edit is made up of the timeline and the output format.

let edit = new Shotstack.Edit;
edit
.setTimeline(timeline)
.setOutput(output);
9. POST the edit to the API

The SDK takes care of converting the edit to JSON and posts it to the API for rendering returning a Promise.

const api = new Shotstack.EditApi();
api.postRender(edit).then((render) => {
console.log(render.response);
});
10. Get the rendered video URL

Finally, check the status of the render and when done output the video URL. Modify the request from step 9 and add the status check inside the render response.

const api = new Shotstack.EditApi();
api.postRender(edit).then((render) => {
api.getRender(render.response.id).then((data) => {
if (data.response.status === 'done') {
console.log(data.response.url);
}
});
});

Output

The Node.js code above prepares a JSON payload, describing the video editing parameters to trim a video clip, POST's it to the Shotstack API and generate this video.

EXPLORE

Check our Node.js video editing examples and demos on GitHub

DISCOVER

See what the Shotstack API and Node.js can do