Struct rustube::VideoFetcher [−][src]
pub struct VideoFetcher { /* fields omitted */ }
Expand description
A fetcher used to download all necessary data from YouTube, which then could be used to extract video-URLs.
You will probably rarely use this type directly, and use Video
instead.
Example
const URL: &str = "https://youtube.com/watch?iv=5jlI4uzZGjU"; let url = Url::parse(URL).unwrap(); let fetcher: VideoFetcher = VideoFetcher::from_url(&url).unwrap();
How it works
So you want to download a YouTube video? You probably already noticed, that YouTube makes this quite hard, and does not just provide static URLs for their videos. In fact, there’s not the one URL for each video. When currently nobody is watching a video, there’s actually no URL for this video at all!
So we need to somehow show YouTube that we want to watch the video, so the YouTube server
generates a URL for us. To do this, we do what every ‘normal’ human being would do: we
request the webpage of the video. To do so, we need nothing more, then the video’s id (If you
want to learn more about the id, you can have a look at Id
. But you don’t need to know
anything about it for now). Let’s, for example, take the id ‘5jlI4uzZGjU’. With this id, we
can then visit https://youtube.com/watch?v=5jlI4uzZGjU, the site, you as a human would visit
when just watching the video.
The next step is to extract as much information from https://youtube.com/watch?v=5jlI4uzZGjU as possible. This is, i.e., information like “is the video age-restricted?”, or “can we watch the video without being a member of that channel?”.
But there’s information, which is a lot more important then knowing if we are old enough to watch the video: The VideoInfo
, the PlayerResponse
and the JavaScript of the
page. VideoInfo
and PlayerResponse
are JSON objects, which contain the most
important information about the video. If you are feeling brave, feel free to have a look
at the definitions of those two types, their subtypes, and all the information they contain
(It’s huge!). The JavaScript is not processed by fetch
, but is used later by
VideoDescrambler::descramble
to generate the transform_plan
and the transform_map
(you will learn about both when it comes to descrambling).
To get the videos VideoInfo
, we actually need to request one more page. One you probably
don’t frequently visit as a ‘normal’ human being. Because we, programmers, are really
creative when it comes to naming stuff, a video’s VideoInfo
can be requested at
https://youtube.com/get_video_info. Btw.: If you want to see how the computer feels, when
we ask him to deserialize the response into the VideoInfo
struct, you can have a look
at https://www.youtube.com/get_video_info?video_id=5jlI4uzZGjU&eurl=https%3A%2F%2Fyoutube.com%2Fwatch%3Fiv%3D5jlI4uzZGjU&sts=
(most browsers, will download a text file!). This is the actual VideoInfo
for the
video with the id ‘5jlI4uzZGjU’.
That’s it! Of course, we cannot download the video yet. But that’s not the task of fetch
.
fetch
is just responsible for requesting all the essential information. To learn how the
journey continues, have a look at VideoDescrambler
.
Implementations
This is supported on crate features fetch
and regex
only.
fetch
and regex
only.Constructs a VideoFetcher
from an Url
.
Errors
- When
Id::from_raw
fails to extracted the videos id from the url. - When
reqwest
fails to initialize an newClient
.
This is supported on crate feature fetch
only.
fetch
only.This is supported on crate feature fetch
only.
fetch
only.Constructs a VideoFetcher
from an Id
and an existing Client
.
There are no special constrains, what the Client
has to look like.
It’s recommended to use the cookie jar returned from [recommended_cookies
].
It’s recommended to use the headers returned from [recommended_headers
].
This is supported on crate feature fetch
only.
fetch
only.Fetches all available video data and deserializes it into VideoInfo
.
Errors
- When the video is private, only for members, or otherwise not accessible.
- When requests to some video resources fail.
- When deserializing the raw response fails.
When having a good internet connection, only errors due to inaccessible videos should occur.
Other errors usually mean, that YouTube changed their API, and rustube
did not adapt to
this change yet. Please feel free to open a GitHub issue if this is the case.
This is supported on crate feature fetch
only.
fetch
only.Fetches all available video data, and deserializes it into VideoInfo
.
This method will only return the VideoInfo
. You won’t have the ability to download
the video afterwards. If you want to download videos, have a look at VideoFetcher::fetch
.
This method is useful if you want to find out something about a video that is not available for download, like live streams that are offline.
Errors
- When requests to some video resources fail.
- When deserializing the raw response fails.
When having a good internet connection, this method should not fail. Errors usually mean,
that YouTube changed their API, and rustube
did not adapt to this change yet. Please feel
free to open a GitHub issue if this is the case.
This is supported on crate feature fetch
only.
fetch
only.The id of the video.
Trait Implementations
fetch
only.fetch
only.fetch
only.fetch
only.fetch
only.Auto Trait Implementations
impl !RefUnwindSafe for VideoFetcher
impl Send for VideoFetcher
impl Sync for VideoFetcher
impl Unpin for VideoFetcher
impl !UnwindSafe for VideoFetcher
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more