Sub topic 6
Building the transcoding pipeline
This chapter assumes you know what a codec and a container are, and that you have seen the upload and ingest stage that feeds this one. You should be comfortable with message queues and object storage, and able to read a shell command.
What the pipeline does
Transcoding converts one source file into several renditions. A rendition is a version of the video at a specific resolution and bitrate. Each rendition must also be cut so that a player can switch between them cleanly. The pipeline is the system that runs this work reliably for thousands of uploads.
Transcoding is CPU-heavy and slow. A one-hour video can take many minutes to encode, so it cannot run inside the upload request. It runs as asynchronous jobs.
Pipeline stages
- Trigger. The ingest service writes the source to object storage and publishes an event. A queue holds the job so workers can pull at their own pace.
- Probe and validate. A worker inspects the file, for example with
ffprobe. It rejects corrupt files and records duration, codec, and resolution. - Encode. One job per rendition runs in parallel. Each rendition is independent, so this is easy to scale out.
- Package. The outputs are segmented and a master playlist is written. The next chapters cover the bitrate ladder and packaging in detail.
- Publish. The worker updates the metadata store. Only then does the video become visible to viewers.
Encoding one rendition
This command produces a 720p H.264 rendition cut into 4-second HLS segments:
ffmpeg -i input.mp4 \
-vf scale=-2:720 \
-c:v libx264 -preset medium -b:v 2800k -maxrate 3000k -bufsize 6000k \
-g 48 -keyint_min 48 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 -hls_playlist_type vod \
-hls_segment_filename "720p/seg_%04d.ts" \
720p/index.m3u8Several flags matter for streaming:
-g 48 -keyint_min 48 -sc_threshold 0forces a keyframe every 48 frames and disables extra keyframes at scene changes. At 24 frames per second that is every 2 seconds, so each 4-second segment starts on a keyframe.-maxrateand-bufsizecap bitrate spikes, so the real bitrate stays near what the ladder promises.scale=-2:720keeps the aspect ratio and picks an even width, which H.264 requires.
Making it reliable
Workers fail, machines are reclaimed, and some files are malformed. Design for that:
| Concern | Approach |
|---|---|
| Worker crash | Use a visibility timeout on the queue, so an unacknowledged job returns for retry. |
| Duplicate runs | Make jobs idempotent. Write output to a path derived from the video ID and rendition, so a retry overwrites the same files. |
| Poison files | Retry a limited number of times, then move the job to a dead-letter queue and mark the video as failed. |
| Partial output | Write to a temporary prefix and publish the metadata update last, so viewers never see half-finished renditions. |
| Long videos | Split the source into chunks at keyframes, encode chunks in parallel, then stitch the results. |
Splitting long videos trades extra coordination for lower latency. For short clips, one job per rendition is simpler and usually enough.
Common errors
- Encoding on the API servers, which starves request handling.
- Letting renditions use different keyframe intervals.
- Marking a video ready before every rendition and the master playlist exist.
- Retrying forever on a file that can never decode.
Exercise
A worker crashes halfway through encoding the 1080p rendition, and the queue redelivers the job. Name two design choices that make the retry safe, and say when the video should become visible to viewers.
Show answer
Idempotent jobs that write to a path derived from the video ID and rendition, so the retry overwrites partial output, and a queue visibility timeout so the unacknowledged job returns for retry. Writing to a temporary prefix also helps. The video becomes visible only after all renditions and the master playlist exist and the metadata store is updated to ready.