tydle 0.1.15

YouTube video extractor written in Rust that can be used anywhere in web or native environments, based on an extremely small subset of yt-dlp.
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use anyhow::Result;
use maplit::hashmap;
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;

use crate::{
    extractor::token_policy::{
        GvsPoTokenPolicy, PlayerPoTokenPolicy, StreamingProtocol, SubsPoTokenPolicy,
        WEB_PO_TOKEN_POLICIES, create_default_gvs_po_token_policy,
    },
    yt_interface::{PREFERRED_LOCALE, YtClient},
};

#[derive(Debug, Clone, Serialize)]
pub struct InnerTubeClient {
    #[serde(rename = "INNERTUBE_CONTEXT")]
    pub innertube_context: HashMap<&'static str, HashMap<&'static str, Value>>,
    #[serde(rename = "INNERTUBE_HOST")]
    pub innertube_host: &'static str,
    #[serde(rename = "INNERTUBE_CONTEXT_CLIENT_NAME")]
    pub innertube_context_client_name: i32,
    #[serde(rename = "SUPPORTS_COOKIES")]
    pub supports_cookies: bool,
    #[serde(rename = "REQUIRE_JS_PLAYER")]
    pub require_js_player: bool,
    #[serde(rename = "REQUIRE_AUTH")]
    pub require_auth: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authenticated_user_agent: Option<&'static str>,
    #[serde(rename = "GVS_PO_TOKEN_POLICY")]
    pub gvs_po_token_policy: HashMap<StreamingProtocol, GvsPoTokenPolicy>,
    #[serde(rename = "PLAYER_PO_TOKEN_POLICY")]
    pub player_po_token_policy: PlayerPoTokenPolicy,
    #[serde(rename = "SUBS_PO_TOKEN_POLICY")]
    pub subs_po_token_policy: SubsPoTokenPolicy,
    #[serde(skip_serializing)]
    pub priority: isize,
}

impl InnerTubeClient {
    pub fn to_json_val_hashmap(&self) -> Result<HashMap<String, Value>> {
        let serialized = serde_json::to_value(self)?;

        if let Value::Object(obj) = serialized {
            let mut hashmap = HashMap::new();
            for (k, v) in obj {
                hashmap.insert(k, v);
            }

            return Ok(hashmap);
        }

        Ok(HashMap::new())
    }
}

