@@ -68,8 +68,9 @@ interface YouTubeConfig {
6868export class YouTubeTranscriptExtractor {
6969 private static readonly USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' ;
7070 private static cookieStore : string = '' ;
71- // Cache YouTube config after first extraction to avoid repeated HTML fetches
71+ // Cache YouTube config after first extraction to avoid repeated HTML fetches within the same video request
7272 private static cachedConfig : YouTubeConfig | null = null ;
73+ private static cachedVideoId : string | null = null ;
7374 // Fallback client version if extraction fails (updated to current version)
7475 private static readonly FALLBACK_CLIENT_VERSION = '2.20260128.05.00' ;
7576
@@ -100,6 +101,13 @@ export class YouTubeTranscriptExtractor {
100101 static async fetchTranscript ( videoId : string , options : TranscriptOptions = { } ) : Promise < TranscriptResult > {
101102 transcriptLogger . debug ( `Fetching YouTube transcript for video: ${ videoId } ` ) ;
102103
104+ // Clear cached config when video changes to prevent stale title/metadata from a previous request
105+ if ( this . cachedVideoId !== videoId ) {
106+ this . cachedConfig = null ;
107+ this . cachedVideoId = videoId ;
108+ transcriptLogger . debug ( `New video ID detected (${ videoId } ), cleared config cache` ) ;
109+ }
110+
103111 try {
104112 // Always try local methods first, then fall back to paid APIs
105113 const attempts : Array < { method : string ; error : string } > = [ ] ;
@@ -612,6 +620,7 @@ export class YouTubeTranscriptExtractor {
612620 */
613621 static clearConfigCache ( ) : void {
614622 this . cachedConfig = null ;
623+ this . cachedVideoId = null ;
615624 transcriptLogger . debug ( 'YouTube config cache cleared' ) ;
616625 }
617626
@@ -1254,8 +1263,8 @@ export class YouTubeTranscriptExtractor {
12541263 } ) ) . filter ( segment => ! ! segment . text ) ;
12551264
12561265 transcriptLogger . debug ( `Supadata: successfully extracted ${ segments . length } segments` ) ;
1257- const metadata : TranscriptMetadata = this . cachedConfig ?. metadata ?? { } ;
1258- transcriptLogger . debug ( `Supadata: using cached metadata — title="${ metadata . title } ", author="${ metadata . author } "` ) ;
1266+ const metadata : TranscriptMetadata = await this . getVideoMetadata ( videoId ) ;
1267+ transcriptLogger . debug ( `Supadata: fetched metadata — title="${ metadata . title } ", author="${ metadata . author } "` ) ;
12591268 return { segments, metadata } ;
12601269 }
12611270
@@ -1310,8 +1319,8 @@ export class YouTubeTranscriptExtractor {
13101319 } ) ) . filter ( segment => ! ! segment . text ) ;
13111320
13121321 transcriptLogger . debug ( `ScrapeCreators: successfully extracted ${ segments . length } segments` ) ;
1313- const metadata : TranscriptMetadata = this . cachedConfig ?. metadata ?? { } ;
1314- transcriptLogger . debug ( `ScrapeCreators: using cached metadata — title="${ metadata . title } ", author="${ metadata . author } "` ) ;
1322+ const metadata : TranscriptMetadata = await this . getVideoMetadata ( videoId ) ;
1323+ transcriptLogger . debug ( `ScrapeCreators: fetched metadata — title="${ metadata . title } ", author="${ metadata . author } "` ) ;
13151324 return { segments, metadata } ;
13161325 }
13171326
0 commit comments