@@ -44,46 +44,62 @@ export async function transcribeYouTube(url: string, language: string): Promise<
4444 } ;
4545}
4646
47+ const PLAYER_ENDPOINTS = [
48+ 'https://youtubei.googleapis.com/youtubei/v1/player?prettyPrint=false' ,
49+ 'https://www.youtube.com/youtubei/v1/player?prettyPrint=false' ,
50+ ] ;
51+
4752async function fetchIosPlayer ( videoId : string ) : Promise < any > {
48- const resp = await fetch ( 'https://www.youtube.com/youtubei/v1/player?prettyPrint=false' , {
49- method : 'POST' ,
50- headers : {
51- 'Content-Type' : 'application/json' ,
52- 'User-Agent' : IOS_UA ,
53- 'X-YouTube-Client-Name' : '5' ,
54- } ,
55- body : JSON . stringify ( {
56- context : {
57- client : {
58- clientName : 'IOS' ,
59- clientVersion : '21.02.3' ,
60- deviceMake : 'Apple' ,
61- deviceModel : 'iPhone16,2' ,
62- osName : 'iPhone' ,
63- osVersion : '18.3.2.22D82' ,
64- hl : 'en' ,
65- gl : 'US' ,
66- } ,
53+ const body = JSON . stringify ( {
54+ context : {
55+ client : {
56+ clientName : 'IOS' ,
57+ clientVersion : '21.02.3' ,
58+ deviceMake : 'Apple' ,
59+ deviceModel : 'iPhone16,2' ,
60+ osName : 'iPhone' ,
61+ osVersion : '18.3.2.22D82' ,
62+ hl : 'en' ,
63+ gl : 'US' ,
6764 } ,
68- videoId ,
69- contentCheckOk : true ,
70- racyCheckOk : true ,
71- } ) ,
65+ } ,
66+ videoId ,
67+ contentCheckOk : true ,
68+ racyCheckOk : true ,
7269 } ) ;
7370
74- if ( ! resp . ok ) {
75- throw new TranscribeError ( `YouTube player request failed (${ resp . status } ).` , 502 ) ;
71+ let lastError = 'YouTube player request failed.' ;
72+ for ( const endpoint of PLAYER_ENDPOINTS ) {
73+ try {
74+ const resp = await fetch ( endpoint , {
75+ method : 'POST' ,
76+ headers : {
77+ 'Content-Type' : 'application/json' ,
78+ 'User-Agent' : IOS_UA ,
79+ 'X-YouTube-Client-Name' : '5' ,
80+ } ,
81+ body,
82+ } ) ;
83+ if ( ! resp . ok ) {
84+ lastError = `YouTube player request failed (${ resp . status } ).` ;
85+ continue ;
86+ }
87+ const data = await resp . json ( ) ;
88+ const status = data ?. playabilityStatus ?. status ;
89+ const tracks =
90+ data ?. captions ?. playerCaptionsTracklistRenderer ?. captionTracks || [ ] ;
91+ if ( status === 'OK' && tracks . length > 0 ) {
92+ return data ;
93+ }
94+ lastError =
95+ data ?. playabilityStatus ?. reason ||
96+ `YouTube rejected playback (${ status || 'unknown' } ).` ;
97+ } catch ( err : any ) {
98+ lastError = err ?. message || lastError ;
99+ }
76100 }
77101
78- const data = await resp . json ( ) ;
79- const status = data ?. playabilityStatus ?. status ;
80- if ( status && status !== 'OK' ) {
81- throw new TranscribeError (
82- data ?. playabilityStatus ?. reason || `YouTube rejected playback (${ status } ).` ,
83- 502
84- ) ;
85- }
86- return data ;
102+ throw new TranscribeError ( lastError , 502 ) ;
87103}
88104
89105function getCaptionTracks ( player : any ) : CaptionTrack [ ] {
0 commit comments