This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
YouTubeExtractor.java
763 lines (680 loc) · 32 KB
/
YouTubeExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package at.huber.youtubeExtractor;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.util.SparseArray;
import com.evgenii.jsevaluator.JsEvaluator;
import com.evgenii.jsevaluator.interfaces.JsCallback;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class YouTubeExtractor extends AsyncTask<String, Void, SparseArray<YtFile>> {
private final static boolean CACHING = true;
static boolean LOGGING = false;
private final static String LOG_TAG = "YouTubeExtractor";
private final static String CACHE_FILE_NAME = "decipher_js_funct";
private final static int DASH_PARSE_RETRIES = 5;
private Context context;
private String videoID;
private VideoMeta videoMeta;
private boolean includeWebM = true;
private boolean useHttp = false;
private boolean parseDashManifest = false;
private volatile String decipheredSignature;
private static String decipherJsFileName;
private static String decipherFunctions;
private static String decipherFunctionName;
private final Lock lock = new ReentrantLock();
private final Condition jsExecuting = lock.newCondition();
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36";
private static final String STREAM_MAP_STRING = "url_encoded_fmt_stream_map";
private static final Pattern patYouTubePageLink = Pattern.compile("(http|https)://(www\\.|m.|)youtube\\.com/watch\\?v=(.+?)( |\\z|&)");
private static final Pattern patYouTubeShortLink = Pattern.compile("(http|https)://(www\\.|)youtu.be/(.+?)( |\\z|&)");
private static final Pattern patDashManifest1 = Pattern.compile("dashmpd=(.+?)(&|\\z)");
private static final Pattern patDashManifest2 = Pattern.compile("\"dashmpd\":\"(.+?)\"");
private static final Pattern patDashManifestEncSig = Pattern.compile("/s/([0-9A-F|.]{10,}?)(/|\\z)");
private static final Pattern patTitle = Pattern.compile("title=(.*?)(&|\\z)");
private static final Pattern patAuthor = Pattern.compile("author=(.+?)(&|\\z)");
private static final Pattern patChannelId = Pattern.compile("ucid=(.+?)(&|\\z)");
private static final Pattern patLength = Pattern.compile("length_seconds=(\\d+?)(&|\\z)");
private static final Pattern patViewCount = Pattern.compile("view_count=(\\d+?)(&|\\z)");
private static final Pattern patHlsvp = Pattern.compile("hlsvp=(.+?)(&|\\z)");
private static final Pattern patHlsItag = Pattern.compile("/itag/(\\d+?)/");
private static final Pattern patItag = Pattern.compile("itag=([0-9]+?)([&,])");
private static final Pattern patEncSig = Pattern.compile("s=([0-9A-F|.]{10,}?)([&,\"])");
private static final Pattern patIsSigEnc = Pattern.compile("s%3D([0-9A-F|.]{10,}?)%26");
private static final Pattern patUrl = Pattern.compile("url=(.+?)([&,])");
private static final Pattern patVariableFunction = Pattern.compile("([{; =])([a-zA-Z$][a-zA-Z0-9$]{0,2})\\.([a-zA-Z$][a-zA-Z0-9$]{0,2})\\(");
private static final Pattern patFunction = Pattern.compile("([{; =])([a-zA-Z$_][a-zA-Z0-9$]{0,2})\\(");
private static final Pattern patDecryptionJsFile = Pattern.compile("jsbin\\\\/(player-(.+?).js)");
private static final Pattern patSignatureDecFunction = Pattern.compile("\"signature\",(.{1,3}?)\\(.{1,10}?\\)");
private static final SparseArray<Format> FORMAT_MAP = new SparseArray<>();
static {
// http://en.wikipedia.org/wiki/YouTube#Quality_and_formats
// Video and Audio
FORMAT_MAP.put(17, new Format(17, "3gp", 144, Format.VCodec.MPEG4, Format.ACodec.AAC, 24, false));
FORMAT_MAP.put(36, new Format(36, "3gp", 240, Format.VCodec.MPEG4, Format.ACodec.AAC, 32, false));
FORMAT_MAP.put(5, new Format(5, "flv", 240, Format.VCodec.H263, Format.ACodec.MP3, 64, false));
FORMAT_MAP.put(43, new Format(43, "webm", 360, Format.VCodec.VP8, Format.ACodec.VORBIS, 128, false));
FORMAT_MAP.put(18, new Format(18, "mp4", 360, Format.VCodec.H264, Format.ACodec.AAC, 96, false));
FORMAT_MAP.put(22, new Format(22, "mp4", 720, Format.VCodec.H264, Format.ACodec.AAC, 192, false));
// Dash Video
FORMAT_MAP.put(160, new Format(160, "mp4", 144, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(133, new Format(133, "mp4", 240, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(134, new Format(134, "mp4", 360, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(135, new Format(135, "mp4", 480, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(136, new Format(136, "mp4", 720, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(137, new Format(137, "mp4", 1080, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(264, new Format(264, "mp4", 1440, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(266, new Format(266, "mp4", 2160, Format.VCodec.H264, Format.ACodec.NONE, true));
FORMAT_MAP.put(298, new Format(298, "mp4", 720, Format.VCodec.H264, 60, Format.ACodec.NONE, true));
FORMAT_MAP.put(299, new Format(299, "mp4", 1080, Format.VCodec.H264, 60, Format.ACodec.NONE, true));
// Dash Audio
FORMAT_MAP.put(140, new Format(140, "m4a", Format.VCodec.NONE, Format.ACodec.AAC, 128, true));
FORMAT_MAP.put(141, new Format(141, "m4a", Format.VCodec.NONE, Format.ACodec.AAC, 256, true));
// WEBM Dash Video
FORMAT_MAP.put(278, new Format(278, "webm", 144, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(242, new Format(242, "webm", 240, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(243, new Format(243, "webm", 360, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(244, new Format(244, "webm", 480, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(247, new Format(247, "webm", 720, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(248, new Format(248, "webm", 1080, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(271, new Format(271, "webm", 1440, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(313, new Format(313, "webm", 2160, Format.VCodec.VP9, Format.ACodec.NONE, true));
FORMAT_MAP.put(302, new Format(302, "webm", 720, Format.VCodec.VP9, 60, Format.ACodec.NONE, true));
FORMAT_MAP.put(308, new Format(308, "webm", 1440, Format.VCodec.VP9, 60, Format.ACodec.NONE, true));
FORMAT_MAP.put(303, new Format(303, "webm", 1080, Format.VCodec.VP9, 60, Format.ACodec.NONE, true));
FORMAT_MAP.put(315, new Format(315, "webm", 2160, Format.VCodec.VP9, 60, Format.ACodec.NONE, true));
// WEBM Dash Audio
FORMAT_MAP.put(171, new Format(171, "webm", Format.VCodec.NONE, Format.ACodec.VORBIS, 128, true));
FORMAT_MAP.put(249, new Format(249, "webm", Format.VCodec.NONE, Format.ACodec.OPUS, 48, true));
FORMAT_MAP.put(250, new Format(250, "webm", Format.VCodec.NONE, Format.ACodec.OPUS, 64, true));
FORMAT_MAP.put(251, new Format(251, "webm", Format.VCodec.NONE, Format.ACodec.OPUS, 160, true));
// HLS Live Stream
FORMAT_MAP.put(91, new Format(91, "mp4", 144 ,Format.VCodec.H264, Format.ACodec.AAC, 48, false, true));
FORMAT_MAP.put(92, new Format(92, "mp4", 240 ,Format.VCodec.H264, Format.ACodec.AAC, 48, false, true));
FORMAT_MAP.put(93, new Format(93, "mp4", 360 ,Format.VCodec.H264, Format.ACodec.AAC, 128, false, true));
FORMAT_MAP.put(94, new Format(94, "mp4", 480 ,Format.VCodec.H264, Format.ACodec.AAC, 128, false, true));
FORMAT_MAP.put(95, new Format(95, "mp4", 720 ,Format.VCodec.H264, Format.ACodec.AAC, 256, false, true));
FORMAT_MAP.put(96, new Format(96, "mp4", 1080 ,Format.VCodec.H264, Format.ACodec.AAC, 256, false, true));
}
public YouTubeExtractor(Context con) {
context = con;
}
@Override
protected void onPostExecute(SparseArray<YtFile> ytFiles) {
onExtractionComplete(ytFiles, videoMeta);
}
/**
* Start the extraction.
*
* @param youtubeLink the youtube page link or video id
* @param parseDashManifest true if the dash manifest should be downloaded and parsed
* @param includeWebM true if WebM streams should be extracted
*/
public void extract(String youtubeLink, boolean parseDashManifest, boolean includeWebM) {
this.parseDashManifest = parseDashManifest;
this.includeWebM = includeWebM;
this.execute(youtubeLink);
}
protected abstract void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta videoMeta);
@Override
protected SparseArray<YtFile> doInBackground(String... params) {
videoID = null;
String ytUrl = params[0];
if (ytUrl == null) {
return null;
}
Matcher mat = patYouTubePageLink.matcher(ytUrl);
if (mat.find()) {
videoID = mat.group(3);
} else {
mat = patYouTubeShortLink.matcher(ytUrl);
if (mat.find()) {
videoID = mat.group(3);
} else if (ytUrl.matches("\\p{Graph}+?")) {
videoID = ytUrl;
}
}
if (videoID != null) {
try {
return getStreamUrls();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e(LOG_TAG, "Wrong YouTube link format");
}
return null;
}
private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedException {
String ytInfoUrl = (useHttp) ? "http://" : "https://";
ytInfoUrl += "www.youtube.com/get_video_info?video_id=" + videoID + "&eurl="
+ URLEncoder.encode("https://youtube.googleapis.com/v/" + videoID, "UTF-8");
String dashMpdUrl = null;
String streamMap;
BufferedReader reader = null;
URL getUrl = new URL(ytInfoUrl);
if(LOGGING)
Log.d(LOG_TAG, "infoUrl: " + ytInfoUrl);
HttpURLConnection urlConnection = (HttpURLConnection) getUrl.openConnection();
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
streamMap = reader.readLine();
} finally {
if (reader != null)
reader.close();
urlConnection.disconnect();
}
Matcher mat;
String curJsFileName = null;
String[] streams;
SparseArray<String> encSignatures = null;
parseVideoMeta(streamMap);
if(videoMeta.isLiveStream()){
mat = patHlsvp.matcher(streamMap);
if(mat.find()) {
String hlsvp = URLDecoder.decode(mat.group(1), "UTF-8");
SparseArray<YtFile> ytFiles = new SparseArray<>();
getUrl = new URL(hlsvp);
urlConnection = (HttpURLConnection) getUrl.openConnection();
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if(line.startsWith("https://") || line.startsWith("http://")){
mat = patHlsItag.matcher(line);
if(mat.find()){
int itag = Integer.parseInt(mat.group(1));
YtFile newFile = new YtFile(FORMAT_MAP.get(itag), line);
ytFiles.put(itag, newFile);
}
}
}
} finally {
if (reader != null)
reader.close();
urlConnection.disconnect();
}
if (ytFiles.size() == 0) {
if (LOGGING)
Log.d(LOG_TAG, streamMap);
return null;
}
return ytFiles;
}
return null;
}
// "use_cipher_signature" disappeared, we check whether at least one ciphered signature
// exists int the stream_map.
boolean sigEnc = true;
if(streamMap != null && streamMap.contains(STREAM_MAP_STRING)){
String streamMapSub = streamMap.substring(streamMap.indexOf(STREAM_MAP_STRING));
mat = patIsSigEnc.matcher(streamMapSub);
if(!mat.find())
sigEnc = false;
}
// Some videos are using a ciphered signature we need to get the
// deciphering js-file from the youtubepage.
if (sigEnc) {
// Get the video directly from the youtubepage
if (CACHING
&& (decipherJsFileName == null || decipherFunctions == null || decipherFunctionName == null)) {
readDecipherFunctFromCache();
}
getUrl = new URL("https://youtube.com/watch?v=" + videoID);
urlConnection = (HttpURLConnection) getUrl.openConnection();
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
// Log.d("line", line);
if (line.contains(STREAM_MAP_STRING)) {
streamMap = line.replace("\\u0026", "&");
break;
}
}
} finally {
if (reader != null)
reader.close();
urlConnection.disconnect();
}
encSignatures = new SparseArray<>();
mat = patDecryptionJsFile.matcher(streamMap);
if (mat.find()) {
curJsFileName = mat.group(1).replace("\\/", "/");
if (decipherJsFileName == null || !decipherJsFileName.equals(curJsFileName)) {
decipherFunctions = null;
decipherFunctionName = null;
}
decipherJsFileName = curJsFileName;
}
if (parseDashManifest) {
mat = patDashManifest2.matcher(streamMap);
if (mat.find()) {
dashMpdUrl = mat.group(1).replace("\\/", "/");
mat = patDashManifestEncSig.matcher(dashMpdUrl);
if (mat.find()) {
encSignatures.append(0, mat.group(1));
} else {
dashMpdUrl = null;
}
}
}
} else {
if (parseDashManifest) {
mat = patDashManifest1.matcher(streamMap);
if (mat.find()) {
dashMpdUrl = URLDecoder.decode(mat.group(1), "UTF-8");
}
}
streamMap = URLDecoder.decode(streamMap, "UTF-8");
}
streams = streamMap.split(",|"+STREAM_MAP_STRING+"|&adaptive_fmts=");
SparseArray<YtFile> ytFiles = new SparseArray<>();
for (String encStream : streams) {
encStream = encStream + ",";
if (!encStream.contains("itag%3D")) {
continue;
}
String stream;
stream = URLDecoder.decode(encStream, "UTF-8");
mat = patItag.matcher(stream);
int itag;
if (mat.find()) {
itag = Integer.parseInt(mat.group(1));
if (LOGGING)
Log.d(LOG_TAG, "Itag found:" + itag);
if (FORMAT_MAP.get(itag) == null) {
if (LOGGING)
Log.d(LOG_TAG, "Itag not in list:" + itag);
continue;
} else if (!includeWebM && FORMAT_MAP.get(itag).getExt().equals("webm")) {
continue;
}
} else {
continue;
}
if (curJsFileName != null) {
mat = patEncSig.matcher(stream);
if (mat.find()) {
encSignatures.append(itag, mat.group(1));
}
}
mat = patUrl.matcher(encStream);
String url = null;
if (mat.find()) {
url = mat.group(1);
}
if (url != null) {
Format format = FORMAT_MAP.get(itag);
String finalUrl = URLDecoder.decode(url, "UTF-8");
YtFile newVideo = new YtFile(format, finalUrl);
ytFiles.put(itag, newVideo);
}
}
if (encSignatures != null) {
if (LOGGING)
Log.d(LOG_TAG, "Decipher signatures");
String signature;
decipheredSignature = null;
if (decipherSignature(encSignatures)) {
lock.lock();
try {
jsExecuting.await(7, TimeUnit.SECONDS);
} finally {
lock.unlock();
}
}
signature = decipheredSignature;
if (signature == null) {
return null;
} else {
String[] sigs = signature.split("\n");
for (int i = 0; i < encSignatures.size() && i < sigs.length; i++) {
int key = encSignatures.keyAt(i);
if (key == 0) {
dashMpdUrl = dashMpdUrl.replace("/s/" + encSignatures.get(key), "/signature/" + sigs[i]);
} else {
String url = ytFiles.get(key).getUrl();
url += "&signature=" + sigs[i];
YtFile newFile = new YtFile(FORMAT_MAP.get(key), url);
ytFiles.put(key, newFile);
}
}
}
}
if (parseDashManifest && dashMpdUrl != null) {
for (int i = 0; i < DASH_PARSE_RETRIES; i++) {
try {
// It sometimes fails to connect for no apparent reason. We just retry.
parseDashManifest(dashMpdUrl, ytFiles);
break;
} catch (IOException io) {
Thread.sleep(5);
if (LOGGING)
Log.d(LOG_TAG, "Failed to parse dash manifest " + (i + 1));
}
}
}
if (ytFiles.size() == 0) {
if (LOGGING)
Log.d(LOG_TAG, streamMap);
return null;
}
return ytFiles;
}
private boolean decipherSignature(final SparseArray<String> encSignatures) throws IOException {
// Assume the functions don't change that much
if (decipherFunctionName == null || decipherFunctions == null) {
String decipherFunctUrl = "https://s.ytimg.com/yts/jsbin/" + decipherJsFileName;
BufferedReader reader = null;
String javascriptFile;
URL url = new URL(decipherFunctUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder("");
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(" ");
}
javascriptFile = sb.toString();
} finally {
if (reader != null)
reader.close();
urlConnection.disconnect();
}
if (LOGGING)
Log.d(LOG_TAG, "Decipher FunctURL: " + decipherFunctUrl);
Matcher mat = patSignatureDecFunction.matcher(javascriptFile);
if (mat.find()) {
decipherFunctionName = mat.group(1);
if (LOGGING)
Log.d(LOG_TAG, "Decipher Functname: " + decipherFunctionName);
Pattern patMainVariable = Pattern.compile("(var |\\s|,|;)" + decipherFunctionName.replace("$", "\\$") +
"(=function\\((.{1,3})\\)\\{)");
String mainDecipherFunct;
mat = patMainVariable.matcher(javascriptFile);
if (mat.find()) {
mainDecipherFunct = "var " + decipherFunctionName + mat.group(2);
} else {
Pattern patMainFunction = Pattern.compile("function " + decipherFunctionName.replace("$", "\\$") +
"(\\((.{1,3})\\)\\{)");
mat = patMainFunction.matcher(javascriptFile);
if (!mat.find())
return false;
mainDecipherFunct = "function " + decipherFunctionName + mat.group(2);
}
int startIndex = mat.end();
for (int braces = 1, i = startIndex; i < javascriptFile.length(); i++) {
if (braces == 0 && startIndex + 5 < i) {
mainDecipherFunct += javascriptFile.substring(startIndex, i) + ";";
break;
}
if (javascriptFile.charAt(i) == '{')
braces++;
else if (javascriptFile.charAt(i) == '}')
braces--;
}
decipherFunctions = mainDecipherFunct;
// Search the main function for extra functions and variables
// needed for deciphering
// Search for variables
mat = patVariableFunction.matcher(mainDecipherFunct);
while (mat.find()) {
String variableDef = "var " + mat.group(2) + "={";
if (decipherFunctions.contains(variableDef)) {
continue;
}
startIndex = javascriptFile.indexOf(variableDef) + variableDef.length();
for (int braces = 1, i = startIndex; i < javascriptFile.length(); i++) {
if (braces == 0) {
decipherFunctions += variableDef + javascriptFile.substring(startIndex, i) + ";";
break;
}
if (javascriptFile.charAt(i) == '{')
braces++;
else if (javascriptFile.charAt(i) == '}')
braces--;
}
}
// Search for functions
mat = patFunction.matcher(mainDecipherFunct);
while (mat.find()) {
String functionDef = "function " + mat.group(2) + "(";
if (decipherFunctions.contains(functionDef)) {
continue;
}
startIndex = javascriptFile.indexOf(functionDef) + functionDef.length();
for (int braces = 0, i = startIndex; i < javascriptFile.length(); i++) {
if (braces == 0 && startIndex + 5 < i) {
decipherFunctions += functionDef + javascriptFile.substring(startIndex, i) + ";";
break;
}
if (javascriptFile.charAt(i) == '{')
braces++;
else if (javascriptFile.charAt(i) == '}')
braces--;
}
}
if (LOGGING)
Log.d(LOG_TAG, "Decipher Function: " + decipherFunctions);
decipherViaWebView(encSignatures);
if (CACHING) {
writeDeciperFunctToChache();
}
} else {
return false;
}
} else {
decipherViaWebView(encSignatures);
}
return true;
}
private void parseDashManifest(String dashMpdUrl, SparseArray<YtFile> ytFiles) throws IOException {
Pattern patBaseUrl = Pattern.compile("<BaseURL yt:contentLength=\"[0-9]+?\">(.+?)</BaseURL>");
String dashManifest;
BufferedReader reader = null;
URL getUrl = new URL(dashMpdUrl);
HttpURLConnection urlConnection = (HttpURLConnection) getUrl.openConnection();
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
reader.readLine();
dashManifest = reader.readLine();
} finally {
if (reader != null)
reader.close();
urlConnection.disconnect();
}
if (dashManifest == null)
return;
Matcher mat = patBaseUrl.matcher(dashManifest);
while (mat.find()) {
int itag;
String url = mat.group(1);
Matcher mat2 = patItag.matcher(url);
if (mat2.find()) {
itag = Integer.parseInt(mat2.group(1));
if (FORMAT_MAP.get(itag) == null)
continue;
if (!includeWebM && FORMAT_MAP.get(itag).getExt().equals("webm"))
continue;
} else {
continue;
}
url = url.replace("&", "&").replace(",", "%2C").
replace("mime=audio/", "mime=audio%2F").
replace("mime=video/", "mime=video%2F");
YtFile yf = new YtFile(FORMAT_MAP.get(itag), url);
ytFiles.append(itag, yf);
}
}
private void parseVideoMeta(String getVideoInfo) throws UnsupportedEncodingException {
boolean isLiveStream = false;
String title = null, author = null, channelId = null;
long viewCount = 0, length = 0;
Matcher mat = patTitle.matcher(getVideoInfo);
if (mat.find()) {
title = URLDecoder.decode(mat.group(1), "UTF-8");
}
mat = patHlsvp.matcher(getVideoInfo);
if(mat.find())
isLiveStream = true;
mat = patAuthor.matcher(getVideoInfo);
if (mat.find()) {
author = URLDecoder.decode(mat.group(1), "UTF-8");
}
mat = patChannelId.matcher(getVideoInfo);
if (mat.find()) {
channelId = mat.group(1);
}
mat = patLength.matcher(getVideoInfo);
if (mat.find()) {
length = Long.parseLong(mat.group(1));
}
mat = patViewCount.matcher(getVideoInfo);
if (mat.find()) {
viewCount = Long.parseLong(mat.group(1));
}
videoMeta = new VideoMeta(videoID, title, author, channelId, length, viewCount, isLiveStream);
}
private void readDecipherFunctFromCache() {
if (context != null) {
File cacheFile = new File(context.getCacheDir().getAbsolutePath() + "/" + CACHE_FILE_NAME);
// The cached functions are valid for 2 weeks
if (cacheFile.exists() && (System.currentTimeMillis() - cacheFile.lastModified()) < 1209600000) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(cacheFile), "UTF-8"));
decipherJsFileName = reader.readLine();
decipherFunctionName = reader.readLine();
decipherFunctions = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
/**
* Parse the dash manifest for different dash streams and high quality audio. Default: false
*/
public void setParseDashManifest(boolean parseDashManifest) {
this.parseDashManifest = parseDashManifest;
}
/**
* Include the webm format files into the result. Default: true
*/
public void setIncludeWebM(boolean includeWebM) {
this.includeWebM = includeWebM;
}
/**
* Set default protocol of the returned urls to HTTP instead of HTTPS.
* HTTP may be blocked in some regions so HTTPS is the default value.
* <p/>
* Note: Enciphered videos require HTTPS so they are not affected by
* this.
*/
public void setDefaultHttpProtocol(boolean useHttp) {
this.useHttp = useHttp;
}
private void writeDeciperFunctToChache() {
if (context != null) {
File cacheFile = new File(context.getCacheDir().getAbsolutePath() + "/" + CACHE_FILE_NAME);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "UTF-8"));
writer.write(decipherJsFileName + "\n");
writer.write(decipherFunctionName + "\n");
writer.write(decipherFunctions);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private void decipherViaWebView(final SparseArray<String> encSignatures) {
if (context == null) {
return;
}
final StringBuilder stb = new StringBuilder(decipherFunctions + " function decipher(");
stb.append("){return ");
for (int i = 0; i < encSignatures.size(); i++) {
int key = encSignatures.keyAt(i);
if (i < encSignatures.size() - 1)
stb.append(decipherFunctionName).append("('").append(encSignatures.get(key)).
append("')+\"\\n\"+");
else
stb.append(decipherFunctionName).append("('").append(encSignatures.get(key)).
append("')");
}
stb.append("};decipher();");
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
new JsEvaluator(context).evaluate(stb.toString(), new JsCallback() {
@Override
public void onResult(String result) {
lock.lock();
try {
decipheredSignature = result;
jsExecuting.signal();
} finally {
lock.unlock();
}
}
@Override
public void onError(String errorMessage) {
lock.lock();
try {
if(LOGGING)
Log.e(LOG_TAG, errorMessage);
jsExecuting.signal();
} finally {
lock.unlock();
}
}
});
}
});
}
}