Background Removal (Video)
Removes the background from a short video and keeps the subject. One request in, one video out: mask, a greyscale MP4 matte (white = subject) at the input's displayed size and frame rate, one matte frame per input frame. A 10-second clip usually finishes in seconds.
Create a job
POST /v1/video/remove-background with the video and, optionally, the model and a webhook URL. It answers 202 Accepted with the Job; the work runs in the background. Send an Idempotency-Key so a retry never creates a second job. Scopes: jobs:write, plus files:write with input_url or a multipart file (the input becomes a File).
curl -s https://api.zilapi.com/v1/video/remove-background \
-H "Authorization: Bearer $ZILAPI_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"input_url": "https://example.com/clip.mp4",
"model": "zilapi/bgr-video-1.0",
"webhook_url": "https://your-app.example.com/hooks/zilapi"}' \
> job.json
JOB_ID=$(jq -r .id job.json)| Field | Type | Description |
|---|---|---|
| input_url | string | An https URL we fetch, up to 250 MB. The job waits (waiting_for_file) until the video is fetched and checked. |
| file | string | Instead of input_url: a file_… id from Files (up to 250 MB), or, in a multipart request, the video itself (up to 50 MB). Send exactly one input. |
| model | string | Optional. Default zilapi/bgr-video-1.0. |
| webhook_url | string | Optional. An https URL that receives this job's job.succeeded, job.failed or job.canceled event, signed. See Webhooks. Example: https://your-app.example.com/hooks/zilapi. |
| end_user | string | Optional. Your own id for the user or workspace this job is for: 1 to 128 characters from A-Z a-z 0-9 . _ : @ -. Send an opaque or hashed id, never a raw email. See Jobs. |
There are no other options: the model finds the subject itself, and the matte always has the input's displayed size and frame rate.
Send a local video
For a video on your machine up to 50 MB, send it in the same request as multipart/form-data, in a field named file. model, webhook_url and end_user go along as plain fields if you want them.
curl -s https://api.zilapi.com/v1/video/remove-background \
-H "Authorization: Bearer $ZILAPI_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-F "file=@clip.mp4;type=video/mp4" \
> job.json
JOB_ID=$(jq -r .id job.json) # job_…, status "waiting_for_file" or "queued"Up to 250 MB, and for any video over about 5 MB or sent from far from the US East Coast: upload it straight to storage with Files first (content_type video/mp4, video/quicktime or video/webm; files over 100 MB use the multipart upload described there), then pass the ready file's id as file. Or host it and send input_url.
POST /v1/files, PUT the video to the signed URL, POST /v1/files/{id}/complete, then create the job with file (example below). The bytes go straight to storage instead of through our API servers on the US East Coast, so uploads from far away are more reliable, and a failed PUT can simply be sent again to the same URL. Keep the one-request upload for small files. More on uploads.#!/usr/bin/env bash
set -euo pipefail # stop at the first failed step
FILE=clip.mp4
AUTH="Authorization: Bearer $ZILAPI_KEY"
# 1. Start the upload: a file id and a signed PUT URL, valid for 6 hours
curl -fsS https://api.zilapi.com/v1/files -H "$AUTH" \
-H "Content-Type: application/json" \
-d '{"filename": "'"$FILE"'", "size_bytes": '"$(wc -c < "$FILE")"', "content_type": "video/mp4"}' \
> upload.json
FILE_ID=$(jq -er .id upload.json)
[ "$(jq -r .upload.method upload.json)" = PUT ] || { echo "over 100 MB: upload it in parts, see https://zilapi.com/docs/files/#multipart" >&2; exit 1; }
# 2. PUT the bytes straight to storage, with every header in upload.headers.
# A failed PUT is safe to send again to the same URL: --retry does that.
mapfile -t HEADERS < <(jq -r '.upload.headers | to_entries[] | "-H", "\(.key): \(.value)"' upload.json)
curl -fsS --retry 3 --retry-all-errors -X PUT "$(jq -r .upload.url upload.json)" "${HEADERS[@]}" --data-binary @"$FILE"
# 3. Finish. Usually "ready" in this response; a large file can still be "validating" for a moment
curl -fsS -X POST https://api.zilapi.com/v1/files/$FILE_ID/complete -H "$AUTH" > file.json
while [ "$(jq -r .status file.json)" = validating ]; do
sleep 2
curl -fsS https://api.zilapi.com/v1/files/$FILE_ID -H "$AUTH" > file.json
done
[ "$(jq -r .status file.json)" = ready ] || { jq -c '{status, error}' file.json >&2; exit 1; }
# 4. Create the job with the file id
curl -fsS https://api.zilapi.com/v1/video/remove-background -H "$AUTH" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"file": "'"$FILE_ID"'", "model": "zilapi/bgr-video-1.0"}' \
> job.json
JOB_ID=$(jq -er .id job.json)
echo "$JOB_ID $(jq -r .status job.json)" # job_… queuedOutput: the matte video
Wait until the job has succeeded (or use webhook_url), then ask for the matte with GET /v1/jobs/{id}/output?name=mask: a signed download URL, valid for up to 1 hour, plus the file's name. output.files on the job lists it (format: mp4).
while :; do
STATUS=$(curl -s https://api.zilapi.com/v1/jobs/$JOB_ID -H "Authorization: Bearer $ZILAPI_KEY" | jq -r .status)
case "$STATUS" in waiting_for_file|queued|processing) sleep 2 ;; *) break ;; esac
done
echo "$STATUS" # succeeded (or failed, canceled)| Name | Ask with | Filename | Contains |
|---|---|---|---|
| mask | ?name=mask | <stem>-matte.mp4 | The matte video: MP4 (H.264), full-range greyscale with white for the subject, at the input's displayed size and frame rate, one matte frame per input frame |
curl -s "https://api.zilapi.com/v1/jobs/$JOB_ID/output?name=mask" -H "Authorization: Bearer $ZILAPI_KEY" > output.json
curl -s -o "$(jq -r .filename output.json)" "$(jq -r .url output.json)" # clip-matte.mp4<stem> is your input's filename without its extension. Outputs are deleted 7 days after the job succeeds.
Transparent or green-screen video with ffmpeg
The matte lines up with your video frame for frame, so one ffmpeg command turns the two into a finished clip on your side. ffmpeg applies the input's rotation by default, just as we do for the matte.
# The matte becomes the alpha channel: a VP9 WebM with transparency (audio re-encoded to Opus).
ffmpeg -i clip.mp4 -i clip-matte.mp4 -filter_complex \
"[1:v]format=gray[m];[0:v][m]alphamerge,format=yuva420p[v]" \
-map "[v]" -map "0:a?" -c:v libvpx-vp9 -auto-alt-ref 0 -c:a libopus clip-transparent.webm# The subject over solid green (#00FF00): an H.264 MP4 for editors that key green (audio copied).
ffmpeg -i clip.mp4 -i clip-matte.mp4 -filter_complex \
"[1:v]format=gray[m];[0:v]split[fg][bg];[fg][m]alphamerge[cut];[bg]drawbox=c=0x00FF00:t=fill[green];[green][cut]overlay=format=auto,format=yuv420p[v]" \
-map "[v]" -map "0:a?" -c:v libx264 -crf 18 -c:a copy clip-green.mp4Speed and throughput
- A 10-second clip usually finishes in seconds; longer and higher-resolution clips take longer, and uploading and downloading add their own time.
- One video background removal job runs at a time per account. More are queued and start as the previous one finishes, within the 24-hour job lifetime: they do not fail because they waited.
- The 120 job creates per minute per API key apply to video jobs too (the shared create bucket, see Rate limits).
- Your account's limit on live jobs at once still applies (10 before your first top-up, 50 after): over it, a create answers
429 too_many_active_jobswithRetry-After, and the sameIdempotency-Keymakes the retry safe. See account limits.
Price and limits
zilapi/bgr-video-1.0is $0.10 per video minute, billed by the millisecond with a 1-second minimum: the charge isceil(billable_ms × price per minute / 60000)micro-dollars, wherebillable_msis the video's exact duration in milliseconds, measured from its frames and timeline (at least 1000 ms). Each job'sreceiptshows it withunit: "video_second"andbillable_ms.- Examples: a 0.5-second clip is billed as the 1-second minimum ($0.0017), a 10-second clip $0.0167, a 10.010-second clip $0.016684 exactly, a 30-second clip $0.05 and a 60-second clip $0.10. The dashboard shows each job's charge to 4 decimals (a 60-second clip as $0.1000) and its exact billed duration (10.010 s).
- Failed and canceled jobs cost nothing. A test key is free and returns a short sample matte instead of running the model.
- Inputs: MP4, MOV or WebM (H.264, HEVC, VP8, VP9 or AV1) up to 60 seconds, 250 MB, 1920×1080 in either orientation and 60 fps, at least 64 px and an even number of pixels per side (as displayed). The container's rotation is applied.
| Status | Code | When |
|---|---|---|
| 422 | invalid_video | The file is not a video we can decode (not MP4, MOV or WebM, another codec, no frames), a side is under 64 px or an odd number of pixels, or it runs at over 60 fps. |
| 422 | video_too_large | Larger than 1920×1080 (1080×1920 in portrait). |
| 422 | video_too_long | Longer than 60 seconds. |
| 413 | file_too_large | Over 50 MB in a multipart request, or over 250 MB by file or input_url. |
| 422 | model_not_available | The model is unknown or not available to your account. |
With a ready file the input checks answer the create call at once; with input_url or a multipart upload the job fails with the same error.code. Nothing is charged either way. See Errors.
Statuses, polling and canceling work as for every job: see Jobs. Credit and receipts: Pricing & billing. Model details: zilapi/bgr-video-1.0.