Skip to content

Commit

Permalink
Add Ingest API resize video example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Shillitto committed May 5, 2024
1 parent eb29477 commit 3c17cdc
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
- **create-api/text-to-image.js** -
Generate an image using the Shotstack text-to-image provider.

### Ingest and transformation examples
- **ingest-api/resize-video.js** -
Resize a video from 1080p to 720p resolution.

### Installation

Install the required dependencies including the [Shotstack Node SDK](https://www.npmjs.com/package/shotstack-sdk)
Expand Down Expand Up @@ -181,3 +185,20 @@ To check the status of a create task, run the `status.js` example with the asset
```bash
node examples/create-api/status.js 01gx3-2827k-dxmpz-x5n32-chw4oq
```

#### 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.

To resize a video from 1080p to 720p resolution:

```bash
node examples/ingest-api/resize-video.js
```

To check the status of an ingest task, run the `status.js` example with the ingest ID:

```bash
node examples/ingest-api/status.js zzy7wxvy-1h1e-vt4j-kn0y-3qn7kj1hocpw
```
2 changes: 1 addition & 1 deletion examples/create-api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ api.getGeneratedAsset(id).then((asset) => {
if (status == 'done') {
console.log(`>> Asset URL: ${asset.data.attributes.url}`);
} else if (status == 'failed') {
console.log('>> Something went wrong, rendering has terminated and will not continue.');
console.log('>> Something went wrong, processing has terminated and will not continue.');
} else {
console.log('>> Processing in progress, please try again shortly.\n>> Note: Processing may take some time to complete.');
}
Expand Down
10 changes: 7 additions & 3 deletions examples/create-api/text-to-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ defaultClient.basePath = apiUrl;
DeveloperKey.apiKey = process.env.SHOTSTACK_KEY;

const textToImage = new Shotstack.ShotstackTextToImageOptions;
textToImage.setWidth(1024).setHeight(1024).setPrompt('A realistic photo of the planet Mars with a black outer space background');
textToImage
.setPrompt('A realistic photo of the planet Mars with a black outer space background')
.setWidth(1024)
.setHeight(1024);

const generatedAsset = new Shotstack.ShotstackGeneratedAsset;
generatedAsset.setOptions(textToImage);
generatedAsset
.setOptions(textToImage);

api.postGenerateAsset(generatedAsset).then((asset) => {
const status = asset.data.attributes.status;
const id = asset.data.id

console.log(`Request '${status}' with id: ${id}\n`);
console.log('>> Now check the progress of image by running:');
console.log('>> Now check the progress by running:');
console.log(`>> node examples/create-api/status.js ${id}`);
}, (error) => {
console.error('Request failed: ', error);
Expand Down
52 changes: 52 additions & 0 deletions examples/ingest-api/resize-video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Shotstack = require('shotstack-sdk');

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

const apiUrlBase = 'https://api.shotstack.io/ingest/';
let apiUrl = apiUrlBase + '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_CREATE_HOST) {
apiUrl = process.env.SHOTSTACK_CREATE_HOST;
}

if (process.env.SHOTSTACK_ENV) {
apiUrl = apiUrlBase + process.env.SHOTSTACK_ENV;
}

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

const size = new Shotstack.Size;
size
.setHeight(720);

const rendition = new Shotstack.Rendition;
rendition
.setSize(size);

const outputs = new Shotstack.Outputs;
outputs
.setRenditions([rendition]);

const source = new Shotstack.Source;
source
.setUrl('https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/footage/cliffs-sunset.mp4')
.setOutputs(outputs);

api.postSource(source).then((source) => {
const id = source.data.id

console.log(`Request 'queued' with id: ${id}\n`);
console.log('>> Now check the progress by running:');
console.log(`>> node examples/ingest-api/status.js ${id}`);
}, (error) => {
console.error('Request failed: ', error);
process.exit(1);
});
49 changes: 49 additions & 0 deletions examples/ingest-api/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Shotstack = require('shotstack-sdk');

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

const apiUrlBase = 'https://api.shotstack.io/ingest/';
let apiUrl = apiUrlBase + 'stage';

const id = process.argv[2];

if (!id) {
console.log(">> Please provide the UUID of the ingest task (i.e. node examples/ingest-api/status.js zzy7wxvy-1h1e-vt4j-kn0y-3qn7kj1hocpw)\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;
}

if (process.env.SHOTSTACK_ENV) {
apiUrl = apiUrlBase + process.env.SHOTSTACK_ENV;
}

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

api.getSource(id).then((source) => {
const status = source.data.attributes.outputs.renditions[0].status;

console.log(`Status: '${status}'\n`);

if (status == 'ready') {
console.log(`>> Source URL: ${source.data.attributes.source}`);
console.log(`>> Rendition URL: ${source.data.attributes.outputs.renditions[0].url}`);
} else if (status == 'failed') {
console.log('>> Something went wrong, processing has terminated and will not continue.');
} else {
console.log('>> Processing in progress, please try again shortly.\n>> Note: Processing may take some time to complete.');
}
}, (error) => {
console.error('Request failed or not found: ', error);
process.exit(1);
});

0 comments on commit 3c17cdc

Please sign in to comment.