Skip to content

Commit

Permalink
Add template examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Shillitto committed Dec 5, 2022
1 parent e454eae commit 7547f9f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
- **gif.js** -
Create an animated gif that plays once.

### Template examples

- **templates/create.js**
Save an edit as a re-usable template with placeholders.

- **templates/render.js**
Render a template using merge fields to replace placeholders.

### Polling example

- **status.js** -
Expand Down
64 changes: 64 additions & 0 deletions examples/templates/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Shotstack = require('shotstack-sdk');

const defaultClient = Shotstack.ApiClient.instance;
const DeveloperKey = defaultClient.authentications['DeveloperKey'];
const api = new Shotstack.EditApi();

let apiUrl = 'https://api.shotstack.io/stage';

if (!process.env.SHOTSTACK_KEY) {
console.log('API Key is required. Set using: export SHOTSTACK_KEY=your_key_here');
process.exit(1);
}

if (process.env.SHOTSTACK_HOST) {
apiUrl = process.env.SHOTSTACK_HOST;
}

defaultClient.basePath = apiUrl;
DeveloperKey.apiKey = process.env.SHOTSTACK_KEY;

const videoAsset = new Shotstack.VideoAsset;
videoAsset
.setSrc('{{ URL }}')
.setTrim('{{ TRIM }}');

const videoClip = new Shotstack.Clip;
videoClip
.setAsset(videoAsset)
.setStart(0)
.setLength('{{ LENGTH }}');

const track = new Shotstack.Track;
track.setClips([videoClip]);

const timeline = new Shotstack.Timeline;
timeline.setTracks([track]);

const output = new Shotstack.Output;
output
.setFormat('mp4')
.setResolution('sd');

const edit = new Shotstack.Edit;
edit
.setTimeline(timeline)
.setOutput(output);

const template = new Shotstack.Template;
template
.setName('Trim Template')
.setTemplate(edit);

api.postTemplate(template).then((data) => {
let message = data.response.message;
let id = data.response.id

console.log(message + '\n');
console.log('>> Now render the template using the id:');
console.log('>> node examples/templates/render.js ' + id);

}, (error) => {
console.error('Request failed: ', error);
process.exit(1);
});
61 changes: 61 additions & 0 deletions examples/templates/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const Shotstack = require('shotstack-sdk');

const defaultClient = Shotstack.ApiClient.instance;
const DeveloperKey = defaultClient.authentications['DeveloperKey'];
const api = new Shotstack.EditApi();
const id = process.argv[2];

let apiUrl = 'https://api.shotstack.io/stage';

if (!id) {
console.log(">> Please provide the UUID of the template (i.e. node examples/templates/render.js 7feabb0e-b5eb-8c5e-847d-82297dd4802a)\n");
process.exit(1);
}

if (!process.env.SHOTSTACK_KEY) {
console.log('API Key is required. Set using: export SHOTSTACK_KEY=your_key_here');
process.exit(1);
}

if (process.env.SHOTSTACK_HOST) {
apiUrl = process.env.SHOTSTACK_HOST;
}

defaultClient.basePath = apiUrl;
DeveloperKey.apiKey = process.env.SHOTSTACK_KEY;

const mergeFieldUrl = new Shotstack.MergeField;
mergeFieldUrl
.setFind('URL')
.setReplace('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4');

const mergeFieldTrim = new Shotstack.MergeField;
mergeFieldTrim
.setFind('TRIM')
.setReplace(3);

const mergeFieldLength = new Shotstack.MergeField;
mergeFieldLength
.setFind('LENGTH')
.setReplace(6);

const template = new Shotstack.TemplateRender;
template
.setId(id)
.setMerge([
mergeFieldUrl,
mergeFieldTrim,
mergeFieldLength,
]);

api.postTemplateRender(template).then((data) => {
let message = data.response.message;
let id = data.response.id

console.log(message + '\n');
console.log('>> Now check the progress of your render by running:');
console.log('>> node examples/status.js ' + id);
}, (error) => {
console.error('Request failed: ', error);
process.exit(1);
});

0 comments on commit 7547f9f

Please sign in to comment.