pub static INNERTUBE_CLIENTS: Lazy<HashMap<YtClient, InnerTubeClient>> = Lazy::new(|| {
    const DEFAULT_INNERTUBE_HOST: &str = "www.youtube.com";
    const BASE_CLIENTS: &[&str; 5] = &["android", "mweb", "tv", "web", "ios"];
    let base_client_indices: HashMap<&str, usize> = BASE_CLIENTS
        .iter()
        .enumerate()
        .map(|(i, &name)| (name, i))
        .collect();

    let mut m = HashMap::new();

    let web_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "WEB".into(),
            "clientVersion" => "2.20260114.08.00".into(),
            "hl" => PREFERRED_LOCALE.into()
        }
    };

    m.insert(
        YtClient::Web,
        InnerTubeClient {
            priority: 0,
            innertube_context: web_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 1,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: WEB_PO_TOKEN_POLICIES.player_po_token_policy,
            subs_po_token_policy: WEB_PO_TOKEN_POLICIES.subs_po_token_policy,
        },
    );

    let web_safari_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "WEB".into(),
            "clientVersion" => "2.20260114.08.00".into(),
            "userAgent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15,gzip(gfe)".into(),
            "hl" => PREFERRED_LOCALE.into()
        },
    };

    m.insert(
        YtClient::WebSafari,
        InnerTubeClient {
            priority: 0,
            innertube_context: web_safari_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 1,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: WEB_PO_TOKEN_POLICIES.player_po_token_policy,
            subs_po_token_policy: WEB_PO_TOKEN_POLICIES.subs_po_token_policy,
        },
    );

    let web_embedded_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "WEB_EMBEDDED_PLAYER".into(),
            "clientVersion" => "1.20260115.01.00".into(),
            "hl" => PREFERRED_LOCALE.into()
        }
    };

    m.insert(
        YtClient::WebEmbedded,
        InnerTubeClient {
            priority: 0,
            innertube_context: web_embedded_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 56,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: WEB_PO_TOKEN_POLICIES.player_po_token_policy,
            subs_po_token_policy: WEB_PO_TOKEN_POLICIES.subs_po_token_policy,
        },
    );

    let web_music_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "WEB_REMIX".into(),
            "clientVersion" => "1.20260115.01.00".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::WebMusic,
        InnerTubeClient {
            priority: 0,
            innertube_context: web_music_context,
            innertube_host: "music.youtube.com",
            innertube_context_client_name: 67,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let web_creator_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "WEB_CREATOR".into(),
            "clientVersion" => "1.20260114.05.00".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::WebCreator,
        InnerTubeClient {
            priority: 0,
            innertube_context: web_creator_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 62,
            supports_cookies: true,
            require_js_player: true,
            require_auth: true,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let android_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "ANDROID".into(),
            "clientVersion" => "21.02.35".into(),
            "androidSdkVersion" => 30.into(),
            "userAgent" => "com.google.android.youtube/21.02.35 (Linux; U; Android 11) gzip".into(),
            "osName" => "Android".into(),
            "osVersion" => "11".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    let android_gvs_po_token_policy = hashmap! {
        StreamingProtocol::Https => GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        },
        StreamingProtocol::Dash => GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        },
        StreamingProtocol::Hls =>  GvsPoTokenPolicy {
            required: false,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        }
    };

    m.insert(
        YtClient::Android,
        InnerTubeClient {
            priority: 0,
            innertube_context: android_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 3,
            supports_cookies: false,
            require_js_player: false,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: android_gvs_po_token_policy,
            player_po_token_policy: PlayerPoTokenPolicy {
                required: false,
                recommended: true,
                not_required_for_premium: false,
            },
            subs_po_token_policy: Default::default(),
        },
    );

    let android_sdkless_context = hashmap! {
        "client" => hashmap!  {
            "clientName" => "ANDROID".into(),
            "clientVersion" => "21.02.35".into(),
            "userAgent" => "com.google.android.youtube/21.02.35 (Linux; U; Android 11) gzip".into(),
            "osName" =>"Android".into(),
            "osVersion" => "11".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::AndroidSdkless,
        InnerTubeClient {
            priority: 0,
            innertube_context: android_sdkless_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 3,
            supports_cookies: false,
            require_js_player: false,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: create_default_gvs_po_token_policy(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let android_vr_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "ANDROID_VR".into(),
            "clientVersion" => "1.65.10".into(),
            "deviceMake" => "Oculus".into(),
            "deviceModel" => "Quest 3".into(),
            "androidSdkVersion" => 32.into(),
            "userAgent" => "com.google.android.apps.youtube.vr.oculus/1.65.10 (Linux; U; Android 12L; eureka-user Build/SQ3A.220605.009.A1) gzip".into(),
            "osName" => "Android".into(),
            "osVersion" => "12L".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::AndroidVr,
        InnerTubeClient {
            priority: 0,
            innertube_context: android_vr_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 28,
            supports_cookies: false,
            require_js_player: false,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: create_default_gvs_po_token_policy(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let ios_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "IOS".into(),
            "clientVersion" => "21.02.3".into(),
            "deviceMake" => "Apple".into(),
            "deviceModel" => "iPhone16,2".into(),
            "userAgent" => "com.google.ios.youtube/21.02.3 (iPhone16,2; U; CPU iOS 18_3_2 like Mac OS X;)".into(),
            "osName" => "iPhone".into(),
            "osVersion" => "18.3.2.22D82".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };
    let ios_gvs_po_token_policy = hashmap! {
        StreamingProtocol::Https =>GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        },
        // HLS Livestreams require POT 30 seconds in.
        StreamingProtocol::Hls => GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        }
    };

    m.insert(
        YtClient::IOS,
        InnerTubeClient {
            priority: 0,
            innertube_context: ios_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 5,
            supports_cookies: false,
            require_js_player: false,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: ios_gvs_po_token_policy,
            player_po_token_policy: PlayerPoTokenPolicy {
                required: false,
                recommended: true,
                not_required_for_premium: false,
            },
            subs_po_token_policy: Default::default(),
        },
    );

    let ios_downgraded_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "IOS".into(),
            "clientVersion" => "19.49.7".into(),
            "deviceMake" => "Apple".into(),
            "deviceModel" => "iPhone16,2".into(),
            "userAgent" => "com.google.ios.youtube/19.49.7 (iPhone16,2; U; CPU iOS 17_5_1 like Mac OS X;)".into(),
            "osName" => "iPhone".into(),
            "osVersion" => "17.5.1.21F90".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };
    let ios_downgraded_gvs_po_token_policy = hashmap! {
        StreamingProtocol::Https =>GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        },
        // HLS Livestreams require POT 30 seconds in.
        StreamingProtocol::Hls => GvsPoTokenPolicy {
            required: false,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: true,
        }
    };

    m.insert(
        YtClient::IOSDowngraded,
        InnerTubeClient {
            priority: 0,
            innertube_context: ios_downgraded_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 5,
            supports_cookies: false,
            require_js_player: false,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: ios_downgraded_gvs_po_token_policy,
            player_po_token_policy: PlayerPoTokenPolicy {
                required: false,
                recommended: true,
                not_required_for_premium: false,
            },
            subs_po_token_policy: Default::default(),
        },
    );

    let mweb_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "MWEB".into(),
            "clientVersion" => "2.20260115.01.00".into(),
            "userAgent" => "Mozilla/5.0 (iPad; CPU OS 16_7_10 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1,gzip(gfe)".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::MWeb,
        InnerTubeClient {
            priority: 0,
            innertube_context: mweb_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 2,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let tv_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "TVHTML5".into(),
            "clientVersion" => "7.20260114.12.00".into(),
            "userAgent" => "Mozilla/5.0 (ChromiumStylePlatform) Cobalt/25.lts.30.1034943-gold (unlike Gecko), Unknown_TV_Unknown_0/Unknown (Unknown, Unknown)".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::Tv,
        InnerTubeClient {
            priority: 0,
            innertube_context: tv_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 7,
            supports_cookies: true,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let tv_downgraded_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "TVHTML5".into(),
            "clientVersion" => "5.20260114".into(),
            "userAgent" => "Mozilla/5.0 (ChromiumStylePlatform) Cobalt/Version".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::TvDowngraded,
        InnerTubeClient {
            priority: 0,
            innertube_context: tv_downgraded_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 7,
            supports_cookies: true,
            require_js_player: true,
            require_auth: true,
            authenticated_user_agent: None,
            gvs_po_token_policy: WEB_PO_TOKEN_POLICIES.gvs_po_token_policy.clone(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let tv_simply_context = hashmap! {
        "client" =>  hashmap! {
            "clientName" => "TVHTML5_SIMPLY".into(),
            "clientVersion" => "1.0".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };
    let tv_simply_gvs_po_token_policy = hashmap! {
        StreamingProtocol::Https => GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: false,
        },
        StreamingProtocol::Dash => GvsPoTokenPolicy {
            required: true,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: false,
        },
        StreamingProtocol::Hls => GvsPoTokenPolicy {
            required: false,
            recommended: true,
            not_required_for_premium: false,
            not_required_with_player_token: false,
        }
    };

    m.insert(
        YtClient::TvSimply,
        InnerTubeClient {
            priority: 0,
            innertube_context: tv_simply_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 75,
            supports_cookies: false,
            require_js_player: true,
            require_auth: false,
            authenticated_user_agent: None,
            gvs_po_token_policy: tv_simply_gvs_po_token_policy,
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let tv_embedded_context = hashmap! {
        "client" => hashmap! {
            "clientName" => "TVHTML5_SIMPLY_EMBEDDED_PLAYER".into(),
            "clientVersion" => "2.0".into(),
            "hl" => PREFERRED_LOCALE.into(),
        }
    };

    m.insert(
        YtClient::TvEmbedded,
        InnerTubeClient {
            priority: 0,
            innertube_context: tv_embedded_context,
            innertube_host: DEFAULT_INNERTUBE_HOST,
            innertube_context_client_name: 85,
            supports_cookies: true,
            require_js_player: true,
            require_auth: true,
            authenticated_user_agent: None,
            gvs_po_token_policy: create_default_gvs_po_token_policy(),
            player_po_token_policy: Default::default(),
            subs_po_token_policy: Default::default(),
        },
    );

    let third_party: HashMap<&str, Value> = hashmap! {
        // Can be any valid URL.
        "embedUrl" => "https://www.youtube.com/".into(),
    };

    for (yt_client, ytcfg) in &mut m {
        let client_base_name = yt_client.get_base();
        let priority_index = 10
            * base_client_indices
                .get(client_base_name)
                .map(|&i| i as isize)
                .unwrap_or(-1);

        if yt_client.get_variant() == "embedded" {
            ytcfg
                .innertube_context
                .insert("thirdParty", third_party.clone());
            ytcfg.priority = priority_index - 2;
        } else {
            ytcfg.priority = priority_index - 3;
        }
    }

    m
});