@@ -91,9 +91,45 @@ public static String resolveResolutionString(int itag) {
9191 }
9292 }
9393
94- private String decryptionCode = "" ;
94+
95+ // static values
9596 private static final String DECRYPTION_FUNC_NAME ="decrypt" ;
9697
98+ // cached values
99+ private static volatile String decryptionCode = "" ;
100+
101+ public void initService (String site ) {
102+ // The Youtube service needs to be initialized by downloading the
103+ // js-Youtube-player. This is done in order to get the algorithm
104+ // for decrypting cryptic signatures inside certain stream urls.
105+
106+ // Star Wars Kid is used as a dummy video, in order to download the youtube player.
107+ //String site = Downloader.download("https://www.youtube.com/watch?v=HPPj6viIBmU");
108+ //-------------------------------------
109+ // extracting form player args
110+ //-------------------------------------
111+ try {
112+ String jsonString = matchGroup1 ("ytplayer.config\\ s*=\\ s*(\\ {.*?\\ });" , site );
113+ JSONObject jsonObj = new JSONObject (jsonString );
114+
115+ //----------------------------------
116+ // load an parse description code
117+ //----------------------------------
118+ if (decryptionCode .isEmpty ()) {
119+ JSONObject ytAssets = jsonObj .getJSONObject ("assets" );
120+ String playerUrl = ytAssets .getString ("js" );
121+ if (playerUrl .startsWith ("//" )) {
122+ playerUrl = "https:" + playerUrl ;
123+ }
124+ decryptionCode = loadDecryptionCode (playerUrl );
125+ }
126+
127+ } catch (Exception e ){
128+ Log .d (TAG , "Could not initialize the extractor of the Youtube service." );
129+ e .printStackTrace ();
130+ }
131+ }
132+
97133 @ Override
98134 public String getVideoId (String videoUrl ) {
99135 try {
@@ -147,18 +183,18 @@ public VideoInfo getVideoInfo(String siteUrl) {
147183 videoInfo .age_limit = 0 ;
148184 videoInfo .webpage_url = siteUrl ;
149185
186+
187+ initService (site );
188+
150189 //-------------------------------------
151190 // extracting form player args
152191 //-------------------------------------
153192 JSONObject playerArgs = null ;
154- JSONObject ytAssets = null ;
155- String dashManifest ;
156193 {
157194 try {
158195 String jsonString = matchGroup1 ("ytplayer.config\\ s*=\\ s*(\\ {.*?\\ });" , site );
159196 JSONObject jsonObj = new JSONObject (jsonString );
160197 playerArgs = jsonObj .getJSONObject ("args" );
161- ytAssets = jsonObj .getJSONObject ("assets" );
162198 }
163199 catch (Exception e ) {
164200 e .printStackTrace ();
@@ -168,31 +204,31 @@ public VideoInfo getVideoInfo(String siteUrl) {
168204 }
169205 }
170206
207+ //-----------------------
208+ // load and extract audio
209+ //-----------------------
210+ try {
211+ String dashManifest = playerArgs .getString ("dashmpd" );
212+ videoInfo .audioStreams = parseDashManifest (dashManifest , decryptionCode );
213+
214+ } catch (NullPointerException e ) {
215+ Log .e (TAG , "Could not find \" dashmpd\" upon the player args (maybe no dash manifest available)." );
216+ } catch (Exception e ) {
217+ e .printStackTrace ();
218+ }
219+
171220 try {
221+ //--------------------------------------------
222+ // extract general information about the video
223+ //--------------------------------------------
224+
172225 videoInfo .uploader = playerArgs .getString ("author" );
173226 videoInfo .title = playerArgs .getString ("title" );
174227 //first attempt gating a small image version
175228 //in the html extracting part we try to get a thumbnail with a higher resolution
176229 videoInfo .thumbnail_url = playerArgs .getString ("thumbnail_url" );
177230 videoInfo .duration = playerArgs .getInt ("length_seconds" );
178231 videoInfo .average_rating = playerArgs .getString ("avg_rating" );
179- String playerUrl = ytAssets .getString ("js" );
180- if (playerUrl .startsWith ("//" )) {
181- playerUrl = "https:" + playerUrl ;
182- }
183- if (decryptionCode .isEmpty ()) {
184- decryptionCode = loadDecryptionCode (playerUrl );
185- }
186-
187- // extract audio
188- try {
189- dashManifest = playerArgs .getString ("dashmpd" );
190- videoInfo .audioStreams = parseDashManifest (dashManifest , decryptionCode );
191- } catch (Exception e ) {
192- //todo: check if the following statement is true
193- Log .e (TAG , "Dash manifest doesn't seem to be available." );
194- e .printStackTrace ();
195- }
196232
197233 //------------------------------------
198234 // extract video stream url
@@ -211,9 +247,6 @@ public VideoInfo getVideoInfo(String siteUrl) {
211247
212248 // if video has a signature: decrypt it and add it to the url
213249 if (tags .get ("s" ) != null ) {
214- if (decryptionCode .isEmpty ()) {
215- decryptionCode = loadDecryptionCode (playerUrl );
216- }
217250 streamUrl = streamUrl + "&signature=" + decryptSignature (tags .get ("s" ), decryptionCode );
218251 }
219252
@@ -231,9 +264,9 @@ public VideoInfo getVideoInfo(String siteUrl) {
231264 e .printStackTrace ();
232265 }
233266
234- //-------------------------------
235- // extracting from html page
236- //-------------------------------
267+ //---------------------------------------
268+ // extracting information from html page
269+ //---------------------------------------
237270
238271
239272 // Determine what went wrong when the Video is not available
0 commit comments