---
title: Upload a video
description: "Create an analysis, upload the file to its upload URL, resume an interrupted upload, and start the analysis."
url: "https://mediaseer.ai/docs/guides/upload/"
---

# Upload a video

An analysis begins with [`createVodAnalysis`](https://mediaseer.ai/docs/api/operations/createvodanalysis/): you send the file’s name and size, and get back the analysis in state `UPLOADING` and an upload URL (`upload.url`). You upload the file to that URL, then start the analysis with [`startVodAnalysis`](https://mediaseer.ai/docs/api/operations/startvodanalysis/).

Creating an analysis takes one of your account’s [analysis slots](https://mediaseer.ai/docs/lifecycle/#slots) right away, before any byte is uploaded. The file never passes through the API: the upload URL is a Google Cloud Storage resumable upload session, and you send the file straight to it, without your API key. Whoever has the URL can write the upload, so treat it like a key: keep it out of logs, and off command lines, which other users of the machine can read in the process list. The examples hand it to curl in a config file (`-K`).

The examples need bash, curl and jq. They read `MEDIASEER_API_URL` (the API’s address without `/api/v1`, as in the [quickstart](https://mediaseer.ai/docs/guides/quickstart/#prepare)), `MEDIASEER_API_KEY` and `VIDEO_FILE` from the environment, and print `created: <id>` as soon as the analysis exists.

## Create and upload

```bash title="upload.sh"
#!/usr/bin/env bash
#
# Creates an analysis for VIDEO_FILE and uploads the file to its upload URL the resumable way: it asks
# the upload session how much it holds, then sends the rest. It doesn't start the analysis.
set -euo pipefail

base="${MEDIASEER_API_URL:?the API origin, without /api/v1}"
base="${base%/}/api/v1"
: "${MEDIASEER_API_KEY:?your API key}"
file="${VIDEO_FILE:?the video file to upload}"

api() { curl -q -sS -H @- "$@" <<<"X-Api-Key: $MEDIASEER_API_KEY"; }
storage() { curl -q -s --globoff -K "$tmp/upload-url" "$@"; }

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

name="$(basename "$file")"
size="$(wc -c <"$file" | tr -d ' ')"
case "$name" in
  *.mp4 | *.m4v) type=video/mp4 ;;
  *.mov) type=video/quicktime ;;
  *.webm) type=video/webm ;;
  *.mkv) type=video/x-matroska ;;
  *) type="" ;;
esac

# Create the analysis. It takes a slot, and its answer holds the upload URL.
jq -n --arg name "$name" --argjson size "$size" --arg type "$type" \
  '{fileName: $name, sizeBytes: $size} + (if $type == "" then {} else {contentType: $type} end)' \
  >"$tmp/create.json"
status="$(api -o "$tmp/created.json" -w '%{http_code}' -H 'Content-Type: application/json' \
  --data-binary @"$tmp/create.json" "$base/vod-analysis")" || {
  echo "create got no answer: curl exit $?" >&2
  exit 1
}
if [[ "$status" != 201 ]]; then
  echo "create answered $status $(jq -r '.code // empty' "$tmp/created.json" 2>/dev/null || true)" >&2
  exit 1
fi
id="$(jq -r .analysis.id "$tmp/created.json")"
echo "created: $id"

# The upload URL is a credential: it goes into curl's config file and nowhere else.
jq -r '"url = \(.upload.url | tojson)"' "$tmp/created.json" >"$tmp/upload-url"

# Ask the session how much it holds. 308 means "not complete", with "Range: bytes=0-<last byte>" once
# it holds anything; 200 or 201 means it already has the whole file.
status="$(storage -o /dev/null -D "$tmp/headers" -w '%{http_code}' -X PUT \
  -H 'Content-Length: 0' -H "Content-Range: bytes */$size")" || {
  echo "the upload failed: curl exit $?" >&2
  exit 1
}
if [[ "$status" == 308 ]]; then
  last="$(awk 'tolower($0) ~ /^range: bytes=0-/ { sub(/^[^-]*-/, ""); sub(/\r$/, ""); print }' "$tmp/headers")"
  if [[ -n "$last" && ! "$last" =~ ^(0|[1-9][0-9]*)$ ]]; then
    echo "the upload answered 308 with an unexpected Range header" >&2
    exit 1
  fi
  offset=$((${last:--1} + 1))
  status="$(tail -c "+$((offset + 1))" "$file" | storage -o /dev/null -w '%{http_code}' --upload-file - \
    -H "Transfer-Encoding:" -H "Content-Length: $((size - offset))" -H "Content-Range: bytes $offset-$((size - 1))/$size")" || {
    echo "the upload failed: curl exit $?" >&2
    exit 1
  }
fi
if [[ "$status" != 200 && "$status" != 201 ]]; then
  echo "the upload answered $status" >&2
  exit 1
fi
echo "uploaded: $id"
```

- The size you declare in `sizeBytes` is checked against the [size limit](https://mediaseer.ai/docs/limits/#file-size-and-video-length) when you create the analysis, and the uploaded file again when you start it.
- Check the [upload URL and start deadlines](https://mediaseer.ai/docs/limits/#how-long-links-last). After the start deadline, create a new analysis.
- If a step after the create fails, the analysis still holds its slot. Delete it with [`deleteVodAnalysis`](https://mediaseer.ai/docs/api/operations/deletevodanalysis/), or upload the file and start it.

## Resume an interrupted upload

How an upload resumes depends on the client:

- The app remembers an upload in your browser. Pick the same file again, also after a reload, and the upload goes on where it stopped.
- The CLI resumes an upload after network errors only while it runs. Once the process has ended, nothing resumes it: upload the file again, as a new analysis.
- With the API, it is your code that resumes, with the upload URL. Keep the URL until the upload has finished.

To find out where to go on, send a `PUT` without a body to the upload URL, with `Content-Range: bytes */<size>`, as the example does before it sends anything:

- `308` with `Range: bytes=0-<last>`: the session holds the bytes up to `<last>`. Send the rest from the next byte, with its `Content-Range`.
- `308` without `Range`: the session holds nothing yet. Send the whole file.
- `200` or `201`: the session has the whole file.
- `404` or `410`: the session is gone. Create a new analysis.

## Not from your own web page

Browser uploads work only from Mediaseer’s app; upload from your server or a native client.

## Start

```bash title="start.sh"
#!/usr/bin/env bash
#
# Creates an analysis for VIDEO_FILE, uploads the file in one request and starts the analysis. The
# start is paid: it runs an encoding and a scene analysis.
set -euo pipefail

base="${MEDIASEER_API_URL:?the API origin, without /api/v1}"
base="${base%/}/api/v1"
: "${MEDIASEER_API_KEY:?your API key}"
file="${VIDEO_FILE:?the video file to analyse}"

api() { curl -q -sS -H @- "$@" <<<"X-Api-Key: $MEDIASEER_API_KEY"; }
storage() { curl -q -s --globoff -K "$tmp/upload-url" "$@"; }

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

name="$(basename "$file")"
size="$(wc -c <"$file" | tr -d ' ')"

# Create the analysis: it takes a slot, and its answer holds the upload URL.
jq -n --arg name "$name" --argjson size "$size" '{fileName: $name, sizeBytes: $size}' >"$tmp/create.json"
status="$(api -o "$tmp/created.json" -w '%{http_code}' -H 'Content-Type: application/json' \
  --data-binary @"$tmp/create.json" "$base/vod-analysis")" || {
  echo "create got no answer: curl exit $?" >&2
  exit 1
}
if [[ "$status" != 201 ]]; then
  echo "create answered $status $(jq -r '.code // empty' "$tmp/created.json" 2>/dev/null || true)" >&2
  exit 1
fi
id="$(jq -r .analysis.id "$tmp/created.json")"
echo "created: $id"

# Upload the whole file in one request. The upload URL goes into curl's config file and nowhere else.
jq -r '"url = \(.upload.url | tojson)"' "$tmp/created.json" >"$tmp/upload-url"
status="$(storage -o /dev/null -w '%{http_code}' --upload-file "$file" \
  -H "Content-Range: bytes 0-$((size - 1))/$size")" || {
  echo "the upload failed: curl exit $?" >&2
  exit 1
}
if [[ "$status" != 200 && "$status" != 201 ]]; then
  echo "the upload answered $status" >&2
  exit 1
fi

# Start the analysis. From here on it costs money, also when you stop or delete it later.
status="$(api -o "$tmp/started.json" -w '%{http_code}' -X POST -H 'Content-Length: 0' \
  "$base/vod-analysis/$id/start")" || {
  echo "start got no answer: curl exit $?" >&2
  exit 1
}
if [[ "$status" != 202 ]]; then
  echo "start answered $status $(jq -r '.code // empty' "$tmp/started.json" 2>/dev/null || true)" >&2
  exit 1
fi
state="$(jq -r .state "$tmp/started.json")"
echo "state: $state"
```

Starting is the step that costs money: it runs an encoding and a scene analysis, and counts against your [starts per day](https://mediaseer.ai/docs/limits/#starts-per-day). Stopping or deleting the analysis later doesn’t give the start back. The answer is `202` with the analysis in `QUEUED`. If the upload hasn’t finished yet, the start answers `409` `UPLOAD_INCOMPLETE` and the analysis stays `UPLOADING`; start it again once the upload is done. All of a start’s refusals are on the [limits page](https://mediaseer.ai/docs/limits/#when-create-and-start-refuse).

To follow the analysis from here, see [Wait for the result](https://mediaseer.ai/docs/guides/wait/).
