youtube-transcript-plus 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -7
- package/dist/cache/fs-cache.d.ts +18 -0
- package/dist/cache/in-memory-cache.d.ts +14 -0
- package/dist/errors.d.ts +14 -0
- package/dist/formatters.d.ts +57 -0
- package/dist/index.d.ts +141 -10
- package/dist/types.d.ts +125 -1
- package/dist/utils.d.ts +20 -0
- package/dist/youtube-transcript-plus.cjs +732 -0
- package/dist/youtube-transcript-plus.js +19 -16
- package/dist/youtube-transcript-plus.mjs +716 -0
- package/package.json +26 -15
package/dist/types.d.ts
CHANGED
|
@@ -1,28 +1,152 @@
|
|
|
1
|
+
/** Interface for implementing custom caching strategies. */
|
|
1
2
|
export interface CacheStrategy {
|
|
3
|
+
/** Retrieve a cached value by key. Returns `null` if not found or expired. */
|
|
2
4
|
get(key: string): Promise<string | null>;
|
|
5
|
+
/** Store a value in the cache with an optional TTL in milliseconds. */
|
|
3
6
|
set(key: string, value: string, ttl?: number): Promise<void>;
|
|
4
7
|
}
|
|
8
|
+
/** Parameters passed to custom fetch functions (`videoFetch`, `playerFetch`, `transcriptFetch`). */
|
|
5
9
|
export interface FetchParams {
|
|
10
|
+
/** The URL to fetch. */
|
|
6
11
|
url: string;
|
|
12
|
+
/** The requested language code (e.g., `'en'`, `'fr'`). */
|
|
7
13
|
lang?: string;
|
|
14
|
+
/** The User-Agent string for the request. */
|
|
8
15
|
userAgent?: string;
|
|
16
|
+
/** HTTP method — `'GET'` for watch/transcript pages, `'POST'` for the Innertube player endpoint. */
|
|
9
17
|
method?: 'GET' | 'POST';
|
|
18
|
+
/** Request body (only present for POST requests). */
|
|
10
19
|
body?: string;
|
|
20
|
+
/** Additional HTTP headers (e.g., `Content-Type` for the player endpoint). */
|
|
11
21
|
headers?: Record<string, string>;
|
|
22
|
+
/** AbortSignal to cancel the request. */
|
|
23
|
+
signal?: AbortSignal;
|
|
12
24
|
}
|
|
25
|
+
/** Configuration options for fetching transcripts. */
|
|
13
26
|
export interface TranscriptConfig {
|
|
27
|
+
/** BCP 47 language code for the transcript (e.g., `'en'`, `'fr'`). */
|
|
14
28
|
lang?: string;
|
|
29
|
+
/** Custom User-Agent string for HTTP requests. */
|
|
15
30
|
userAgent?: string;
|
|
31
|
+
/** Custom caching strategy implementing the {@link CacheStrategy} interface. */
|
|
16
32
|
cache?: CacheStrategy;
|
|
33
|
+
/** Time-to-live for cache entries in milliseconds. Defaults to 1 hour. */
|
|
17
34
|
cacheTTL?: number;
|
|
35
|
+
/** Use HTTP instead of HTTPS for YouTube requests. Not recommended for production. */
|
|
18
36
|
disableHttps?: boolean;
|
|
37
|
+
/** Custom fetch function for the YouTube watch page (GET request). */
|
|
19
38
|
videoFetch?: (params: FetchParams) => Promise<Response>;
|
|
39
|
+
/** Custom fetch function for the transcript XML data (GET request). */
|
|
20
40
|
transcriptFetch?: (params: FetchParams) => Promise<Response>;
|
|
41
|
+
/** Custom fetch function for the YouTube Innertube player API (POST request). */
|
|
21
42
|
playerFetch?: (params: FetchParams) => Promise<Response>;
|
|
43
|
+
/** Maximum number of retry attempts for transient failures (429, 5xx). Defaults to `0` (no retries). */
|
|
44
|
+
retries?: number;
|
|
45
|
+
/** Base delay in milliseconds for exponential backoff between retries. Defaults to `1000`. */
|
|
46
|
+
retryDelay?: number;
|
|
47
|
+
/** AbortSignal to cancel in-flight requests. */
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
/** When `true`, return a {@link TranscriptResult} containing both video details and segments. */
|
|
50
|
+
videoDetails?: boolean;
|
|
22
51
|
}
|
|
23
|
-
|
|
52
|
+
/** A single transcript segment returned by {@link fetchTranscript}. */
|
|
53
|
+
export interface TranscriptSegment {
|
|
54
|
+
/** The text content of the transcript segment. */
|
|
24
55
|
text: string;
|
|
56
|
+
/** Duration of the segment in seconds. */
|
|
25
57
|
duration: number;
|
|
58
|
+
/** Start time of the segment in seconds from the beginning of the video. */
|
|
26
59
|
offset: number;
|
|
60
|
+
/** The language code of the transcript. */
|
|
27
61
|
lang: string;
|
|
28
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* @deprecated Use {@link TranscriptSegment} instead.
|
|
65
|
+
*/
|
|
66
|
+
export type TranscriptResponse = TranscriptSegment;
|
|
67
|
+
/** A video thumbnail image. */
|
|
68
|
+
export interface Thumbnail {
|
|
69
|
+
/** URL of the thumbnail image. */
|
|
70
|
+
url: string;
|
|
71
|
+
/** Width of the thumbnail in pixels. */
|
|
72
|
+
width: number;
|
|
73
|
+
/** Height of the thumbnail in pixels. */
|
|
74
|
+
height: number;
|
|
75
|
+
}
|
|
76
|
+
/** Metadata about a YouTube video, extracted from the Innertube player response. */
|
|
77
|
+
export interface VideoDetails {
|
|
78
|
+
/** The video's unique ID. */
|
|
79
|
+
videoId: string;
|
|
80
|
+
/** Video title. */
|
|
81
|
+
title: string;
|
|
82
|
+
/** Channel/uploader name. */
|
|
83
|
+
author: string;
|
|
84
|
+
/** Uploader's channel ID. */
|
|
85
|
+
channelId: string;
|
|
86
|
+
/** Duration in seconds. */
|
|
87
|
+
lengthSeconds: number;
|
|
88
|
+
/** Number of views. */
|
|
89
|
+
viewCount: number;
|
|
90
|
+
/** Video description. */
|
|
91
|
+
description: string;
|
|
92
|
+
/** Array of keyword tags. */
|
|
93
|
+
keywords: string[];
|
|
94
|
+
/** Array of thumbnail images at various resolutions. */
|
|
95
|
+
thumbnails: Thumbnail[];
|
|
96
|
+
/** Whether the video is live content. */
|
|
97
|
+
isLiveContent: boolean;
|
|
98
|
+
}
|
|
99
|
+
/** Result returned by {@link fetchTranscript} when `videoDetails: true` is set. */
|
|
100
|
+
export interface TranscriptResult {
|
|
101
|
+
/** Metadata about the video. */
|
|
102
|
+
videoDetails: VideoDetails;
|
|
103
|
+
/** Array of transcript segments. */
|
|
104
|
+
segments: TranscriptSegment[];
|
|
105
|
+
}
|
|
106
|
+
/** Shape of a single caption track from the Innertube player response. */
|
|
107
|
+
export interface CaptionTrack {
|
|
108
|
+
baseUrl?: string;
|
|
109
|
+
url?: string;
|
|
110
|
+
languageCode: string;
|
|
111
|
+
name?: {
|
|
112
|
+
simpleText?: string;
|
|
113
|
+
};
|
|
114
|
+
kind?: string;
|
|
115
|
+
}
|
|
116
|
+
/** Information about an available caption track for a video. */
|
|
117
|
+
export interface CaptionTrackInfo {
|
|
118
|
+
/** BCP 47 language code (e.g., `'en'`, `'fr'`, `'pt-BR'`). */
|
|
119
|
+
languageCode: string;
|
|
120
|
+
/** Human-readable language name (e.g., `'English'`, `'French'`). */
|
|
121
|
+
languageName: string;
|
|
122
|
+
/** Whether the captions are auto-generated by YouTube's speech recognition. */
|
|
123
|
+
isAutoGenerated: boolean;
|
|
124
|
+
}
|
|
125
|
+
/** Shape of the Innertube player JSON response (relevant subset). */
|
|
126
|
+
export interface InnertubePlayerResponse {
|
|
127
|
+
playabilityStatus?: {
|
|
128
|
+
status?: string;
|
|
129
|
+
};
|
|
130
|
+
videoDetails?: {
|
|
131
|
+
videoId?: string;
|
|
132
|
+
title?: string;
|
|
133
|
+
author?: string;
|
|
134
|
+
channelId?: string;
|
|
135
|
+
lengthSeconds?: string;
|
|
136
|
+
viewCount?: string;
|
|
137
|
+
shortDescription?: string;
|
|
138
|
+
keywords?: string[];
|
|
139
|
+
thumbnail?: {
|
|
140
|
+
thumbnails?: Thumbnail[];
|
|
141
|
+
};
|
|
142
|
+
isLiveContent?: boolean;
|
|
143
|
+
};
|
|
144
|
+
captions?: {
|
|
145
|
+
playerCaptionsTracklistRenderer?: {
|
|
146
|
+
captionTracks?: CaptionTrack[];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
playerCaptionsTracklistRenderer?: {
|
|
150
|
+
captionTracks?: CaptionTrack[];
|
|
151
|
+
};
|
|
152
|
+
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { FetchParams } from './types';
|
|
2
2
|
export declare function decodeXmlEntities(text: string): string;
|
|
3
3
|
export declare function retrieveVideoId(videoId: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* Validate that a language code matches a BCP 47-like pattern.
|
|
6
|
+
* @throws {@link YoutubeTranscriptInvalidLangError} if the language code is invalid.
|
|
7
|
+
*/
|
|
8
|
+
export declare function validateLang(lang: string): void;
|
|
4
9
|
export declare function defaultFetch(params: FetchParams): Promise<Response>;
|
|
10
|
+
/** Returns true if the HTTP status code is retryable (429 or 5xx). */
|
|
11
|
+
export declare function isRetryableStatus(status: number): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Wrap a fetch call with retry logic using exponential backoff.
|
|
14
|
+
*
|
|
15
|
+
* Retries on 429 (Too Many Requests) and 5xx (Server Errors).
|
|
16
|
+
* Client errors (4xx other than 429) are returned immediately.
|
|
17
|
+
*
|
|
18
|
+
* @param fetchFn - Function that performs the fetch call.
|
|
19
|
+
* @param retries - Maximum number of retry attempts (0 = no retries).
|
|
20
|
+
* @param retryDelay - Base delay in milliseconds for exponential backoff.
|
|
21
|
+
* @param signal - Optional AbortSignal to cancel the operation.
|
|
22
|
+
* @returns The fetch Response.
|
|
23
|
+
*/
|
|
24
|
+
export declare function fetchWithRetry(fetchFn: () => Promise<Response>, retries: number, retryDelay: number, signal?: AbortSignal): Promise<Response>;
|