Skip to main content

Transforms

Transforms are ways to combine and compose multiple Operations.

note

Currently Transforms are in Alpha and only supported in JavaScript

Getting Started#

First, let's initialize a transform. You'll need a:

  1. An array of input files

  2. A webhook url

    All transforms are processed asynchronously

const exampleTransform = modfy.transform(inputFiles, webhookUrl)

Adding Operations#

Now that we have a transform, we can start adding our desired operations.

We do this by calling transform.add() and supplying a function which takes an array of input files, and returns a new instance of an operation.

The array of input files contains the output video of the last operation.

import { Convert, Compress } from '@modfy/core'
transform.add(inputFiles => new Convert({ inputFiles, to: { format: 'MOV' }}))
transform.add(inputFiles => new Compress({ inputFiles, compressValue: 10 }))

Type Reference#

The function has an argument of type AcceptedFiles and expects a return of type OperationClass.

OperationClass here is the parent class of all operations, so any valid operation is accepted

type AddFunc = (previouslyRenderedVideo: AcceptedFiles) => OperationClass
transform.add(func: AddFunc) => Transform

Wrapping things up#

Once you've added all your operations, we can now start executing the transform ๐Ÿ˜Ž

const { id, uuid } = await transform.process()
// Returns { id: string; uuid: string }

When processing is completed you will get a webhook response, check Async Processing to learn how to deal with the webhook

Full Example#

import Modfy, { Convert, Compress } from '@modfy/core'
const modfy = new Modfy({ token: '', secretToken: '' })
const transform = modfy.transform(['inputFile.mp4'], 'https://api.example.com/modfyWebhook')
const { uuid } = await transformer
.add(inputFiles => new Convert({ inputFiles, to: { format: 'MOV' } }))
.add(inputFiles => new Compress({ inputFiles, compressValue: 10 }))
.process()
// An example webhook handler on your server
router.post('/modfyWebhook', async (req: Request, res: Response) => {
const { uuid } = req.body
const buffer = await modfy.retrieve(uuid, 'arraybuffer')
fs.writeFileSync('video.mp4', buffer)
})