httpx.
The code
cut-clips.mjs
How it works
The import does the heavy part.includeTranscript: true with transcriptMode: "ai" runs speech-to-text on the same job, so the result carries mediaUrl (the video) and transcript.data.segments: one segment per sentence, with start and end in seconds. File and transcript are billed together, and totalCostInDollars is the total.
compatibleCodecs: true asks for H.264 video with AAC audio, which every editor, browser and phone plays. Leave it out and you may get a smaller AV1 or VP9 file at the same resolution.
maxCostDollars caps the job. A long video at 720p plus speech-to-text costs more than a short clip, and a job that would go over the cap fails with MAX_COST_EXCEEDED before anything is charged.
The cut re-encodes. Copying the stream (-c copy) is faster but starts on the nearest keyframe, which can be seconds before the sentence begins. Re-encoding a short clip takes a moment and lands on the sentence. PAD adds half a second on each side so the first and last words aren’t clipped.
matchingSegments is a plain substring search. Replace it with whatever your app needs: a keyword list, a search index over the segments, or an LLM that reads the transcript and returns the timestamps of the best moments.
download reads the whole file into memory, which is fine for short videos. For long ones, stream it to disk instead: await pipeline(Readable.fromWeb(res.body), createWriteStream(path)) with pipeline from node:stream/promises.
Errors to handle
Every error has a
documentationUrl with the fix. See Error handling.
In production
Don’t poll inside a web request. Start the import from your API route with awebhookUrl, and cut the clips in a background worker when Importly calls back. See Webhooks, and BullMQ or Inngest for the worker side.
Keep the source video. Add store: true and a storagePath so the file also lands in your own bucket, then cut new clips from there whenever a user asks, without importing again. See Save to your bucket.
Captions cost less when the video has them. Send transcriptMode: "captions" (or "auto", the default, which uses captions and falls back to speech-to-text when there are none) and transcriptLanguage to use the platform’s captions, which are timed line by line and sometimes auto-generated. See Transcripts.
A 10-minute 720p video (about 77 MB) costs about $0.15 to import, and $0.10 more for the speech-to-text transcript. Status checks are free. Try it free, no card required. See Pricing & credits.