Video watch and playback
Video watch and playback turns a YouTube video ID into a usable viewing experience and a set of stable interfaces. It retrieves and caches video data, presents the data on watch and embed pages, and gives browsers and API clients access to streams, captions, clips, transcripts, storyboards, and related metadata.
Video retrieval and normalized data
Video retrieval
The video retrieval flow uses the YouTube player data as its source and keeps a local record so repeated page and API requests can reuse recent data. A missing or stale record is fetched again, while a region-specific request reads fresh data for that region. Retrieval errors become clear not-found or server errors at the page and API boundaries.
The retrieval result remains a single Video value for the rest of the system. This gives the watch page, embed page, playback routes, and API serializers the same view of the video.
flowchart LR
Request[Video request] --> Cache{Usable cached video?}
Cache -- Yes --> Video[Normalized video data]
Cache -- No --> Player[YouTube player data]
Player --> Parse[Video parser]
Parse --> Store[Video cache]
Store --> Video
Video --> Page[Watch or embed page]
Video --> API[Video and media APIs]
References: src/…/videos.cr• L303-360 src/…/parser.cr• L58-134
Video metadata
The normalized video data joins playback state with the information needed around the player. It covers title, author, description, dates, view and like counts, access state, region limits, category, license, music, comments state, related videos, and live or scheduled status.
The same data also carries media-specific values such as stream lists, HLS and DASH manifest links, captions, storyboards, projection type, and audio or video properties. API serialization exposes these values in a client-facing shape, while HTML views use them to build page content and player configuration.
References: src/…/parser.cr• L186-237 src/…/parser.cr• L323-457 src/…/video_json.cr• L6-77
Watch and embed presentation
Watch page
The watch page is the full viewing surface. It combines the player with video details, channel information, description, comments, download choices, watch history effects, playlist context, and related videos.
The route resolves the video before it prepares the page. It applies the request and user preferences to playback, selects preferred caption tracks, marks a watched video when the account settings allow it, and handles unavailable audio or unsupported quality choices before rendering. Raw playback requests can redirect directly to a selected audio or combined stream. Download requests reuse the current-stream and caption paths so they keep the same expiry and format rules.
References: src/…/watch.cr• L4-72 src/…/watch.cr• L114-196 src/…/watch.cr• L285-331 src/…/watch.ecr• L53-91 src/…/watch.ecr• L205-237
Embed page
The embed page provides the player without the full watch-page content. It accepts direct video IDs and can resolve playlist series, playlist positions, and live-stream aliases before it renders the embedded player.
Watch and embed pages share the player source and preference model. They differ in the surrounding page data: the watch page adds navigation and video context, while the embed page sends only the data needed for playback and embed navigation.
References: src/…/embed.cr• L35-136 src/…/embed.cr• L159-214 src/…/embed.ecr• L18-42
Player sources
The shared player maps the normalized media data to browser sources. A live video can use HLS when that mode is available. A normal video can use a combined stream, separate audio and video streams through DASH, or audio-only sources when listen mode is active.
The player also attaches caption tracks and passes a small JSON data set to the browser. That data controls choices such as quality, autoplay, start and end times, loop, VR mode, captions, and local proxy use. The browser-side player then manages playback controls and quality selection.
References: src/…/player.ecr• L4-99 src/…/player_sources.ecr• L1-31 src/…/video_preferences.cr• L33-164
Stream and manifest access
Stream formats
YouTube supplies two broad stream groups. Combined formats contain audio and video together, while adaptive formats separate video-only and audio-only streams. The system groups these into format streams, video streams, and audio streams so the page, API, and manifest paths can choose the right source.
Format metadata turns YouTube format identifiers into useful client fields such as container, codec, resolution, frame rate, and audio rate. The format table supports common MP4, WebM, HLS, DASH, and audio variants without making callers interpret each identifier themselves.
References: src/…/videos.cr• L94-123 src/…/formats.cr• L1-115 src/…/video_json.cr• L79-207
DASH manifest
The DASH route gives players one manifest that describes compatible audio and video representations. It prefers YouTube's manifest when one exists and can build a manifest from adaptive streams when it does not.
The manifest route can keep upstream URLs or replace them with local playback URLs. It also provides a playback bridge so DASH segment requests reach the guarded video proxy route. This lets browser clients use DASH without direct cross-origin access to YouTube media hosts.
References: src/…/manifest.cr• L1-143 src/…/manifest.cr• L146-166 src/…/routing.cr• L196-207
HLS manifest
HLS support serves playlist content for live playback. When local mode is requested, playlist URLs are rewritten to pass through Invidious playback routes instead of sending the browser to upstream media hosts.
This path is separate from the DASH path because live playlists and their variants use HLS media rules. The player selects the HLS source for live videos when livestream playback is enabled.
References: src/…/manifest.cr• L168-245 src/…/player.ecr• L10-12
Video playback proxy
The video playback proxy fetches media from an approved YouTube media host and streams it to the client. It resolves upstream redirects before serving content, supports range requests, adds cross-origin access for player use, and transfers large media in chunks.
The proxy also serves live transport-stream segments and has an options path for browser preflight requests. The path form of the endpoint normalizes encoded media parameters before redirecting to the query form. Host validation prevents the route from becoming a general-purpose open proxy. The latest_version route fetches current stream data before redirecting because YouTube playback URLs expire.
sequenceDiagram
participant P as Player or client
participant I as Invidious playback route
participant Y as YouTube media host
P->>I: Request stream or byte range
I->>I: Validate host and request
I->>Y: HEAD or ranged GET
Y-->>I: Redirect or media bytes
I->>Y: Follow approved host
I-->>P: Stream bytes and playback headers
References: src/…/video_playback.cr• L4-95 src/…/video_playback.cr• L96-215 src/…/video_playback.cr• L217-255 src/…/video_playback.cr• L257-313
Captions, transcripts, and preview data
Caption tracks
Caption metadata comes from the video response and identifies each track by label, language code, source URL, and whether YouTube generated it. Watch and embed pages order tracks by the user's preferred caption settings before adding them to the player.
The captions API lists available tracks when no track is selected. When a client selects one by label or language, it returns WebVTT. The service can obtain the track through the transcript flow or convert YouTube timed-text data into a player-friendly WebVTT form. A translation language can be requested, and a title can turn the result into a download.
References: src/…/caption.cr• L5-36 src/…/videos.cr• L29-175 src/…/watch.cr• L144-154
Transcripts
Transcripts provide timed text as structured lines rather than only a subtitle file. A line has a start time, end time, text, and either a regular or heading type. The API lists available transcript languages and then returns the selected transcript as JSON.
The transcript flow builds a request for the selected language and generated status, reads the YouTube transcript panel, and preserves its timing and label. Clients can select a transcript by language or label. Generated transcripts use an explicit query flag so the requested source stays distinct from a hand-made transcript.
References: src/…/transcript.cr• L4-24 src/…/transcript.cr• L51-123 src/…/videos.cr• L440-524
Storyboards
Storyboards are preview thumbnail sheets that let a player show visual seeking hints. The video data describes each sheet's dimensions, timing, image count, and grid. The storyboard API returns either these definitions or a WebVTT file whose cues point to the correct image regions.
This keeps preview images separate from the main video stream while still allowing the player to request a sheet that matches a requested width or height. Live storyboards use a simpler live-specific description.
References: src/…/storyboard.cr• L5-19 src/…/storyboard.cr• L32-120 src/…/videos.cr• L177-255
Clips
A clip is a time range that points to an existing video. The clip flow resolves the YouTube clip URL, reads its video ID and start or end times, then either redirects a browser to the matching watch URL or returns the range and full video data through the API.
The clip title stays with the API result when YouTube provides one. The watch redirect keeps the clip range as start and end parameters so the shared video preference flow can apply it to playback.
References: src/…/clip.cr• L3-25 src/…/watch.cr• L264-283 src/…/videos.cr• L397-437
API media surface
Video API
The video API exposes one serialized view of a video for programmatic clients. It includes identity, descriptive and channel data, playback state, thumbnails, storyboards, stream formats, captions, music tracks, and recommended videos.
Clients can request proxied media URLs through the local option. Otherwise, the response keeps the upstream format URLs and provides Invidious manifest and caption links for the supported playback paths. The same API family also exposes clip data and the caption, transcript, and storyboard forms that clients need to add playback features.
References: src/…/videos.cr• L7-27 src/…/videos.cr• L29-175 src/…/videos.cr• L177-255 src/…/videos.cr• L397-524 src/…/video_json.cr• L6-77 src/…/video_json.cr• L79-279
Route contracts
The video feature has separate contracts for page rendering, media transport, and structured data. Their relationship is stable: page routes resolve a video, playback routes serve the selected media, and API routes expose the same video and auxiliary media data without rendering HTML.
| Client need | Main route | Result |
|---|---|---|
| Full watch experience | /watch?v=<id> | HTML page with player and video context |
| Embedded playback | /embed/<id> | Minimal HTML player page |
| Current media URL | /latest_version | Redirect to a current stream URL |
| Stream bytes | /videoplayback | Proxied media or live segment data |
| Adaptive playback | /api/manifest/dash/id/<id> | DASH manifest |
| Video data | /api/v1/videos/<id> | JSON video object |
| Captions | /api/v1/captions/<id> | Track list or WebVTT |
| Transcripts | /api/v1/transcripts/<id> | Structured transcript JSON |
| Preview thumbnails | /api/v1/storyboards/<id> | Storyboard data or WebVTT |
| Clip data | /api/v1/clips/<id> | Clip range plus video JSON |
| Download selection | /download | Caption file or current media redirect |
References: src/…/routing.cr• L161-216 src/…/routing.cr• L242-253 src/…/watch.cr• L285-331
Related knowledge
The parent page, YouTube backend integration, explains the outbound access layer that supplies the player and page data. For channel and playlist context, see Channel content and subscriptions. For the route map, see Web and API route families. For the shape of API responses, see API v1 response serialization.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the normalized video fields must stay aligned with the watch and embed page data and the API v1 serializer.
- Changes to stream URL handling must be checked across direct player sources, DASH or HLS manifest rewriting,
/latest_version,/videoplayback, and the download handoff. - Changes to caption or transcript selection must preserve both the HTML player track links and the API list and fetch contracts. Check label, language, generated status, translation, and download behavior together.
- Changes to clip time parameters must remain compatible with watch start and end preference parsing.
- Changes to storyboard timing or image layout must stay compatible with both storyboard JSON fields and generated WebVTT cues.
- Changes to cache schema or refresh rules must account for playback URLs that expire before cached video data does.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| YouTube returns a different video ID than requested | The instance exposes data for the wrong video and playback is counted as a failure | The parser rejects the response and returns an error state |
| Cached video data is stale after its stream URLs expire | The player follows an invalid media URL | latest_version fetches current video data before redirecting |
| Adaptive streams are sent directly from YouTube | Browser cross-origin rules can block playback | Watch and embed pages proxy adaptive stream URLs |
| No audio stream exists for a non-live video | Audio mode or DASH playback cannot work | Watch and embed routes fall back from the unsupported choice |
| A playback request names an unapproved host | The endpoint could act as an open proxy | The playback route accepts only YouTube media host patterns |
| A requested caption or transcript label is absent | The client receives an empty or invalid media response | Caption and transcript routes return a not-found response |
| A requested storyboard size has no matching sheet | The player cannot build preview cues | The storyboard route returns not found instead of an unrelated sheet |
Key files
| File | Why to read it |
|---|---|
src/invidious/videos.cr | Defines the shared video data model and retrieval boundary used by pages and APIs |
src/invidious/videos/parser.cr | Shows how YouTube player data becomes normalized video and stream metadata |
src/invidious/routes/watch.cr | Traces the full watch request from video lookup to page rendering, raw playback, and downloads |
src/invidious/routes/embed.cr | Explains embed aliases, playlist series handling, and minimal player rendering |
src/invidious/views/components/player.ecr | Shows how stream choices and captions become browser player sources |
src/invidious/routes/video_playback.cr | Defines media proxy, range, redirect, live segment, and current-URL behavior |
src/invidious/routes/api/manifest.cr | Explains DASH and HLS manifest delivery and local URL rewriting |
src/invidious/routes/api/v1/videos.cr | Defines video, caption, storyboard, clip, and transcript API flows |
src/invidious/jsonify/api_v1/video_json.cr | Defines the client-facing video and playback metadata shape |
src/invidious/videos/transcript.cr | Defines structured transcript data and its WebVTT conversion |