forked from noctarius/funimation-downloader-nx-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuni.js
921 lines (833 loc) · 30.8 KB
/
funi.js
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
#!/usr/bin/env node
// modules build-in
const fs = require('fs');
const path = require('path');
// package json
const packageJson = require('./package.json');
// program name
console.log(`\n=== Funimation Downloader NX ${packageJson.version} ===\n`);
const api_host = 'https://prod-api-funimationnow.dadcdigital.com/api';
// modules extra
const yaml = require('yaml');
const shlp = require('sei-helper');
const { lookpath } = require('lookpath');
const m3u8 = require('m3u8-parsed');
const crypto = require('crypto');
const got = require('got');
// extra
const appYargs = require('./modules/module.app-args');
const getYamlCfg = require('./modules/module.cfg-loader');
const vttConvert = require('./modules/module.vttconvert');
// new-cfg
const workingDir = process.pkg ? path.dirname(process.execPath) : __dirname;
const binCfgFile = path.join(workingDir, 'config', 'bin-path');
const dirCfgFile = path.join(workingDir, 'config', 'dir-path');
const cliCfgFile = path.join(workingDir, 'config', 'cli-defaults');
const tokenFile = path.join(workingDir, 'config', 'token');
// params
let cfg = {
bin: getYamlCfg(binCfgFile),
dir: getYamlCfg(dirCfgFile),
cli: getYamlCfg(cliCfgFile),
};
// make sure cfg params aren't null
if (cfg.bin === null){
cfg.bin = {};
console.log('[WARN] bin-path.yml is empty or does not exist!\n');
}
if (cfg.dir === null){
cfg.dir = {};
console.log('[WARN] dir-path.yml is empty or does not exist!\n');
}
if (!Object.prototype.hasOwnProperty.call(cfg.dir, 'content')) cfg.dir.content = './videos/';
if (cfg.cli === null){
cfg.cli = {};
console.log('[WARN] cli-defaults.yml is empty or does not exist!\n');
}
/* Normalise paths for use outside the current directory */
for (let key of Object.keys(cfg.dir)) {
if (!path.isAbsolute(cfg.dir[key])) {
cfg.dir[key] = path.join(workingDir, cfg.dir[key]);
}
}
for (let key of Object.keys(cfg.bin)) {
if (!path.isAbsolute(cfg.bin[key])) {
cfg.bin[key] = path.join(workingDir, cfg.bin[key]);
}
}
// token
let token = getYamlCfg(tokenFile);
if (token === null) token = false;
else if (token.token === null) token = false;
else token = token.token;
// info if token not set
if(!token){
console.log('[INFO] Token not set!\n');
}
// cli
const argv = appYargs.appArgv(cfg.cli);
module.exports = {
argv,
cfg
};
// Import modules after argv has been exported
const getData = require('./modules/module.getdata.js');
const merger = require('./modules/merger');
// check page
if(!isNaN(parseInt(argv.p, 10)) && parseInt(argv.p, 10) > 0){
argv.p = parseInt(argv.p, 10);
}
else{
argv.p = 1;
}
// fn variables
let title = '',
showTitle = '',
fnEpNum = 0,
fnOutput = '',
season = 0,
tsDlPath = [],
stDlPath = [];
// select mode
if(argv.auth){
auth();
}
else if(argv.search){
searchShow();
}
else if(argv.s && !isNaN(parseInt(argv.s, 10)) && parseInt(argv.s, 10) > 0){
getShow();
}
else{
appYargs.showHelp();
process.exit();
}
// auth
async function auth(){
let authOpts = {};
authOpts.user = await shlp.question('[Q] LOGIN/EMAIL');
authOpts.pass = await shlp.question('[Q] PASSWORD ');
let authData = await getData({
baseUrl: api_host,
url: '/auth/login/',
useProxy: true,
auth: authOpts,
debug: argv.debug,
});
if(authData.ok){
authData = JSON.parse(authData.res.body);
if(authData.token){
console.log('[INFO] Authentication success, your token: %s%s\n', authData.token.slice(0,8),'*'.repeat(32));
fs.writeFileSync(tokenFile + '.yml', yaml.stringify({'token': authData.token}));
}
else if(authData.error){
console.log('[ERROR]%s\n', authData.error);
process.exit(1);
}
}
}
// search show
async function searchShow(){
let qs = {unique: true, limit: 100, q: argv.search, offset: (argv.p-1)*1000 };
let searchData = await getData({
baseUrl: api_host,
url: '/source/funimation/search/auto/',
querystring: qs,
token: token,
useToken: true,
useProxy: true,
debug: argv.debug,
});
if(!searchData.ok){return;}
searchData = JSON.parse(searchData.res.body);
if(searchData.detail){
console.log(`[ERROR] ${searchData.detail}`);
return;
}
if(searchData.items && searchData.items.hits){
let shows = searchData.items.hits;
console.log('[INFO] Search Results:');
for(let ssn in shows){
console.log(`[#${shows[ssn].id}] ${shows[ssn].title}` + (shows[ssn].tx_date?` (${shows[ssn].tx_date})`:''));
}
}
console.log('[INFO] Total shows found: %s\n',searchData.count);
}
// get show
async function getShow(){
// show main data
let showData = await getData({
baseUrl: api_host,
url: `/source/catalog/title/${parseInt(argv.s, 10)}`,
token: token,
useToken: true,
useProxy: true,
debug: argv.debug,
});
// check errors
if(!showData.ok){return;}
showData = JSON.parse(showData.res.body);
if(showData.status){
console.log('[ERROR] Error #%d: %s\n', showData.status, showData.data.errors[0].detail);
process.exit(1);
}
else if(!showData.items || showData.items.length<1){
console.log('[ERROR] Show not found\n');
process.exit(0);
}
showData = showData.items[0];
console.log('[#%s] %s (%s)',showData.id,showData.title,showData.releaseYear);
// show episodes
let qs = { limit: -1, sort: 'order', sort_direction: 'ASC', title_id: parseInt(argv.s,10) };
if(argv.alt){ qs.language = 'English'; }
let episodesData = await getData({
baseUrl: api_host,
url: '/funimation/episodes/',
querystring: qs,
token: token,
useToken: true,
useProxy: true,
debug: argv.debug,
});
if(!episodesData.ok){return;}
let eps = JSON.parse(episodesData.res.body).items, fnSlug = [], is_selected = false;
argv.e = typeof argv.e == 'number' || typeof argv.e == 'string' ? argv.e.toString() : '';
argv.e = argv.e.match(',') ? argv.e.split(',') : [argv.e];
let epSelList = argv.e, epSelRanges = [], epSelEps = [];
epSelList = epSelList.map((e)=>{
if(e.match('-')){
e = e.split('-');
if( e[0].match(/^(?:[A-Z]+|)\d+$/i) && e[1].match(/^\d+$/) ){
e[0] = e[0].replace(/^(?:([A-Z]+)|)(0+)/i,'$1');
let letter = e[0].match(/^([A-Z]+)\d+$/i) ? e[0].match(/^([A-Z]+)\d+$/i)[1].toUpperCase() : '';
e[0] = e[0].replace(/^[A-Z]+(\d+)$/i,'$1');
e[0] = parseInt(e[0]);
e[1] = parseInt(e[1]);
if(e[0] < e[1]){
for(let i=e[0];i<e[1]+1;i++){
epSelRanges.push(letter+i);
}
return '';
}
else{
return (letter+e[0]);
}
}
else{
return '';
}
}
else if(e.match(/^(?:[A-Z]+|)\d+$/i)){
return e.replace(/^(?:([A-Z]+)|)(0+)/i,'$1').toUpperCase();
}
else{
return '';
}
});
epSelList = [...new Set(epSelList.concat(epSelRanges))];
// parse episodes list
for(let e in eps){
let showStrId = eps[e].ids.externalShowId;
let epStrId = eps[e].ids.externalEpisodeId.replace(new RegExp('^'+showStrId),'');
// select
if (argv.all) {
fnSlug.push({title:eps[e].item.titleSlug,episode:eps[e].item.episodeSlug});
epSelEps.push(epStrId);
is_selected = true;
}
else if(epSelList.includes(epStrId.replace(/^(?:([A-Z]+)|)(0+)/,'$1'))){
fnSlug.push({title:eps[e].item.titleSlug,episode:eps[e].item.episodeSlug});
epSelEps.push(epStrId);
is_selected = true;
}
else{
is_selected = false;
}
// console vars
let tx_snum = eps[e].item.seasonNum==1?'':` S${eps[e].item.seasonNum}`;
let tx_type = eps[e].mediaCategory != 'episode' ? eps[e].mediaCategory : '';
let tx_enum = eps[e].item.episodeNum !== '' ?
`#${(eps[e].item.episodeNum < 10 ? '0' : '')+eps[e].item.episodeNum}` : '#'+eps[e].item.episodeId;
let qua_str = eps[e].quality.height ? eps[e].quality.quality + eps[e].quality.height : 'UNK';
let aud_str = eps[e].audio.length > 0 ? `, ${eps[e].audio.join(', ')}` : '';
let rtm_str = eps[e].item.runtime !== '' ? eps[e].item.runtime : '??:??';
// console string
let episodeIdStr = epStrId;
let conOut = `[${episodeIdStr}] `;
conOut += `${eps[e].item.titleName+tx_snum} - ${tx_type+tx_enum} ${eps[e].item.episodeName} `;
conOut += `(${rtm_str}) [${qua_str+aud_str}]`;
conOut += is_selected ? ' (selected)' : '';
conOut += eps.length-1 == e ? '\n' : '';
console.log(conOut);
}
if(fnSlug.length<1){
console.log('[INFO] Episodes not selected!\n');
process.exit();
}
else{
console.log('[INFO] Selected Episodes: %s\n',epSelEps.join(', '));
for(let fnEp=0;fnEp<fnSlug.length;fnEp++){
await getEpisode(fnSlug[fnEp]);
}
}
}
async function getEpisode(fnSlug){
let episodeData = await getData({
baseUrl: api_host,
url: `/source/catalog/episode/${fnSlug.title}/${fnSlug.episode}/`,
token: token,
useToken: true,
useProxy: true,
debug: argv.debug,
});
if(!episodeData.ok){return;}
let ep = JSON.parse(episodeData.res.body).items[0], streamIds = [];
// build fn
showTitle = ep.parent.title;
title = ep.title;
season = parseInt(ep.parent.seasonNumber);
if(ep.mediaCategory != 'Episode'){
ep.number = ep.number !== '' ? ep.mediaCategory+ep.number : ep.mediaCategory+'#'+ep.id;
}
fnEpNum = isNaN(parseInt(ep.number)) ? ep.number : parseInt(ep.number);
// is uncut
let uncut = {
Japanese: false,
English: false
};
// end
console.log(
'[INFO] %s - S%sE%s - %s',
ep.parent.title,
(ep.parent.seasonNumber ? ep.parent.seasonNumber : '?'),
(ep.number ? ep.number : '?'),
ep.title
);
console.log('[INFO] Available streams (Non-Encrypted):');
// map medias
let media = ep.media.map(function(m){
if(m.mediaType == 'experience'){
if(m.version.match(/uncut/i)){
uncut[m.language] = true;
}
return {
id: m.id,
language: m.language,
version: m.version,
type: m.experienceType,
subtitles: getSubsUrl(m.mediaChildren),
};
}
else{
return { id: 0, type: '' };
}
});
const dubType = {
'enUS': 'English',
'esLA': 'Spanish (Latin Am)',
'ptBR': 'Portuguese (Brazil)',
'zhMN': 'Chinese (Mandarin, PRC)',
'jaJP': 'Japanese'
};
// select
stDlPath = [];
for(let m of media){
let selected = false;
if(m.id > 0 && m.type == 'Non-Encrypted'){
let dub_type = m.language;
let localSubs = [];
let selUncut = !argv.simul && uncut[dub_type] && m.version.match(/uncut/i)
? true
: (!uncut[dub_type] || argv.simul && m.version.match(/simulcast/i) ? true : false);
for (let curDub of argv.dub) {
if(dub_type == dubType[curDub] && selUncut){
streamIds.push({
id: m.id,
lang: merger.getLanguageCode(curDub, curDub.slice(0, -2))
});
stDlPath.push(...m.subtitles);
localSubs = m.subtitles;
selected = true;
}
}
console.log(`[#${m.id}] ${dub_type} [${m.version}]${(selected?' (selected)':'')}${
localSubs && localSubs.length > 0 && selected ? ` (using ${localSubs.map(a => `'${a.langName}'`).join(', ')} for subtitles)` : ''
}`);
}
}
let already = [];
stDlPath = stDlPath.filter(a => {
if (already.includes(a.language)) {
return false;
} else {
already.push(a.language);
return true;
}
});
if(streamIds.length <1){
console.log('[ERROR] Track not selected\n');
return;
}
else{
tsDlPath = [];
for (let streamId of streamIds) {
let streamData = await getData({
baseUrl: api_host,
url: `/source/catalog/video/${streamId.id}/signed`,
token: token,
dinstid: 'uuid',
useToken: true,
useProxy: true,
debug: argv.debug,
});
if(!streamData.ok){return;}
streamData = JSON.parse(streamData.res.body);
if(streamData.errors){
console.log('[ERROR] Error #%s: %s\n',streamData.errors[0].code,streamData.errors[0].detail);
return;
}
else{
for(let u in streamData.items){
if(streamData.items[u].videoType == 'm3u8'){
tsDlPath.push({
path: streamData.items[u].src,
lang: streamId.lang
});
break;
}
}
}
}
if(tsDlPath.length < 1){
console.log('[ERROR] Unknown error\n');
return;
}
else{
await downloadStreams();
}
}
}
function getSubsUrl(m){
if(argv.nosubs && !argv.sub){
return [];
}
let subLangs = argv.subLang;
const subType = {
'enUS': 'English',
'esLA': 'Spanish (Latin Am)',
'ptBR': 'Portuguese (Brazil)'
};
let subLangAvailable = m.some(a => subLangs.some(subLang => a.ext == 'vtt' && a.language === subType[subLang]));
if (!subLangAvailable) {
subLangs = [ 'enUS' ];
}
let found = [];
for(let i in m){
let fpp = m[i].filePath.split('.');
let fpe = fpp[fpp.length-1];
for (let lang of subLangs) {
if(fpe == 'vtt' && m[i].language === subType[lang]) {
found.push({
path: m[i].filePath,
ext: `.${lang}`,
langName: subType[lang],
language: m[i].languages[0].code || lang.slice(0, 2)
});
}
}
}
return found;
}
async function downloadStreams(){
// req playlist
let purvideo = [];
let puraudio = [];
let audioAndVideo = [];
let outName;
for (let streamPath of tsDlPath) {
let plQualityReq = await getData({
url: streamPath.path,
useProxy: (argv.ssp ? false : true),
debug: argv.debug,
});
if(!plQualityReq.ok){return;}
let plQualityLinkList = m3u8(plQualityReq.res.body);
let mainServersList = [
'vmfst-api.prd.funimationsvc.com',
'd132fumi6di1wa.cloudfront.net',
'funiprod.akamaized.net',
];
let plServerList = [],
plStreams = {},
plLayersStr = [],
plLayersRes = {},
plMaxLayer = 1,
plNewIds = 1,
plAud = { uri: '' };
// new uris
let vplReg = /streaming_video_(\d+)_(\d+)_(\d+)_index\.m3u8/;
if(plQualityLinkList.playlists[0].uri.match(vplReg)){
let audioKey = Object.keys(plQualityLinkList.mediaGroups.AUDIO).pop();
if(plQualityLinkList.mediaGroups.AUDIO[audioKey]){
let audioData = plQualityLinkList.mediaGroups.AUDIO[audioKey],
audioEl = Object.keys(audioData);
audioData = audioData[audioEl[0]];
plAud = { ...audioData, ...{ langStr: audioEl[0] } };
}
plQualityLinkList.playlists.sort((a, b) => {
let av = parseInt(a.uri.match(vplReg)[3]);
let bv = parseInt(b.uri.match(vplReg)[3]);
if(av > bv){
return 1;
}
if (av < bv) {
return -1;
}
return 0;
});
}
for(let s of plQualityLinkList.playlists){
if(s.uri.match(/_Layer(\d+)\.m3u8/) || s.uri.match(vplReg)){
// set layer and max layer
let plLayerId = 0;
if(s.uri.match(/_Layer(\d+)\.m3u8/)){
plLayerId = parseInt(s.uri.match(/_Layer(\d+)\.m3u8/)[1]);
}
else{
plLayerId = plNewIds, plNewIds++;
}
plMaxLayer = plMaxLayer < plLayerId ? plLayerId : plMaxLayer;
// set urls and servers
let plUrlDl = s.uri;
let plServer = new URL(plUrlDl).host;
if(!plServerList.includes(plServer)){
plServerList.push(plServer);
}
if(!Object.keys(plStreams).includes(plServer)){
plStreams[plServer] = {};
}
if(plStreams[plServer][plLayerId] && plStreams[plServer][plLayerId] != plUrlDl){
console.log(`[WARN] Non duplicate url for ${plServer} detected, please report to developer!`);
}
else{
plStreams[plServer][plLayerId] = plUrlDl;
}
// set plLayersStr
let plResolution = s.attributes.RESOLUTION;
plLayersRes[plLayerId] = plResolution;
let plBandwidth = Math.round(s.attributes.BANDWIDTH/1024);
if(plLayerId<10){
plLayerId = plLayerId.toString().padStart(2,' ');
}
let qualityStrAdd = `${plLayerId}: ${plResolution.width}x${plResolution.height} (${plBandwidth}KiB/s)`;
let qualityStrRegx = new RegExp(qualityStrAdd.replace(/(:|\(|\)|\/)/g,'\\$1'),'m');
let qualityStrMatch = !plLayersStr.join('\r\n').match(qualityStrRegx);
if(qualityStrMatch){
plLayersStr.push(qualityStrAdd);
}
}
else {
console.log(s.uri);
}
}
for(let s of mainServersList){
if(plServerList.includes(s)){
plServerList.splice(plServerList.indexOf(s), 1);
plServerList.unshift(s);
break;
}
}
if(typeof argv.q == 'object' && argv.q.length > 1){
argv.q = argv.q[argv.q.length-1];
}
argv.q = argv.q < 1 || argv.q > plMaxLayer ? plMaxLayer : argv.q;
let plSelectedServer = plServerList[argv.x-1];
let plSelectedList = plStreams[plSelectedServer];
let videoUrl = argv.x < plServerList.length+1 && plSelectedList[argv.q] ? plSelectedList[argv.q] : '';
plLayersStr.sort();
console.log(`[INFO] Servers available:\n\t${plServerList.join('\n\t')}`);
console.log(`[INFO] Available qualities:\n\t${plLayersStr.join('\n\t')}`);
if(videoUrl != ''){
console.log(`[INFO] Selected layer: ${argv.q} (${plLayersRes[argv.q].width}x${plLayersRes[argv.q].height}) @ ${plSelectedServer}`);
console.log('[INFO] Stream URL:',videoUrl);
fnOutput = parseFileName(argv.fileName, title, fnEpNum, showTitle, season, plLayersRes[argv.q].width, plLayersRes[argv.q].height);
outName = fnOutput;
console.log(`[INFO] Output filename: ${fnOutput}.ts`);
}
else if(argv.x > plServerList.length){
console.log('[ERROR] Server not selected!\n');
return;
}
else{
console.log('[ERROR] Layer not selected!\n');
return;
}
let dlFailed = false;
let dlFailedA = false;
video: if (!argv.novids) {
if (plAud.uri && (purvideo.length > 0 || audioAndVideo.length > 0)) {
break video;
} else if (!plAud.uri && (audioAndVideo.some(a => a.lang === streamPath.lang) || puraudio.some(a => a.lang === streamPath.lang))) {
break video;
}
// download video
let reqVideo = await getData({
url: videoUrl,
useProxy: (argv.ssp ? false : true),
debug: argv.debug,
});
if (!reqVideo.ok) { break video; }
let chunkList = m3u8(reqVideo.res.body);
let tsFile = path.join(cfg.dir.content, `${fnOutput}.video${(plAud.uri ? '' : '.' + streamPath.lang )}`);
dlFailed = !await downloadFile(tsFile, chunkList);
if (!dlFailed) {
if (plAud.uri) {
purvideo.push({
path: `${tsFile}.ts`,
lang: plAud.language
});
} else {
audioAndVideo.push({
path: `${tsFile}.ts`,
lang: streamPath.lang
});
}
}
}
else{
console.log('[INFO] Skip video downloading...\n');
}
audio: if (!argv.noaudio && plAud.uri) {
// download audio
if (audioAndVideo.some(a => a.lang === plAud.language) || puraudio.some(a => a.lang === plAud.language))
break audio;
let reqAudio = await getData({
url: plAud.uri,
useProxy: (argv.ssp ? false : true),
debug: argv.debug,
});
if (!reqAudio.ok) { return; }
let chunkListA = m3u8(reqAudio.res.body);
let tsFileA = path.join(cfg.dir.content, fnOutput + `.audio.${plAud.language}`);
dlFailedA = !await downloadFile(tsFileA, chunkListA);
if (!dlFailedA)
puraudio.push({
path: `${tsFileA}.ts`,
lang: plAud.language
});
}
}
// add subs
let subsExt = !argv.mp4 || argv.mp4 && argv.ass ? '.ass' : '.srt';
let addSubs = true;
// download subtitles
if(stDlPath.length > 0){
console.log('[INFO] Downloading subtitles...');
for (let subObject of stDlPath) {
let subsSrc = await getData({
url: subObject.path,
useProxy: true,
debug: argv.debug,
});
if(subsSrc.ok){
let assData = vttConvert(subsSrc.res.body, (subsExt == '.srt' ? true : false), subObject.langName, argv.fontSize);
subObject.file = path.join(cfg.dir.content, `${fnOutput}.subtitle${subObject.ext}${subsExt}`);
fs.writeFileSync(subObject.file, assData);
}
else{
console.log('[ERROR] Failed to download subtitles!');
addSubs = false;
break;
}
}
if (addSubs)
console.log('[INFO] Subtitles downloaded!');
}
if((puraudio.length < 1 && audioAndVideo.length < 1) || (purvideo.length < 1 && audioAndVideo.length < 1)){
console.log('\n[INFO] Unable to locate a video AND audio file\n');
return;
}
if(argv.skipmux){
console.log('[INFO] Skipping muxing...');
return;
}
// usage
let usableMKVmerge = true;
let usableFFmpeg = true;
// check exec path
let mkvmergebinfile = await lookpath(path.join(cfg.bin.mkvmerge));
let ffmpegbinfile = await lookpath(path.join(cfg.bin.ffmpeg));
// check exec
if( !argv.mp4 && !mkvmergebinfile ){
console.log('[WARN] MKVMerge not found, skip using this...');
usableMKVmerge = false;
}
if( !usableMKVmerge && !ffmpegbinfile || argv.mp4 && !ffmpegbinfile ){
console.log('[WARN] FFmpeg not found, skip using this...');
usableFFmpeg = false;
}
if ( argv.novids ){
console.log('[INFO] Video not downloaded. Skip muxing video.');
}
if(!argv.mp4 && usableMKVmerge){
let ffext = !argv.mp4 ? 'mkv' : 'mp4';
let command = merger.buildCommandMkvMerge(audioAndVideo, purvideo, puraudio, stDlPath, `${path.join(cfg.dir.content, outName)}.${ffext}`);
shlp.exec('mkvmerge', `"${mkvmergebinfile}"`, command);
}
else if(usableFFmpeg){
let ffext = !argv.mp4 ? 'mkv' : 'mp4';
let command = merger.buildCommandFFmpeg(audioAndVideo, purvideo, puraudio, stDlPath, `${path.join(cfg.dir.content, outName)}.${ffext}`);
shlp.exec('ffmpeg',`"${ffmpegbinfile}"`,command);
}
else{
console.log('\n[INFO] Done!\n');
return;
}
if (argv.nocleanup)
return;
audioAndVideo.concat(puraudio).concat(purvideo).forEach(a => fs.unlinkSync(a.path));
stDlPath.forEach(subObject => fs.unlinkSync(subObject.file));
console.log('\n[INFO] Done!\n');
}
async function downloadFile(filename, chunkList) {
let offset = 0;
fileCheck: if (fs.existsSync(filename + '.ts')) {
if (fs.existsSync(filename + '.ts.resume')) {
const resume = JSON.parse(fs.readFileSync(`${filename}.ts.resume`));
if (resume.total === chunkList.segments.length) {
offset = resume.downloaded;
break fileCheck;
}
}
let rwts = await shlp.question(`[Q] File «${filename + '.ts'}» already exists! Rewrite or continue? (y/N/c)`);
rwts = rwts || 'N';
if (['N', 'n'].includes(rwts[0])) {
return false;
} else if (['C', 'c'].includes(rwts[0])) {
return true;
} else {
fs.unlinkSync(filename + '.ts');
}
}
let start = Date.now();
console.log(`[INFO] Started ${filename}.ts`);
for (let i = offset; i < chunkList.segments.length; i+=argv.partsize) {
let cur = [];
for (let a = 0; a < Math.min(argv.partsize, chunkList.segments.length - i); a++) {
cur.push(downloadPart(chunkList.segments[i + a], i + a, chunkList.segments.length)
.catch(e => e));
}
let p = await Promise.all(cur);
if (p.some(el => el instanceof Error)) {
console.log(`[ERROR] An error occured while downloading ${filename}.ts`);
return false;
}
fs.writeFileSync(`${filename}.ts.resume`, JSON.stringify({ total: chunkList.segments.length, downloaded: i + argv.partsize }, null, 4));
for (let a = 0; a < p.length; a++) {
fs.writeFileSync(filename + '.ts', p[a].content, { flag: 'a' });
}
logDownloadInfo(start, i + Math.min(argv.partsize, chunkList.segments.length - i),
chunkList.segments.length, i + Math.min(argv.partsize, chunkList.segments.length - i),
chunkList.segments.length);
}
if (fs.existsSync(`${filename}.ts.resume`))
fs.unlinkSync(`${filename}.ts.resume`);
console.log(`[INFO] Finished ${filename}.ts`);
return true;
}
async function downloadPart(chunk, index) {
let key = await generateCrypto(chunk, index);
let headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:70.0) Gecko/20100101 Firefox/70.0'
};
if (chunk.byterange)
headers.Range = `bytes=${chunk.byterange.offset}-${chunk.byterange.offset+chunk.byterange.length-1}`;
let res = (await got({
url: chunk.uri,
headers,
responseType: 'buffer'
}).catch(error => console.log(`[ERROR] ${error.name}: ${error.code||error.message}`)));
if (!res.body) { return new Error('Invalid State'); }
try {
let dec = key.update(res.body);
dec = Buffer.concat([dec, key.final()]);
return { content: dec, index: index};
} catch (e) { return e; }
}
let keys = {};
async function generateCrypto(chunk, index) {
let key = keys[chunk.key.uri];
if (!key) {
let reqKey = await getData({
url: chunk.key.uri,
responseType: 'buffer'
});
if (!reqKey.ok) { console.log('[ERROR] Can\'t get key'); return; }
key = reqKey.res.body;
keys[chunk.key.uri] = key;
}
let iv = Buffer.alloc(16);
let ivs = chunk.key.iv ? chunk.key.iv : [0, 0, 0, index];
for (let i in ivs) {
iv.writeUInt32BE(ivs[i], i * 4);
}
key = crypto.createDecipheriv('aes-128-cbc', key, iv);
return key;
}
/* Snacked from hls-download */
function logDownloadInfo (dateStart, partsDL, partsTotal, partsDLRes, partsTotalRes) {
const dateElapsed = Date.now() - dateStart;
const percentFxd = (partsDL / partsTotal * 100).toFixed();
const percent = percentFxd < 100 ? percentFxd : (partsTotal == partsDL ? 100 : 99);
const revParts = parseInt(dateElapsed * (partsTotal / partsDL - 1));
const time = shlp.formatTime((revParts / 1000).toFixed());
console.log(`[INFO] ${partsDLRes} of ${partsTotalRes} parts downloaded [${percent}%] (${time})`);
}
/**
* @param {string} input
* @param {string} title
* @param {number|string} episode
* @param {string} showTitle
* @param {number} season
* @param {number} width
* @param {number} height
* @returns {string}
*/
function parseFileName(input, title, episode, showTitle, season, width, height) {
const varRegex = /\${[A-Za-z1-9]+}/g;
const vars = input.match(varRegex);
for (let i = 0; i < vars.length; i++) {
const type = vars[i];
switch (type.slice(2, -1).toLowerCase()) {
case 'title':
input = input.replace(vars[i], title);
break;
case 'episode': {
if (typeof episode === 'number') {
let len = episode.toFixed(0).toString().length;
input = input.replace(vars[i], len < argv.numbers ? '0'.repeat(argv.numbers - len) + episode : episode);
} else {
input = input.replace(vars[i], episode);
}
break;
}
case 'showtitle':
input = input.replace(vars[i], showTitle);
break;
case 'season': {
let len = season.toFixed(0).toString().length;
input = input.replace(vars[i], len < argv.numbers ? '0'.repeat(argv.numbers - len) + season : season);
break;
}
case 'width':
input = input.replace(vars[i], width);
break;
case 'height':
input = input.replace(vars[i], height);
break;
default:
break;
}
}
return shlp.cleanupFilename(input);
}