1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
use std::sync::Arc;
use derive_more::Display;
use crate::{Id, Stream, VideoInfo};
use crate::video_info::player_response::video_details::VideoDetails;
/// A YouTube downloader, which allows you to download all available formats and qualities of a
/// YouTube video.
///
/// Each instance of [`Video`] represents an existing, available, and downloadable
/// video.
///
/// There are two ways of constructing an instance of [`Video`]:
/// 1. By using the asynchronous `Video::from_*` methods. These methods will take some kind of
/// video-identifier, like an [`Url`] or an [`Id`], will then internally download the necessary video
/// information and finally descramble it.
/// 2. By calling [`VideoDescrambler::descramble`]. Since a [`VideoDescrambler`] already
/// contains the necessary video information, and just need to descramble it, no requests are
/// performed. (This gives you more control over the process).
///
/// # Examples
/// - Constructing using [`Video::from_url`] (or [`Video::from_id`]) (easiest way)
/// ```no_run
///# use rustube::Video;
///# use url::Url;
///# #[tokio::main]
///# async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
/// let video: Video = Video::from_url(&url).await?;
///# Ok(())
///# }
/// ```
/// - Constructing using [`VideoDescrambler::descramble`]
/// ```no_run
///# use rustube::{Video, VideoFetcher, VideoDescrambler};
///# use url::Url;
///# #[tokio::main]
///# async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
/// let fetcher: VideoFetcher = VideoFetcher::from_url(&url)?;
/// let descrambler: VideoDescrambler = fetcher.fetch().await?;
/// let video: Video = descrambler.descramble()?;
///# Ok(())
///# }
/// ```
/// - Construction using chained calls
/// ```no_run
///# use rustube::{Video, VideoFetcher, VideoDescrambler};
///# use url::Url;
///# #[tokio::main]
///# async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
/// let video: Video = VideoFetcher::from_url(&url)?
/// .fetch()
/// .await?
/// .descramble()?;
///# Ok(())
///# }
/// ```
/// - Downloading a video using an existing [`Video`] instance
/// ```no_run
///# use rustube::{Video, VideoFetcher, VideoDescrambler};
///# use url::Url;
///# #[tokio::main]
///# async fn main() -> Result<(), Box<dyn std::error::Error>> {
///# let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
///# let video: Video = Video::from_url(&url).await?;
/// let video_path = video
/// .streams()
/// .iter()
/// .filter(|stream| stream.includes_video_track && stream.includes_audio_track)
/// .max_by_key(|stream| stream.quality_label)
/// .unwrap()
/// .download()
/// .await?;
///# Ok(())
///# }
/// ```
/// [`Url`]: url::Url
/// [`VideoDescrambler`]: crate::descrambler::VideoDescrambler
/// [`VideoDescrambler::descramble`]: crate::descrambler::VideoDescrambler::descramble
#[derive(Clone, Debug, Display, PartialEq)]
#[display(fmt =
"Video({}, streams: {})",
"video_info.player_response.video_details.video_id", "streams.len()"
)]
pub struct Video {
pub(crate) video_info: VideoInfo,
pub(crate) streams: Vec<Stream>,
}
impl Video {
/// Creates a [`Video`] from an [`Url`](url::Url).
/// ### Errors
/// - When [`VideoFetcher::from_url`](crate::VideoFetcher::from_url) fails.
/// - When [`VideoFetcher::fetch`](crate::VideoFetcher::fetch) fails.
/// - When [`VideoDescrambler::descramble`](crate::VideoDescrambler::descramble) fails.
#[inline]
#[cfg(all(feature = "download", feature = "regex"))]
pub async fn from_url(url: &url::Url) -> crate::Result<Self> {
crate::VideoFetcher::from_url(url)?
.fetch()
.await?
.descramble()
}
/// Creates a [`Video`] from an [`Id`].
/// ### Errors
/// - When [`VideoFetcher::fetch`](crate::VideoFetcher::fetch) fails.
/// - When [`VideoDescrambler::descramble`](crate::VideoDescrambler::descramble) fails.
#[inline]
#[cfg(feature = "download")]
pub async fn from_id(id: crate::IdBuf) -> crate::Result<Self> {
crate::VideoFetcher::from_id(id)?
.fetch()
.await?
.descramble()
}
/// The [`VideoInfo`] of the video.
#[inline]
pub fn video_info(&self) -> &VideoInfo {
&self.video_info
}
/// All [`Stream`]s of the video.
#[inline]
pub fn streams(&self) -> &Vec<Stream> {
&self.streams
}
/// Takes all [`Stream`]s of the video.
#[inline]
pub fn into_streams(self) -> Vec<Stream> {
self.streams
}
/// Decomposes a `Video` into it's raw parts.
#[inline]
pub fn into_parts(self) -> (VideoInfo, Vec<Stream>) {
(self.video_info, self.streams)
}
/// The [`VideoDetails`]s of the video.
#[inline]
pub fn video_details(&self) -> Arc<VideoDetails> {
Arc::clone(&self.video_info.player_response.video_details)
}
/// The [`Id`] of the video.
#[inline]
pub fn id(&self) -> Id<'_> {
self.video_info.player_response.video_details.video_id.as_borrowed()
}
/// The title of the video.
#[inline]
pub fn title(&self) -> &str {
self.video_info.player_response.video_details.title.as_str()
}
/// Whether or not the video is age restricted.
#[inline]
pub fn is_age_restricted(&self) -> bool {
self.video_info.is_age_restricted
}
/// The [`Stream`] with the best quality.
/// This stream is guaranteed to contain both a video as well as an audio track.
#[inline]
pub fn best_quality(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_video_track && stream.includes_audio_track)
.max_by_key(|stream| stream.quality_label)
}
/// The [`Stream`] with the worst quality.
/// This stream is guaranteed to contain both a video as well as an audio track.
#[inline]
pub fn worst_quality(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_video_track && stream.includes_audio_track)
.min_by_key(|stream| stream.quality_label)
}
/// The [`Stream`] with the best video quality.
/// This stream is guaranteed to contain only a video but no audio track.
#[inline]
pub fn best_video(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_video_track && !stream.includes_audio_track)
.max_by_key(|stream| stream.width)
}
/// The [`Stream`] with the worst video quality.
/// This stream is guaranteed to contain only a video but no audio track.
#[inline]
pub fn worst_video(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_video_track && !stream.includes_audio_track)
.min_by_key(|stream| stream.width)
}
/// The [`Stream`] with the best audio quality.
/// This stream is guaranteed to contain only a audio but no video track.
#[inline]
pub fn best_audio(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_audio_track && !stream.includes_video_track)
.max_by_key(|stream| stream.bitrate)
}
/// The [`Stream`] with the worst audio quality.
/// This stream is guaranteed to contain only a audio but no video track.
#[inline]
pub fn worst_audio(&self) -> Option<&Stream> {
self
.streams
.iter()
.filter(|stream| stream.includes_audio_track && !stream.includes_video_track)
.min_by_key(|stream| stream.bitrate)
}
}