@@ -60,6 +60,7 @@ export default class Youtube extends BaseVideo {
6060 await this . interceptCaption ( ) ; // start caption intercept
6161 this . loadCaption ( ) ; // turn on caption for embed video
6262 this . setPlayerCaption ( lang , tlang ) ; //turn on caption on specified lang
63+ await this . waitRandom ( 4000 , 5000 ) ;
6364 this . reloadCaption ( ) ; //reset previous caption immediately
6465 }
6566
@@ -109,26 +110,34 @@ export default class Youtube extends BaseVideo {
109110 if ( lang ) {
110111 subUrl = await this . getTranslatedSubtitleUrl ( subUrl , lang ) ;
111112 }
113+ var json ;
112114 try {
113115 var res = await fetch ( this . getTrafficSafeUrl ( subUrl ) ) ;
116+ json = await res . json ( ) ;
114117 } catch ( error ) {
115118 console . log ( error ) ;
116119 }
117120
118- // if fail, change base url and try again
119- if ( res ?. status != 200 ) {
120- this . isSubtitleRequestFailed = ! this . isSubtitleRequestFailed ;
121- res = await fetch ( this . getTrafficSafeUrl ( subUrl ) ) ;
121+ try {
122+ // if fail, change base url and try again
123+ if ( ! json ) {
124+ this . isSubtitleRequestFailed = ! this . isSubtitleRequestFailed ;
125+ const res = await fetch ( this . getTrafficSafeUrl ( subUrl ) ) ;
126+ json = await res . json ( ) ;
127+ }
128+ } catch ( error ) {
129+ console . error ( "Failed to fetch subtitle:" , error ) ;
122130 }
123- return await res . json ( ) ;
131+ return json ;
124132 }
125133 static getTrafficSafeUrl ( url ) {
126- return this . isSubtitleRequestFailed
127- ? url . replace (
128- `${ this . baseUrl } /api/timedtext` ,
129- "video.google.com/timedtext"
130- )
131- : url ;
134+ if ( this . isSubtitleRequestFailed ) {
135+ const urlObj = new URL ( url ) ;
136+ urlObj . hostname = "video.google.com" ;
137+ urlObj . pathname = "/timedtext" ;
138+ return urlObj . toString ( ) ;
139+ }
140+ return url ;
132141 }
133142
134143 static async getTranslatedSubtitleUrl ( subUrl , lang ) {
@@ -156,6 +165,9 @@ export default class Youtube extends BaseVideo {
156165
157166 // concat sub=====================================
158167 static parseSubtitle ( subtitle , lang ) {
168+ if ( ! subtitle ?. events ) {
169+ return { events : [ ] , pens : [ { } ] , wireMagic : "pb3" , wpWinPositions : [ { } ] , wsWinStyles : [ { } ] } ;
170+ }
159171 var newEvents = [ ] ;
160172 for ( var event of subtitle . events ) {
161173 if ( ! event . segs || ! event . dDurationMs ) {
0 commit comments