-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost-yvdl.ts
1365 lines (1035 loc) · 47.7 KB
/
ghost-yvdl.ts
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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
//import { EventEmitter } from "events"
// this would be a tweaked copy of the server side module: yvdl.ts that served as the core in the
//yvdl web project;
// this should provide the same stable API so that the components can use it,
// it must handleparsing, downloading, parsing, managing user data
// and everything
//so here's the deal
// socket.emit, will become this.emit
// this will be an eventemitter, the components will use it
// the this that i'm talking about is not YVDL module, or USERS ,it's User, and BTW no Users class anymore,
// pretty simple huh
// actually i dont need all this hustel
// you know whatn this was a server , which means it was in complete isolation and thus it had to re impliments everything
// but in react native this is not the case
// the components willl do a great job themselves n and this should only provid UTILITY
// utitlitie are : fbParser, mp3converter, ..
// not providing a whole damn structure ok?
// this is just a bench of thrown up functionalities that will come handy when developing components,
// but remùember, the jsx is the reall thing, the reall app , that's where data, and the only data that exixts, flows
// so go ahead and delete things
//12 feb 2022 4:56 the moment i discovered that expo is not e thing, and i was missing the whole awesome react natve debugin : fast loading and stuff
//after a big trugle to get my phone to boot (yeahit might be it's end soon)
//now i'm both happy bout it (fast and much less tedious devoloping process) but also i feel stupid making the expo sync tool around my lack of knowledge
//i'mean it was awesome but i wish it had a real purpose other than me missing all this.. whatever let's get that app polished last things were splash screen wth cat and handling send but not when app is open
const DEV_EXPO = false // disables everything that expo cannot handle: use it to conditionally fake things like rnfs, parsing, converting
const FAKE_DOWNLOAD_INTERVAL = 500
const FAKE_DOWNLOAD_CYCLES = 200
export const DEV_EXPO_MODE : DEV_EXPO_CONCERN = "UI&PARSER" //this key is globally acceced from all modules, dont re-define somewhire else
export type DEV_EXPO_CONCERN = "UI"|"UI&PARSER" //UI would make evrything go dummy allowing fast UI devlepment (no rela fetch operations), while Parser does all the legit fetching and parssing it only dumifies the ffmpeg and downloading operations
import filenamify from 'filenamify/filenamify'; //
//let filenamify = (val)=>val
import { LogLevel, RNFFmpeg, RNFFmpegConfig, Statistics,CompletedExecution, Execution, } from 'react-native-ffmpeg'
import fn from 'react-native-fs'
//import * as fn from './fnAltDeclarations' //todo: develop mi flow expo to automate commenting and uncommenting this
const ExternalStorageDirectoryPath = DEV_EXPO?"/storage/emulated/0": fn.ExternalStorageDirectoryPath
const DOWNLOADS_DIRECTORY = DEV_EXPO?"/storage/emulated/0": fn.DownloadDirectoryPath
//import path from "react-native-path";
const ffmpeg = null
import DOMParser from 'react-native-html-parser';
import miFbParser from './MiFbParser';
import { DashManifestResolved, FBHDUtils, VideoRepresentation } from './FBHDUtils';
//import { CompletedExecution, Execution, Statistics } from './ffmpegAltDeclarations';
//import { LogLevel, RNFFmpeg, RNFFmpegConfig, Statistics,CompletedExecution, Execution, } from './rnffmpegAltDec.mi'
//import { LogLevel, RNFFmpeg, RNFFmpegConfig, Statistics,CompletedExecution, Execution, } from './rnffmpegAltDec'
import {AppSettings, IOutputLocation, IPreferedNameSource, IPreferedQualty, ITitles, IImages, IPostInfo, IConversionProg} from './rev02types';
import { verbo } from './GeneralUtils';
const SAVE_HTML = true
const HTML_OUTPUT = "/storage/emulated/0"+'/YVDL/'+"last-post.html"
const LOG_FILE = "/storage/emulated/0"+'/YVDL/'+"yvdl-v1.0.log"
const OUTPUT_FOLDER = "/storage/emulated/0"+'/YVDL'
const YVDL_CONFIG_FILE ="/storage/emulated/0"+'/YVDL/yvdl.config.json'
type Iyvdl_config = {
localUserOutput?: string;
wake_lock?: boolean;
}
export interface IPostInfo_v02 {
dashManifestObject: DashManifestResolved
titles:ITitles
images:IImages
VideoSize: number
}
export const DefaultAppSettings= {
AutoFillSettings:
{PreferedNameSource:"og-title", EnableThumbnail:true,PreferedVideoQuality:"Highest","OutputLocation":"Downloads"},
"Legacy":{UseLegacyMode:false},
"General":{EnableAutoFill:true}} as AppSettings
async function parse_config(verbose=true):Promise <Iyvdl_config>{
if(/* DEV_EXPO */ true ){return {}}
return( await fn.exists(YVDL_CONFIG_FILE)? JSON.parse( (await fn.readFile(YVDL_CONFIG_FILE))||'{}')
: undefined)
}
function save_config(config:Iyvdl_config,verbose=true){
return fn.writeFile(YVDL_CONFIG_FILE,JSON.stringify(config,null," "))
}
export class YVDL_Task {
cb_statusChange: (redundantStr:string,state:StateObj)=>void;
cb_videoSaved: (redundantStr:string)=>void;
cb_conversionProgress: (sedundantStr:string, id:number, ffmpegProg:Statistics)=>void;
cb_downloadProgress: (sedundantStr:string, dlProg:IDownloadProg)=>void;
cb_failed:(ev,err)=> void;
constructor(query:string,website:string,mp3:boolean,out_path?:string,filename?:string, appSettings?:AppSettings){
//static ctor time
this.isLegacy=(appSettings?.Legacy?.UseLegacyMode===false)?false:true
this.my_url= query
this.my_website=new Website(website)
this.my_id= idGen(8)
this.key= this.my_id+""
this.mp3conversion= mp3
this.taskFormat = mp3?"MP3":"MP4"
this.capturedAppSettings = appSettings
//
this.status='zero'
this.outputFileName= filename ||"construction time filename"
this.fullOutputFileName = ""
this.outputPath=out_path || (appSettings.AutoFillSettings.OutputLocation=="YVDLFolder"? ExternalStorageDirectoryPath+"/YVDL" :DOWNLOADS_DIRECTORY)
this.download_prog = {d:0,t:0}
verbo(" yvdl consturced")
}
//#static construction time properties
my_url:string
isLegacy:boolean //02rev this keeps most of the old look and behavior
taskFormat:yvdl_task_format
key:string //required by react's flat list
my_id:number
my_website: Website
capturedAppSettings:AppSettings //todo: make sure this is b value cloned .. to make sure settings that are altered later wont affect the task in it's life time
mp3conversion: boolean// will be deprecated
uid : string //will be deprecated
//#static later added properties
post_info:IPostInfo //will be deprecated
post_info_v02:IPostInfo_v02
outputPath : string
outputFileName:string
ffmpegExecutionId:number //deprecate because there is the ffExcecution
ffExcecution:Execution
ffCompletedExecution:CompletedExecution
chosenVideoRepresentation:VideoRepresentation
chosenThumbnailUrl:string
chosenTitle:string
failurReason : string //refers to definitive faulire leading to a dead end
fullOutputFileName:string //02rev the definitive output filename
//dynamic properties
status: yvdl_task_status
download_prog:IDownloadProg
conversion_prog:IConversionProg
ffStatistics:Statistics
currentState():StateObj{
return new StateObj ({status:this.status,id:this.my_id,uid:this.uid, post_info:this.post_info,
post_info_v02:this.post_info_v02
,mp3:this.mp3conversion,download_prog:this.download_prog,converting_prog:this.conversion_prog,
outputmp4: this.outputFileName,
outputmp3: this.outputFileName.slice(0,this.outputFileName.length-4)+".mp3",
failurReason:this.failurReason,
})
}
onDownloadProg(prog:number,t:number){// only sends the event this doeant take car of the console bar
this.download_prog.d = prog
this.download_prog.t = t
this.cb_downloadProgress("downloadProgress",this.download_prog)
}
onConversionProg(ffmpeg_prog:Statistics){
this.conversion_prog= ffmpeg_prog
verbo("here, "+ ffmpeg_prog.time)
this.cb_conversionProgress&& this.cb_conversionProgress("conversionProgress",this.my_id, ffmpeg_prog)
}
//02rev this is used by go.. i , got it out of go's body to clean code
fake_downloader(options:{progress:(res:{bytesWritten:number,contentLength:number})=>void}) {
return{ promise: new Promise((resolve,reject)=>{
let cc= 0
let inv = setInterval(()=>{
cc++
options.progress({bytesWritten:cc*33000000/10,contentLength:33000000})
if(cc==10){
clearInterval(inv)
resolve({statusCode:200})
}
},200)
}) , othe:"iyub"}
}
//pass legit urls so that you can see them in logs for testing
fake_downloader_v02(options:{progress:(res:{bytesWritten:number,contentLength:number,stat_size:number,stat_speed:number})=>void,thumbnailUrl:string,audioUrl:string,fileName:string,postUrlForMeta:string}): Promise<{ffmpgExitCode:number}> {
verbo("fake_downloader_v02 here's the passed args:")
verbo("thumbnailUrl:"+options.thumbnailUrl)
verbo("audioUrl:"+options.audioUrl)
verbo("fileName:"+options.fileName)
return new Promise<{ffmpgExitCode:number}>((resolve,reject)=>{
let cc= 0
let inv = setInterval(()=>{
cc++
let downloaded=cc*33000000/FAKE_DOWNLOAD_CYCLES
options.progress({bytesWritten:downloaded,contentLength:33000000,stat_size:downloaded,stat_speed:55})
if(cc==FAKE_DOWNLOAD_CYCLES){
clearInterval(inv)
resolve({ffmpgExitCode:0})
}
},FAKE_DOWNLOAD_INTERVAL)
})
}
/**
* auto fills properties like chosn video and titles.. and stores the n the yvdl propertes
* this ensures consistency along lifetime and also avoiding doing the computations multiple times
*/
AutoFill(){
this.chosenVideoRepresentation=FBHDUtils.AutoDetermineVideoRepresentation(this.post_info_v02.dashManifestObject, this.capturedAppSettings)
this.chosenThumbnailUrl=this.autoChoseImage();
this.chosenTitle=this.autoChoseTitle();
this.fullOutputFileName = this.outputPath + "/" + filenamify(this.chosenTitle) + (this.taskFormat=="MP3"?".mp3":".mp4")
}
/**
* 02rev auto-fill image determination
*/
autoChoseImage():string{
return this.post_info_v02.images.ogImage||this.post_info_v02.images.thumbnailUrl||this.post_info_v02.images.thumbnailImage
}
/**
* 02rev auto-fill title determination
*/
autoChoseTitle():string{
let prefered= this.capturedAppSettings.AutoFillSettings.PreferedNameSource
let res = (prefered=="og-title"?this.post_info_v02.titles.ogTitle:
prefered=="description"?this.post_info_v02.titles.descriptionTitle:
prefered=="title"?this.post_info_v02.titles.nameTitle:undefined)
||this.post_info_v02.titles.ogTitle||this.post_info_v02.titles.nameTitle||this.post_info_v02.titles.descriptionTitle||"all 3 titles were falsy!"
verbo("prefered: "+prefered+" auto chosed title is: "+res)
return res
}
/**
* Start downloading, and then converting if mp3 is set
* //02rev this seems to be fired by the tascCard's componentDidMount event.. and only by that
*/
Go() {
verbo("GO:::: id:"+this.my_id+", status:"+this.status)
if(this.status!="pending"){
throw new Error("cannot call Go on task '"+this.my_id+"' with status '"+this.status+"'")
}
if (this.isLegacy) {
//#legacy approach untouched:
let url = this.post_info.VideoUrl
verbo("saving vd " + url + " to: " + this.outputPath + "/" + this.outputFileName + ".mp4")
let downloader
if (DEV_EXPO) {
downloader = this.fake_downloader
}
else {
downloader = fn.downloadFile
}
downloader({
fromUrl: url, toFile: this.outputPath + "/" + this.outputFileName + ".mp4",
progress: (res) => { this.onDownloadProg(res.bytesWritten, res.contentLength) }
}).promise
.then((dres) => {
if (dres.statusCode !== 200) {
this.status = "failed"
this.failurReason = "downloead res status code is " + dres.statusCode
this.cb_statusChange('statuchange', this.currentState())
return
}
this.cb_videoSaved("videoSaved")
if (this.mp3conversion) {
this.Go_Conversion();
}
else {
this.status = "done"
this.cb_statusChange('statusChange', this.currentState())
savePage()
}
})
.catch((err) => {
this.cb_failed("failed", err)
})
}
else if (this.isLegacy == false) {
//# fbhd approach:
verbo("non-legacy Go function here:")
let downloadMethod: (obj: any) => Promise<{ ffmpgExitCode: number }>
if ((DEV_EXPO == false)) {
verbo("real donloader was chosed")
if (this.taskFormat == "MP3")
downloadMethod = this.fbhd_mp3_ffmpeg_downloader
else if (this.taskFormat == "MP4")
downloadMethod = this.fbhd_mp4_ffmpeg_downloader
}
else {
verbo("fake donloader was chosed")
downloadMethod = this.fake_downloader_v02;
}
let promise: Promise<{ ffmpgExitCode: number; }> //general for both formats .. to avoide repearting the same pieces of code for mp4
if (this.taskFormat == "MP3") {
promise =
downloadMethod({
audioUrl: this.post_info_v02.dashManifestObject.AudioRepresentations[0].BaseUrl,
thumbnailUrl: this.chosenThumbnailUrl,
fileName: this.fullOutputFileName,//note that the autofil takes care of the extension as part of the full flename but that might get awkward as more futures such as folow-up-tasks are introduced
postUrlForMeta: this.my_url,
postInf: this.post_info_v02,
ExecutionObtained: (exec) => { this.ffExcecution=exec; verbo("ecex obtained: "); verbo(JSON.stringify(exec)) },
progress: (res) => {
if(res.executionId===this.ffExcecution?.executionId){//ensure that the event corresponds to this task and not some other ongoing task
this.onDownloadProg(res.bytesWritten, res.contentLength)
}
}
})
}
else if (this.taskFormat == "MP4") {
verbo("chosen vid rep :")
verbo(this.chosenVideoRepresentation.FBQualityLabel)
promise =
downloadMethod({
audioUrl: this.post_info_v02.dashManifestObject.AudioRepresentations[0].BaseUrl,
thumbnailUrl: this.chosenThumbnailUrl,
videoUrl: this.chosenVideoRepresentation.BaseUrl,
fileName: this.fullOutputFileName,
postUrlForMeta: this.my_url,
postInf: this.post_info_v02,
ExecutionObtained: (exec) => { this.ffExcecution=exec; verbo("ecex obtained: "); verbo(JSON.stringify(exec)) },
progress: (res) => {
if(res.executionId===this.ffExcecution?.executionId){//ensure that the event corresponds to this task and not some other ongoing task
this.onDownloadProg(res.bytesWritten, res.contentLength)
} }
})
}
this.status="downloading"
promise.then((dres) => {
if (dres.ffmpgExitCode == 255) {
//canceled?
this.cb_videoSaved("videoSaved")
this.status = "done"
this.cb_statusChange('statusChange', this.currentState())
savePage()
//todo: specify that it's canceled and not completed through some more state and ui
}
else if (dres.ffmpgExitCode == 0) {
this.cb_videoSaved("videoSaved")
this.status = "done"
this.cb_statusChange('statusChange', this.currentState())
savePage()
return
}
else {
this.status = "failed"
this.failurReason = "return code code is not 0 :" + dres.ffmpgExitCode
alert("failed to 0")
this.cb_statusChange('statuchange', this.currentState())
return
}
})
.catch((err) => {
alert("failed todownload")
this.status = "failed"
this.failurReason = err.message || err
this.cb_statusChange('statuchange', this.currentState())
return
})
}
}
tryCancelingDownload(){
verbo("try canceling")
verbo(" canceling excecution id:"+this.ffExcecution.executionId)
if(DEV_EXPO){
alert("dev no canceling")
return
}
RNFFmpeg.cancelExecution(this.ffExcecution.executionId)
}
/**
* runs ffmpeg , the native one ; idk why i'm keeping the web server one, //23-dec-2020 they're comming, //02rev 7feb because it's awesome code.. remember some details about the epic 2020 coding up there, sorry to delete it for code readability
* ive been strugling to get this wierd phone ffmpeg to work and i did my first mp3 conversion which was easy but confusing
*
* ok bal bla bla shut up and work
* got this to work at around 5:00 pm was easy peasy, just used the same old args
* problem is, this seems kinda slowish, i'm not inmpressed by the speed and the bundle size
* is just the worst i hope that 75mb will enable me to add shit ton of features without more bundeling
*
*
*
* you don't wanna deal with this, use the go_conversion
* @param input the input file name and path
* @param output the output file name and path
* @param onProgress the progress callback, takes the native ffmpeg progress object
* @returns promise the string is just the output filename so u dont have to use is
*/
async mp3Converter_miracles(input:string,output:string,onProgress:(stats:Statistics)=>void):Promise<string>{
return new Promise((resolve,reject)=>{
//fake
if(DEV_EXPO){
setTimeout(() => {
resolve("success")
}, 6000);
return
}
verbo("running ffmpeg ")
let mp3inp = input
let thumb_url = this.post_info.ThumbnailUrl.replace(/https/,"http")
RNFFmpegConfig.enableStatisticsCallback((stats)=>{
console.log(stats.size+" pppppppppppp "+ stats.time)
onProgress(stats)})
RNFFmpeg.executeWithArguments([
//input 1 : the video
"-y",
"-vn" ,
'-i', mp3inp,
//input 2 the thumbnail (it's otions are befor it)
'-f', 'mjpeg',
"-i" , thumb_url,
'-map', '0',
'-map', '1',
'-c:v', 'mjpeg',
'-c:a:0', 'mp3',
'-disposition:v:1', 'attached_pic',
'-id3v2_version', '3',
'-write_id3v1', '1',
'-metadata', 'title=',
'-metadata', 'comment=YassVideoDownloader> " ' +this.my_url + '"',
'-metadata', 'album=YassVideoDownloader>"' +this.my_url + '"',
'-metadata:s:v', 'comment=Cover (front)',
'-metadata:s:v', 'title=Cover (front)',
output
])
.then(result => {
console.log(`FFmpeg process exited with rc=${result}.`)
if(result==0){
verbo('mp3Converter-miracles: ffmpeg finished processing',3);
resolve('success')
}
})
return
let escaped_url = this.my_url.replace(/\\/g,"\\\\")
ffmpeg()
.on('progress', function(progress:any) {
onProgress(progress)
})
})
}
/**
*
* 02rev runs ffmpeg to download audio //02-feb-2022
* @param audioUrl
* @param thumbnail if undefined there will be no thumbnail
* @param output full filename with extension
* @param onProgress
* @returns
*/
async fbhd_mp3_ffmpeg_downloader(options:{progress:(res:{bytesWritten:number,contentLength:number,executionId:number})=>void,
thumbnailUrl:string,audioUrl:string,fileName:string,postUrlForMeta:string,postInf:IPostInfo_v02,
ExecutionObtained:(exec:Execution)=>void}):Promise<{ffmpgExitCode:number}>{
return new Promise<{ffmpgExitCode:number}>((resolve,reject)=>{
verbo("running ffmpeg ")
verbo("fbhd_mp3_ffmpeg_downloader here's the passed args:")
verbo("thumbnailUrl:"+options.thumbnailUrl)
verbo("audioUrl:"+options.audioUrl)
verbo("fileName:"+options.fileName)
let thumb_url = options.thumbnailUrl.replace(/https/,"http")
let audUrl= options.audioUrl.replace(/https/,"http")
let totalDuration = (options.postInf.dashManifestObject.Duration.getTime()-new Date(0,0,0,0,0,0,0).getTime()) // in terms of millisecond/10, just like ffmpeg stats returns it
RNFFmpegConfig.enableStatisticsCallback((stats:Statistics)=>{
options.progress({bytesWritten:stats.time,contentLength:totalDuration,executionId:stats.executionId})
})//todo adabt from ffmpeg stts to what's eccpected
//whatfbhd literally does for mp3:
//ffmpeg -v 32 -multiple_requests 1 -hide_banner -vn -i "audUrlGoesHere"
// -an -i "imgUrlGoesHere"
// -map 0 -map 1 -c:v:0 mjpeg -disposition:v:0 attached_pic -metadata title=""
//-metadata:s:v "comment='Cover (front)'" -metadata:s:v "title='Cover (front)'"
// "E:\TOOLS\endof2021 classical music\Summer Was Fun & Laura Brehm - Prism [NCS Release].mp3" -y
RNFFmpeg.executeAsyncWithArguments([
//input 1 : the video
"-multiple_requests","1",
"-hide_banner",
"-vn" ,
'-i', audUrl,
//input 2 the thumbnail (it's otions are befor it)
'-an',
"-i" , thumb_url,
'-map', '0',
'-map', '1',
'-c:v:0', 'mjpeg',
'-disposition:v:0', 'attached_pic',
'-id3v2_version', '3',
'-write_id3v1', '1',
'-metadata', 'title=',
'-metadata', 'comment=YassVideoDownloader> " ' +options.postUrlForMeta + '"',
'-metadata', 'album=YassVideoDownloader>"' +options.postUrlForMeta + '"',
'-metadata:s:v', 'comment=Cover (front)',
'-metadata:s:v', 'title=Cover (front)',
options.fileName , "-y"
],(completedExecution)=>{
//do the rc checking here
console.log(`FFmpeg process exited with rc=${completedExecution.returnCode}.`)
if(completedExecution.returnCode==0||completedExecution.returnCode==255){//hope 255 only means canceled by user
verbo("scaning")
fn.scanFile(options.fileName).then((res)=>{
console.log("scaning done : ",res)
})
resolve({ffmpgExitCode:completedExecution.returnCode})
}
else{
reject("non zero exit code")
}
})
.then(executionId => {
RNFFmpeg.listExecutions().then(executionList => {
let executionObj= executionList.find(ex=>(ex as Execution).executionId==executionId)
options.ExecutionObtained&& options.ExecutionObtained(executionObj);
});
})
return
})
}
/**
* * 02rev runs ffmpeg to download vid //03-feb-2022
* @param options
* @returns
*/
async fbhd_mp4_ffmpeg_downloader(options:{progress:(res:{bytesWritten:number,contentLength:number,executionId:number})=>void,
thumbnailUrl:string,audioUrl:string,videoUrl,fileName:string,postUrlForMeta:string,
postInf:IPostInfo_v02,ExecutionObtained:(exec:Execution)=>void}):Promise<{ffmpgExitCode:number}>{
return new Promise<{ffmpgExitCode:number}>((resolve,reject)=>{
verbo("running ffmpeg for mp4 downloading, the passed args:")
verbo("thumbnailUrl:"+options.thumbnailUrl)
verbo("audioUrl:"+options.audioUrl)
verbo("vdUrl:"+options.videoUrl)
verbo("fileName:"+options.fileName)
let thumb_url = options.thumbnailUrl.replace(/https/,"http")
let audUrl= options.audioUrl.replace(/https/,"http")
let vidUrl= options.videoUrl.replace(/https/,"http")
let totalDuration = (options.postInf.dashManifestObject.Duration.getTime()-new Date(0,0,0,0,0,0,0).getTime()) // in terms of millisecond/10, just like ffmpeg stats returns it
RNFFmpegConfig.enableStatisticsCallback((stats:Statistics)=>{
options.progress({bytesWritten:stats.time,contentLength:totalDuration,executionId:stats.executionId})
})//todo adabt from ffmpeg stts to what's eccpected
//whatfbhd literally does for mp4 taks:
//ffmpeg -v 32 -multiple_requests 1 -vn -i "audGoesHere"
// -an -i "thumbGoesHere"
// -an -i "vidGoesHere"
// -map 0 -map 1 -map 2 -c:v:0 mjpeg -disposition:v:0 attached_pic -metadata title=""
// -metadata:s:v:0 "comment='Cover (front)'" -metadata:s:v:0 "title='Cover (front)'"
// "fullFilenAME.mp4" -y
RNFFmpeg.executeAsyncWithArguments([
//input 1 : the AUDIO
"-multiple_requests","1",
//"-hide_banner",
"-vn" ,
'-i', audUrl,
//input 2 the thumbnail
'-an',
"-i" , thumb_url,
//INPUT3 THE VID
'-an',
"-i" , vidUrl,
'-map', '0',
'-map', '1',
'-map', '2',
'-c:v:0', 'mjpeg',
'-c:v:1', 'copy',//02rev this is my (06-feb) solution attempt to the low quality prblem.. it was using the mpeg4 codec which is bad
'-disposition:v:0', 'attached_pic',
'-id3v2_version', '3',
'-write_id3v1', '1',
'-metadata', 'title=',
'-metadata', 'comment=YassVideoDownloader> " ' +options.postUrlForMeta + '"',
'-metadata', 'album=YassVideoDownloader>"' +options.postUrlForMeta + '"',
'-metadata:s:v:0', 'comment=Cover (front)',
'-metadata:s:v:0', 'title=Cover (front)',
options.fileName , "-y"
],(completedExecution)=>{
//do the rc checking here
console.log(`FFmpeg process exited with rc=${completedExecution.returnCode}.`)
if(completedExecution.returnCode==0||completedExecution.returnCode==255){//hope 255 only means canceled by user
verbo("scaning")
fn.scanFile(options.fileName).then((res)=>{
console.log("scaning done : ",res)
})
resolve({ffmpgExitCode:completedExecution.returnCode})
}
else{
reject("non zero exit code")
}
})
.then(executionId => {
verbo("then here")
RNFFmpeg.listExecutions().then(executionList => {
verbo("listing then here")
let executionObj= executionList.find(ex=>(ex as Execution).executionId==executionId)
options.ExecutionObtained&& options.ExecutionObtained(executionObj);
});
})
return
})
}
/**
* start converting, this only requires an existent mp4 to work on, that means it can be called
externaly at any later time (no mp3 flag is involved )
*/
async Go_Conversion(){
DEV_EXPO && verbo("dev_expo: canceling checking for mp4 exist")
if ( (!DEV_EXPO)&& (await fn.exists(( this.outputPath+"/"+ this.outputFileName+ ".mp4"))==false)){
// this.emit("error", "Go_Conversion:: couldnt find file, something went wrong") lets use the status change instead
this.status="failed"
this.failurReason = 'couldnt find mp4 file, try restarting the task'
this.cb_statusChange("statusChange",this.currentState())
savePage()
return
}
this.status= "converting"
this.cb_statusChange("statusChange",this.currentState())
savePage()
// mocked converting
verbo(" converting",4)
let convert = await this.mp3Converter_miracles( (this.outputPath+"/"+this.outputFileName+".mp4") ,
(this.outputPath+"/"+this.outputFileName+".mp3" ) ,
((progress:Statistics)=>{
this.onConversionProg(progress)
}).bind(this)
)
if(convert!=='success'){
// case converting failed
this.status = "failed"
this.failurReason = 'conversion failed'
this.cb_statusChange("statusChange",this.currentState())
savePage()
return
}
verbo("scaning")
fn.scanFile(this.outputPath+"/"+this.outputFileName+".mp3").then((res)=>{
console.log("scaning done : ",res)
})
this.status= "done"
this.cb_statusChange("statusChange",this.currentState())
savePage()
return
}
/**
* 02rev
* this is not used.. it was forgotten and another set of function was written
* start downloading the audio with an underlying ffmpeg utility,
*/
async Go_MP3_auto(){
this.status= "downloading"
this.cb_statusChange("statusChange",this.currentState())
let convert = await this.mp3Converter_miracles( (this.outputPath+"/"+this.outputFileName+".mp4") ,
(this.outputPath+"/"+this.outputFileName+".mp3" ) ,
((progress:Statistics)=>{
this.onConversionProg(progress)
}).bind(this)
)
if(convert!=='success'){
// case converting failed
this.status = "failed"
this.failurReason = 'conversion failed'
this.cb_statusChange("statusChange",this.currentState())
savePage()
return
}
this.status= "done"
this.cb_statusChange("statusChange",this.currentState())
savePage()
return
}
/**
* Initialize the task by fetching post info, like the older yass2049 approach, but with significant tweaks
*/
Init(options?:{name:string}):Promise<StateObj>{
return new Promise(async (resolve,reject)=>{
if(this.my_website.veryfi_url(this.my_url)!==true){
reject (new Error("entered url is not valide for the website "+ this.my_website.name ));
}
verbo('valid url ' + this.my_url,4,false)
if(this.isLegacy){
//#legacy mode.. untouched code
let page_content
try {
verbo("fetching "+this.my_url )
DEV_EXPO && verbo("DEV_EXPO: faking the fetch " )
page_content = {
text:()=>" fake content"
}
if (!DEV_EXPO){
page_content = await fetch(this.my_url)
}
verbo("page fetched")
}
catch(err){
//fetching_post_spinner.stop()
verbo(c("red", "error: couldnt fetch post content this err happened: ")+ c ("red",err.message))
reject(err)
return
}
page_content = await page_content.text()
verbo( "page content lengh is : "+ page_content.length)
DEV_EXPO && verbo("dev_expo: canceling save html")
if( (!DEV_EXPO)&&(SAVE_HTML)){
fn.writeFile(HTML_OUTPUT,page_content)
//fetching_post_spinner.info("html saved at " + c("cyan",HTML_OUTPUT))
}
let Parser = this.my_website.name=="facebook"?FbParser:this.my_website.name=="instagram"?igParser:FbParser
verbo("used parser is : " + c("cyan", Parser.name) + " website.name is : " + this.my_website.name,2)
Parser(page_content)
.then((post_inf)=>{
this.post_info=post_inf
this.status = "pending" // no statechange emiting before the init promise resolves
this.outputFileName = filenamify( post_inf.Title)
resolve (this.currentState())
savePage()
return this
})
.catch(err=>{
verbo(c("red","parser rejected: ", err.message ) )
reject(err)
}) // just pass the same error from the fbParser methode to whoever called this
}
else if(this.isLegacy==false){
//#rev2 mode which is the fbhd approach. using the FBHDUtiles.ts as helper
let page_content : string
try {
verbo("fetching "+this.my_url )
if (DEV_EXPO&&(DEV_EXPO_MODE=="UI" ) ){
verbo("DEV_EXPO: faking the fetch " )
page_content = " fake content"
}
else{
verbo("real fecth operaation")
page_content = await FBHDUtils.FetchVideoPage(this.my_url)
}
verbo("page fetched")
}
catch(err){
//fetching_post_spinner.stop()
verbo(c("red", "error: couldnt fetch post content this err happened: ")+ c ("red",err.message))
reject(err)
return
}
verbo( "page content lengh is : "+ page_content.length)
DEV_EXPO && verbo("dev_expo: canceling save html")
if( (!DEV_EXPO)&&(SAVE_HTML)){
fn.writeFile(HTML_OUTPUT,page_content)
//fetching_post_spinner.info("html saved at " + c("cyan",HTML_OUTPUT))
}
FbParserv02(page_content)
.then((post_inf:IPostInfo_v02)=>{
this.post_info_v02=post_inf
this.status = "pending" // no statechange emiting before the init promise resolves
this.AutoFill()
resolve (this.currentState())
//outpfile name assigning was thre
savePage()
return this
})
.catch(err=>{
verbo(c("red","parser rejected: ", err.message ) )
reject(err)
}) // just pass the same error from the fbParser methode to whoever called this
}
})
}
}
class Website {
constructor(name:string){
this.name = name
}
name :string
/**
*
* @param url url to test
* @returns true: valid url, false: unvalid url
*/
veryfi_url(url:string) :boolean {
return true
}
}
/**
* Based on the awesom: MiFbParser.ts , stable, bug-free, and just works be aware of the VideoSize option
* Resolves the raw fb page content to a IPostInfo object , optionally including the VideoSize which woiuld requere a connection to take place (Head)
* @param date raw html content
*/
function FbParser (data:string ):Promise<IPostInfo> {
verbo("fbparser was called with content lenght: " + data.length)
return miFbParser(data,{fetch_video_size:false})
}
/**
* 02rev keeping much of the logic .. but going fbhd
* @param date raw html content
*/
function FbParserv02 (data:string ):Promise<IPostInfo_v02> {
verbo("fbparserv02 was called with content lenght: " + data.length)
return new Promise((resolve,reject)=>{
if(FBHDUtils.ExractIsSomethingWentWrongPageFromPage(data)){
reject("Server responded with something went wrong page")
return
}
if(FBHDUtils.ExractIsPrivateFromPage(data)){
reject("This video is private")
return
}
let rawDashManif = FBHDUtils. ExractDashManifestRawContentFromPage(data)
if(!rawDashManif){
reject("couldnt find dashmanifest in the page (page content:"+data.length+")")
return
}
let manifestObj = new DashManifestResolved(rawDashManif)
if(!manifestObj){
reject("something went wrong parsing dashmanifest")
return
}
verbo("passed DashManifest parsing")
let titles = { } as ITitles
titles.ogTitle= FBHDUtils.ExractOgtitleFromPage(data)
verbo("passed ogTitle parsing")
titles.nameTitle= FBHDUtils.ExractNametitleFromPage(data)
verbo("passed nameTitle parsing")
titles.descriptionTitle= FBHDUtils.ExractDescriptiontitleFromPage(data)
verbo("passed descriptionTitle parsing")
verbo("passed titles parsing: "+JSON.stringify(titles))