Sub topic 5
Uploading and ingesting source video
This chapter assumes you know object storage basics (buckets, keys, presigned URLs), message queues, and how an API talks to a database. Ingest takes a creator's file from their device to a validated original in storage, then hands it to the transcoding pipeline.
Why the API server should not carry the bytes
Source files are large, often several gigabytes. If they pass through your API servers, those servers spend connections and bandwidth on a job that object storage does better. The usual design splits the work:
- The control plane is your API. It authenticates the user, creates a video record, and issues upload instructions.
- The data plane is object storage. The client sends bytes straight to it using presigned URLs.
Resumable multipart uploads
A single PUT of a 4 GB file fails on any network drop. A multipart upload splits the file into parts, commonly 8 to 64 MB each. The client uploads parts in parallel and retries only the failed ones. When all parts are in, the client asks storage to assemble them.
import boto3
s3 = boto3.client("s3")
BUCKET = "video-uploads"
def start_upload(video_id: str, parts: int) -> dict:
key = f"raw/{video_id}/source.mp4"
upload = s3.create_multipart_upload(
Bucket=BUCKET, Key=key, ContentType="video/mp4"
)
urls = [
s3.generate_presigned_url(
"upload_part",
Params={
"Bucket": BUCKET,
"Key": key,
"UploadId": upload["UploadId"],
"PartNumber": n,
},
ExpiresIn=3600,
)
for n in range(1, parts + 1)
]
return {"upload_id": upload["UploadId"], "key": key, "part_urls": urls}The client PUTs each part and records the ETag header from each response. It then sends the list of part numbers and ETags to your API, which calls complete_multipart_upload.
From upload to validated source
Completing the upload does not mean the file is usable. The ingest flow looks like this:
The validator should check these things before queuing work:
- The container and codecs are readable, for example with
ffprobe -v error -show_streams -of json. - Duration, resolution and file size are within platform limits.
- A video stream exists.
- The checksum does not match an existing upload, if you want deduplication.
Track state explicitly
Store a status on the video record: created, uploading, uploaded, validating, queued, or rejected. Each stage only moves it forward. Clients and support staff can then see where a video is stuck.
Common failure modes
| Problem | Cause | Mitigation |
|---|---|---|
| Orphaned parts accumulate | Users abandon uploads | Lifecycle rule that aborts incomplete multipart uploads after a few days |
| Duplicate transcode jobs | Storage events are delivered more than once | Make the job key the video ID and ignore repeats |
| Corrupt file accepted | No probe step | Validate before queuing and mark `rejected` with a reason |
| Expired URL mid-upload | Short expiry on a slow connection | Let the client request fresh part URLs for the remaining parts |
Keep the original file. Later chapters re-transcode from it when you add codecs or ladder rungs, so store it in a cheaper storage class once the renditions exist.
Exercise
Storage delivers the "object created" event twice for the same upload. What happens without protection, and how do you prevent it?
Show answer
Two transcode jobs for the same video run, which wastes compute and can overwrite outputs. Prevent this by making the job idempotent. Use the video ID as the job key and skip the job if the video's status is already queued or later.