Skip to content

Commit

Permalink
Add smart clip examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Shillitto committed Jul 30, 2024
1 parent f23a750 commit 6118fdd
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Shotstack Node Examples

## Examples

### Video examples

- **text.js** -
Expand Down Expand Up @@ -54,6 +56,14 @@
- **gif.js** -
Create an animated gif that plays once.

### Smart clip examples

- **videos-auto** -
Automatically sequence a series of video clips so that they play one after another.

- **images-auto** -
Automatically sequence a series of images so that they display one after another.

### Template examples

- **templates/create.js**
Expand Down Expand Up @@ -105,15 +115,15 @@
- **ingest-api/resize-video.js** -
Resize a video from 1080p to 720p resolution.

### Installation
## Installation

Install the required dependencies including the [Shotstack Node SDK](https://www.npmjs.com/package/shotstack-sdk)

```bash
npm install
```

### Set your API key
## Set your API key

The demos use the **staging** endpoint by default so use your provided **staging** key:

Expand All @@ -130,12 +140,12 @@ set SHOTSTACK_KEY=your_key_here
You can [get an API key](http://shotstack.io/?utm_source=github&utm_medium=demos&utm_campaign=node_sdk) via the
Shotstack web site.

### Run an example
## Run an example

The examples directory includes a number of examples demonstrating the capabilities of the
Shotstack API.

#### Video editing (Edit API)
### Video editing (Edit API)

To run a rendering/editing example run the examples at the root of the examples folder.

Expand All @@ -151,7 +161,7 @@ To check the status of a render, run the `status.js` example with the render ID:
node examples/status.js 8b844085-779c-4c3a-b52f-d79deca2a960
```

#### Storing and hosting assets (Serve API)
### Storing and hosting assets (Serve API)

To look up assets hosted by Shotstack run the examples in the [examples/serve-api](./examples/serve-api/) directory.

Expand All @@ -169,7 +179,7 @@ Find an asset by asset ID:
node examples/serve-api/assetId.js 3f446298-779c-8c8c-f253-900c1627b776
```

#### Generating assets using AI (Create API)
### Generating assets using AI (Create API)

To create assets using Generative AI providers run the examples in the [examples/create-api](./examples/create-api/)
directory.
Expand All @@ -186,7 +196,7 @@ To check the status of a create task, run the `status.js` example with the asset
node examples/create-api/status.js 01gx3-2827k-dxmpz-x5n32-chw4oq
```

#### Ingesting and transforming/transcoding assets (Ingest API)
### Ingesting and transforming/transcoding assets (Ingest API)

To ingest and transform/transcode assets run the examples in the [examples/ingest-api](./examples/ingest-api/)
directory.
Expand Down
89 changes: 89 additions & 0 deletions examples/images-auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 images = [
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-712850.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-867452.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-752036.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-572487.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-114977.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-347143.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-206290.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-940301.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-266583.jpeg',
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-539432.jpeg'
];

let clips = [];
const length = 1.5;

let soundtrack = new Shotstack.Soundtrack;
soundtrack
.setSrc('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/music/gangsta.mp3')
.setEffect('fadeInFadeOut');

images.forEach((image) => {
let imageAsset = new Shotstack.ImageAsset;
imageAsset
.setSrc(image);

let clip = new Shotstack.Clip;
clip
.setAsset(imageAsset)
.setStart('auto')
.setLength(length)
.setEffect('zoomIn');

clips.push(clip);
});

let track = new Shotstack.Track;
track
.setClips(clips);

let timeline = new Shotstack.Timeline;
timeline
.setBackground('#000000')
.setSoundtrack(soundtrack)
.setTracks([track]);

let output = new Shotstack.Output;
output
.setFormat('mp4')
.setResolution('sd')
.setFps(30);

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

api.postRender(edit).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);
});
78 changes: 78 additions & 0 deletions examples/videos-auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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;

let videoAsset1 = new Shotstack.VideoAsset;
videoAsset1
.setSrc('https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/footage/shore-overhead.mp4')

let videoAsset2 = new Shotstack.VideoAsset;
videoAsset2
.setSrc('https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/footage/cliffs-sunset.mp4')

let videoAsset3 = new Shotstack.VideoAsset;
videoAsset3
.setSrc('https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/footage/tree.mp4')

let videoClip1 = new Shotstack.Clip;
videoClip1
.setAsset(videoAsset1)
.setStart(0)
.setLength('auto');

let videoClip2 = new Shotstack.Clip;
videoClip2
.setAsset(videoAsset2)
.setStart('auto')
.setLength('auto');

let videoClip3 = new Shotstack.Clip;
videoClip3
.setAsset(videoAsset3)
.setStart('auto')
.setLength('auto');

let track = new Shotstack.Track;
track.setClips([videoClip1, videoClip2, videoClip3]);

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

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

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

api.postRender(edit).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 6118fdd

Please sign in to comment.