Skip to main content

Module youtube

Module youtube 

Source
Expand description

YouTube videos: telling a link to one from any other, and finding what there is to play of it.

A link to YouTube is to a page, not to a file. In a browser the page plays the video with a player of YouTube’s own, which is a program that runs in a web engine, and this client has none. What it has is GStreamer (see crate::video), which plays a stream given its address. That address is not on the page for the reading: it is worked out by running what YouTube sends, and YouTube changes how every few weeks. Keeping up with that is the whole of what yt-dlp does, so it is asked: run as a program of its own, if it is installed, when a video is played and not before.

sequenceDiagram
    participant row as The video's row
    participant videos as Videos, in the model
    participant ytdlp as yt-dlp, a program of its own
    participant youtube as YouTube
    participant gst as GStreamer: playbin
    row->>videos: pressed: a YouTube link
    videos-->>row: on its way (the row says so)
    videos->>ytdlp: which stream of the video with this id?
    ytdlp->>youtube: the page, and what it takes to read it
    youtube-->>ytdlp: the streams there are
    ytdlp-->>videos: the address of one with pictures and sound
    videos->>gst: play this address
    gst->>youtube: the stream

yt-dlp is run as its user has set it up: its own configuration file holds here as at a terminal, which is where whatever it needs to get past YouTube on this machine is said (a JavaScript runtime, cookies, a proxy). If yt-dlp --get-url finds a video at a terminal, it is found here; and if not, what yt-dlp says is shown where the video would have played.

Nothing a note says reaches yt-dlp’s command line but the eleven characters of a video’s id, and those only once they are known to be letters, figures, - and _ (see VideoId).

A stream’s address is not good the moment it is given: YouTube turns away whoever asks for it before a time it names, a few seconds on, with a 403. yt-dlp is told that time and waits it out before it downloads anything; so it is asked for it here, along with the address, and waited for before the stream is handed to be played.

One address is asked for, since that is what a playbin plays: one file with both the pictures and the sound in it. YouTube keeps such a file only in a small size (360 lines), and its larger sizes as pictures and sound apart; for a row of a popup that is enough, and the link opens the video in the browser for more.

Of a video that is live there is no such file. It comes as HLS: its pictures in one run of short files and its sound in another, in several sizes, all listed in one playlist. That playlist is one address too, and GStreamer plays what it lists, pictures with sound; so where there is no file, it is the playlist that is asked for (see FORMAT and found).

flowchart TD
    asked["yt-dlp is asked for a stream"] --> file{"Is there one file of pictures and sound?"}
    file -->|"yes: a video that was put up"| address["Its address is played"]
    file -->|"no: a video that is live"| hls{"Are there pictures that come as HLS?"}
    hls -->|"yes"| playlist["The playlist that lists them, and their sound, is played"]
    hls -->|"no"| said["What yt-dlp says is shown where the video would have played"]

Structs§

Video
A YouTube video, as a link names it.
VideoId
What YouTube knows a video by: eleven letters, figures, - or _. There is no making one of anything else, so one can be handed to a command line, or written into an address, as it is.

Enums§

FindError
Why no stream of a video was found.

Constants§

AVAILABLE_AT 🔒
What yt-dlp is asked to write before the stream’s address: when the stream is good to fetch from, in seconds since the Unix epoch, or NA if YouTube named no time.
AVAILABLE_WAIT_MAX 🔒
How long a stream is waited for to be good to fetch, at the most. It is a few seconds; a clock that is wrong must not make it an hour.
FIND_TIMEOUT 🔒
How long yt-dlp gets to find a stream. It answers in a few seconds when it answers.
FORMAT 🔒
The stream asked for: the best that is one file of pictures and sound, no taller than a popup has any use for. Failing any such file, as of a video that is live, pictures that come as HLS: not to be played themselves, having no sound, but for the playlist they are listed in.
ID_CHARS 🔒
How many characters a video’s id has.
LISTED_IN 🔒
And the playlist the stream is listed in, with the others there are of the video, if it is listed in one; NA if not.
SHORT_SIZE 🔒
The proportions of a YouTube short, which is held upright, in pixels as they come.
SOUND 🔒
What yt-dlp is asked to write of the stream it chose: what its sound is in, which is none if it has none.

Functions§

find_stream
Ask yt-dlp for an address at which video is to be had with both its pictures and its sound, and answer with it once it is good to fetch from. See the module docs.
found 🔒
What to play of what yt-dlp found, in what it wrote: the first line that is an https address, which is the stream’s. Its configuration may have it write more. If the stream has no sound (see SOUND), the playlist it is listed in instead (see LISTED_IN), which lists the sound as well; and nothing if it is listed in none, rather than pictures in silence.
said_wrong 🔒
What went wrong, in what yt-dlp wrote to its error output. That it has no JavaScript runtime, or may not use the one it has to solve what YouTube sets it, if it warned of either: whatever error follows comes of that. Otherwise the last of the lines it marks as errors, without the mark, or else the last line it wrote at all.
title
What a video is called, in what YouTube answers when asked about it.
video
The YouTube video url links to, if it links to one: its page, a short, a live one, an embedded one, or the short address of any of them. Only an https link is one, as with everything fetched. A channel, a playlist or a search is not a video.
video_at
The YouTube video the address url links to, if it is an address and links to one (see video).
wait_until_available 🔒
How long to wait before the stream yt-dlp found is asked for, in what it wrote (see AVAILABLE_AT), it being now seconds since the Unix epoch: until the time YouTube named, if it named one that is still to come, and no longer than AVAILABLE_WAIT_MAX.