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
mod audio;
mod common;
mod video;
pub use crate::youtube::player_response::Quality;
pub use audio::Stream as AudioStream;
pub use common::Stream as CommonStream;
pub use video::Stream as VideoStream;
use crate::{
youtube::{
player_response::{FormatType, PlayabilityErrorCode, StreamingData},
video_info::VideoInfo,
},
Client,
};
use std::sync::Arc;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Unable to get content-length")]
UnknownContentLength,
#[error("{code:?}: '{reason:?}'")]
Unplayable {
code: PlayabilityErrorCode,
reason: Option<String>,
},
}
pub(crate) async fn get(
client: Arc<Client>,
id: crate::video::Id,
streaming_data: Option<StreamingData>,
) -> crate::Result<impl Iterator<Item = Stream>> {
let streaming_data = if let Some(streaming_data) = streaming_data {
Ok(streaming_data)
} else {
let video_info = VideoInfo::from_id(&client.client, id).await?;
let player_response = video_info.player_response();
if player_response
.playability_status
.status
.is_stream_recoverable()
{
Ok(player_response
.streaming_data
.expect("Recoverable error did not contain streaming data"))
} else {
Err(Error::Unplayable {
code: player_response.playability_status.status,
reason: player_response.playability_status.reason,
})
}
}?;
Ok(streaming_data
.adaptive_formats
.into_iter()
.map(move |stream| Stream::new(stream, Arc::clone(&client))))
}
#[derive(Debug)]
pub enum Stream {
Audio(AudioStream),
Video(VideoStream),
}
impl Stream {
pub(crate) fn new(
format: crate::youtube::player_response::Format,
client: Arc<Client>,
) -> Self {
match format.ty {
FormatType::Audio(audio) => Self::Audio(AudioStream {
common: CommonStream {
format: format.base,
client,
},
audio,
}),
FormatType::Video(video) => Self::Video(VideoStream {
common: CommonStream {
format: format.base,
client,
},
video,
}),
}
}
pub fn is_audio(&self) -> bool {
matches!(self, Self::Audio(..))
}
pub fn is_video(&self) -> bool {
matches!(self, Self::Video(..))
}
}
impl std::ops::Deref for Stream {
type Target = CommonStream;
fn deref(&self) -> &Self::Target {
match self {
Stream::Audio(audio) => &audio.common,
Stream::Video(video) => &video.common,
}
}
}
#[cfg(test)]
mod test {
#[async_std::test]
async fn get() -> Result<(), Box<dyn std::error::Error>> {
let client = crate::Client::new().await?;
let mut streams = client
.streams("https://www.youtube.com/watch?v=7B2PIVSWtJA".parse()?)
.await?;
assert!(streams.next().is_some());
Ok(())
}
#[async_std::test]
async fn age_restricted() -> Result<(), Box<dyn std::error::Error>> {
let client = crate::Client::new().await?;
let streams = client
.streams("https://www.youtube.com/watch?v=9Jg_Fwc0QOY".parse()?)
.await?;
for stream in streams {
stream.url().await?;
}
Ok(())
}
}