-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_functions.ytdl
862 lines (821 loc) · 31.7 KB
/
.bash_functions.ytdl
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
# vim: ft=bash noet:
set +x
! declare 2>&1 | grep -wq ^colors= && [ $BASH_VERSINFO -ge 4 ] && source $initDir/.colors
test "$debug" -gt 0 && echo "=> Running $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal ..."
#trap 'rc=130;set +x;echo "=> ${BASH_SOURCE[0]}: CTRL+C Interruption trapped.">&2;return $rc' INT
test "$debug" -gt 0 && Echo "\n=> \${BASH_SOURCE[*]} = ${BASH_SOURCE[*]}\n"
if ! type -P youtube-dl >/dev/null 2>&1 && ! type -P yt-dlp >/dev/null 2>&1;then
echo "=> ERROR: <youtube-dl> is not installed, type \"sudo -H pip3 install -U youtube-dl\" to install it" >&2; return 1
echo "=> ERROR: <yt-dlp> is not installed, type \"sudo -H pip3 install -U yt-dlp\" to install it" >&2; return 1
fi
test $osFamily = Linux && export locate="command locate"
test $osFamily = Darwin && export locate="time -p command glocate"
function YTRenameFromFiles {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
# local file="$1"
for file
do
echo "=> file = $file"
echo $file | grep -q __ || continue
id=$(echo $file | awk -F"__|[.]" '{print$(NF-1)}')
format=$(echo $file | awk -F__ '{print$(NF-1)}')
# url="$2"
# test $# != 2 && echo "=> Usage: $FUNCNAME file youtube-URL" && return 1
newName="$(youtube-dl -f "$format" --get-filename -- $id)"
test $newName || continue
# mp4tags -c "$(youtube-dl --get-description "$url")" "$file"
# mp4tags -m "$url" "$file"
test "$(basename $file)" != "$newName" && mv -v "$file" "$(dirname $file)/$newName"
done
trap - INT
}
function YTRenameFromURLs {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
# local URL="$1"
for URL
do
echo "=> URL = $URL"
# format=$(youtube-dl --get-format $URL)
format=18
newName="$(youtube-dl -f "$format" --get-filename -- $URL)"
test $newName || { echo "=> ERROR : This URL contains no file to download." >&2;continue; }
test -s $newName && echo "=> File Already renamed to <$newName>." >&2 && continue
oldName="$(youtube-dl -f "$format" --get-filename -o "%(title)s.%(ext)s" -- $URL)"
test -s $oldName || { echo "=> This URL has not been downloaded" >&2;continue; }
mv -v "$oldName" "$newName"
done
trap - INT
}
function addYoutubeDescriptionToVideoFromURL {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local video="$1"
local url="$2"
test $# != 2 && echo "=> Usage: $FUNCNAME video youtube-URL" && return 1
youtube-dl -qs -- "https://www.youtube.com/watch?v=$url" 2>/dev/null && url="https://www.youtube.com/watch?v=$url"
mp4tags -c "$(youtube-dl --get-description "$url")" "$video"
mp4tags -m "$url" "$video"
# artworkURL=$(youtube-dl --list-thumbnails "$url" | awk '/http/{print$NF}')
# wget -nv "$artworkURL"
# artworkFile=$(basename "$artworkURL")
# AtomicParsley "$video" --artwork "$artworkFile" --overWrite
# rm "$artworkFile"
chmod -w "$video"
trap - INT
}
function continueYoutube_Download {
for videoFile
do
format=$(echo $videoFile | awk -F__ '{print$(NF-1)}')
url=$(echo $videoFile | awk -F"__|[.]" '{print$(NF-2)}')
echo "=> url = <$url>"
time youtube-dl -f "$format" -qs -- "$url" 2>&1 | \grep --color -A1 ^ERROR: && echo && continue
cd $(dirname $videoFile) && pwd
getRestrictedFilenamesFORMAT "$format" $url
cd - >/dev/null
done
}
function getAliveURLsFromFile {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local listOfUrls=""
for file
do
echo "=> Checking if urls in \"$file\" are alive ..." >&2
echo >&2
urls=$(awk '{printf$1" "}' $file)
for url in $urls
do
printf "=> Testing $url ..." >&2
\curl -o /dev/null -Lsw "%{http_code}\n" $url | \egrep -q "^(200|301)$" && echo OK >&2 && listOfUrls="$listOfUrls $url" || echo DOWN >&2
done
echo >&2
done
echo $listOfUrls
trap - INT
}
function getFullURLsFromIDs {
local youtubeURLPrefix=https://www.youtube.com/watch?v=
local dailymotionURLPrefix=https://www.dailymotion.com/video/
local vimeoURLPrefix=https://vimeo.com/
local prefix="not_yet_known"
[ $# -le 1 ] && {
echo "=> Usage : $FUNCNAME youtube|dailymotion|vimeo|... ID1 [ID2] [ID3] ..." >&2
return 1
}
local service=$1
shift
case $service in
youtube) prefix=$youtubeURLPrefix;;
dailymotion) prefix=$dailymotionURLPrefix;;
vimeo) prefix=$vimeoURLPrefix;;
*) prefix="unknown_service";echo "=> ERROR : The service <$service> is not known, therefore the prefix is : <$prefix>.">&2;echo $prefix;return 2;;
esac
set -- "${@/#/$prefix}" # Add $prefix to all arguments
echo "$@"
}
function getPlaylistsRestrictedFilenamesFSD {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local playList playListTitle="not_yet_known" listOfUrls="not_yet_known" fqdn="not_yet_known" service="not_yet_known"
for playList
do
echo "=> Treating playList : $playList ..." >&2
playListTitle=$(\youtube-dl --ignore-config --ignore-errors --flat-playlist -J $playList | jq -r '.title')
playListTitle="${playListTitle// /_}"
mkdir -pv $playListTitle && cd $playListTitle
if echo $playList | \egrep -q "youtube|dailymotion|vevo|tonvid"
then
listOfUrls=$(hxwls 2>/dev/null "$playList" | awk -F '&' '/\<(watch|video)\>/{print$1}' | sort -u | paste -sd" ")
test $? != 0 && return
else
fqdn=$(echo "$playList" | sed "s|https\?://||" | cut -d/ -f1)
service=$(echo $fqdn | awk -F. '{print$(NF-1)}')
listOfUrls=$(time getVideoURLsFromVideoIDs $service $(ytdlGetVideoIDsFromPlayListURL $playList))
fi
getRestrictedFilenamesFSD $listOfUrls
cd - >/dev/null
done
trap - INT
}
function getPlaylistsRestrictedFilenamesFORMAT {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local playList playListTitle="not_yet_known" listOfUrls="not_yet_known" fqdn="not_yet_known" service="not_yet_known"
local format=unset
test $# -gt 1 && format=$1 && shift
for playList
do
echo "=> Treating playList : $playList ..." >&2
playListTitle=$(\youtube-dl --ignore-config --ignore-errors --flat-playlist -J $playList | jq -r '.title')
playListTitle="${playListTitle// /_}"
mkdir -pv $playListTitle && cd $playListTitle
if echo $playList | \egrep -q "youtube|dailymotion|vevo|tonvid"
then
listOfUrls=$(hxwls 2>/dev/null "$playList" | awk -F '&' '/\<(watch|video)\>/{print$1}' | sort -u | paste -sd" ")
test $? != 0 && return
else
fqdn=$(echo "$playList" | sed "s|https\?://||" | cut -d/ -f1)
service=$(echo $fqdn | awk -F. '{print$(NF-1)}')
listOfUrls=$(time getVideoURLsFromVideoIDs $service $(ytdlGetVideoIDsFromPlayListURL $playList))
fi
getRestrictedFilenamesFORMAT $format $listOfUrls
cd - >/dev/null
done
trap - INT
}
function getPlaylistsRestrictedFilenamesHD {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local playList playListTitle="not_yet_known" listOfUrls="not_yet_known" fqdn="not_yet_known" service="not_yet_known"
for playList
do
echo "=> Treating playList : $playList ..." >&2
playListTitle=$(\youtube-dl --ignore-config --ignore-errors --flat-playlist -J $playList | jq -r '.title')
playListTitle="${playListTitle// /_}"
mkdir -pv $playListTitle && cd $playListTitle
if echo $playList | \egrep -q "youtube|dailymotion|vevo|tonvid"
then
listOfUrls=$(hxwls 2>/dev/null "$playList" | awk -F '&' '/\<(watch|video)\>/{print$1}' | sort -u | paste -sd" ")
else
fqdn=$(echo "$playList" | sed "s|https\?://||" | cut -d/ -f1)
service=$(echo $fqdn | awk -F. '{print$(NF-1)}')
listOfUrls=$(time getVideoURLsFromVideoIDs $service $(ytdlGetVideoIDsFromPlayListURL $playList))
fi
getRestrictedFilenamesHD $listOfUrls
cd - >/dev/null
done
trap - INT
}
function getPlaylistsRestrictedFilenamesSD {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local playList playListTitle="not_yet_known" listOfUrls="not_yet_known" fqdn="not_yet_known" service="not_yet_known"
for playList
do
echo "=> Treating playList : $playList ..." >&2
playListTitle=$(\youtube-dl --ignore-config --ignore-errors --flat-playlist -J $playList | jq -r '.title')
playListTitle="${playListTitle// /_}"
mkdir -pv $playListTitle && cd $playListTitle
if echo $playList | \egrep -q "youtube|dailymotion|vevo|tonvid"
then
listOfUrls=$(hxwls 2>/dev/null "$playList" | awk -F '&' '/\<(watch|video)\>/{print$1}' | sort -u | paste -sd" ")
test $? != 0 && return
else
fqdn=$(echo "$playList" | sed "s|https\?://||" | cut -d/ -f1)
service=$(echo $fqdn | awk -F. '{print$(NF-1)}')
listOfUrls=$(time getVideoURLsFromVideoIDs $service $(ytdlGetVideoIDsFromPlayListURL $playList))
fi
getRestrictedFilenamesSD $listOfUrls
cd - >/dev/null
done
trap - INT
}
function getRestrictedFilenamesBEST {
getRestrictedFilenamesFORMAT "(best[ext=mp4]/best[ext=webm]/best[ext=flv])" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesFHD {
getRestrictedFilenamesFORMAT "(mp4[height<=?1080]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesFORMAT {
trap 'rc=127;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
# local translate="trans -b :en" # to translate any language to english
local ytdlExtraOptions
local translate=cat # Translate through pipe disabled by default
local initialSiteVideoFormat="$1"
local siteVideoFormat="not_yet_known" downloadOK=-1 extension="not_yet_known" fqdn="not_yet_known" service="not_yet_known" fileSizeOnFS=0 remoteFileSize=0
shift
local -i i=0
local isLIVE=false
local ffmpeg="command ffmpeg -hide_banner"
type -P AtomicParsley >/dev/null 2>&1 && local embedThumbnail="--embed-thumbnail" || local embedThumbnail="--write-thumbnail"
echo $initialSiteVideoFormat | grep -q "^9[0-9]" && isLIVE=true
if [ $BASH_VERSINFO -ge 4 ];then
if $isLIVE;then
ytdlExtraOptions=( --hls-use-mpegts --hls-prefer-ffmpeg )
else
ytdlExtraOptions=( --hls-prefer-native )
fi
else
if $isLIVE;then
ytdlExtraOptions="--hls-use-mpegts --hls-prefer-ffmpeg"
else
ytdlExtraOptions="--hls-prefer-native"
fi
fi
for url
do
let i++
echo "=> Downloading url # $i/$# ..."
echo
echo "=> url = $url"
echo $url | \egrep -wq "https?:" || url=https://www.youtube.com/watch?v=$url #Only youtube short urls are handled by "youtube-dl"
fqdn=$(echo $url | awk -F "[./]" '{gsub("www.","");print$3"_"$4}')
service=$(echo $fqdn | awk -F_ '{print$(NF-1)}')
case $url in
*.facebook.com/*) siteVideoFormat=$(echo $initialSiteVideoFormat+m4a | \sed -E "s/^(\(?)\w+/\1bestvideo/g");; #Replace the first word after the opening parenthesis
*) siteVideoFormat=$initialSiteVideoFormat;;
esac
# echo $siteVideoFormat | grep -q '[<>]' && siteVideoFormat=\"$siteVideoFormat\" # Etait necessaire si la variable "youtube_dl" utilise "eval"
echo "=> Testing if $url still exists ..."
fileNames=$(set +x;time youtube-dl -f "$siteVideoFormat" --get-filename -o "%(title)s__%(format_id)s__%(id)s.%(ext)s" --restrict-filenames -- "$url" 2>&1)
echo $fileNames | \egrep --color -A1 ERROR: && echo && continue
local -i j=0
declare -a formats=( $(echo $siteVideoFormat | \sed "s/,/ /g") )
for fileName in $fileNames
do
echo
echo "=> Downloading $url using the <${formats[$j]}> format ..."
echo
extension="${fileName/*./}"
chosenFormatID=$(echo "$fileName" | awk -F '__' '{print$2}')
fileName="${fileName/.$extension/__$fqdn.$extension}"
if [ -f "$fileName" ] && [ $isLIVE = false ]
then
echo "=> The file <$fileName> is already exists, comparing it's size with the remote file ..." 1>&2
echo
fileSizeOnFS=$(stat -c %s "$fileName")
time remoteFileSize=$(command youtube-dl --ignore-config -j -f $chosenFormatID $url | jq -r .filesize)
test $? != 0 && return
[ $remoteFileSize = null ] && remoteFileSize=-1
if [ ! -w "$fileName" ] || [ $fileSizeOnFS -ge $remoteFileSize ]
then
echo
echo "${colors[yellowOnBlue]}=> The file <$fileName> is already downloaded, skipping ...$normal" >&2
echo
let j++
continue
fi
fi
echo "=> fileName to be downloaded = <$fileName>"
echo
echo "=> chosenFormatID = $chosenFormatID"
echo
trap - INT
if [ $extension = mp4 ] || [ $extension = m4a ] || [ $extension = mp3 ]; then
time youtube-dl -o "$fileName" -f "${formats[$j]}" "${ytdlExtraOptions[@]}" "$url" $embedThumbnail
downloadOK=$?
test $downloadOK != 0 && {
time youtube-dl -o $fileName -f "${formats[$j]}" "${ytdlExtraOptions[@]}" "$url" 2>&1 | egrep -A1 'ERROR:.*No space left on device' >&2 && {
echo >&2
downloadOK=1
return 1
}
downloadOK=$?
}
elif [ $extension = m3u ] || [ $extension = m3u8 ]; then
# read m3u_URL resolution <<< "$(youtube-dl --get-filename -o"%(resolution)s" -gf "${formats[$j]}" "$url")"
while read m3u_URL resolution
do
:
done < <(youtube-dl --get-filename -o"%(resolution)s" -gf "${formats[$j]}" "$url")
time youtube-dl -o $fileName "$m3u_URL" 2>&1 | egrep -A1 'ERROR:.*No space left on device' >&2 && {
echo >&2
downloadOK=1
return 1
}
downloadOK=$?
else
time youtube-dl -o $fileName -f "${formats[$j]}" "${ytdlExtraOptions[@]}" "$url" 2>&1 | egrep -A1 'ERROR:.*No space left on device' >&2 && {
echo >&2
downloadOK=1
return 1
}
downloadOK=$?
fi
if ! type -P AtomicParsley >/dev/null 2>&1; then
if [ -s "${fileName/.$extension/.jpg}" ];then
time $ffmpeg -i "$fileName" -i "${fileName/.$extension/.jpg}" -map 0 -map 1 -c copy -disposition:v:1 attached_pic "${fileName/.$extension/_NEW.$extension}" && sync && mv "${fileName/.$extension/_NEW.$extension}" "$fileName" && rm "${fileName/.$extension/.jpg}"
fi
fi
echo
if [ $downloadOK = 0 ];then
if [ $extension = mp4 ] || [ $extension = m4a ] || [ $extension = mp3 ];then
echo "=> Adding $url to $fileName metadata ..."
timestampFileRef=$(mktemp) && touch -r "$fileName" $timestampFileRef
if type -P mp4tags >/dev/null 2>&1;then
mp4tags -m "$url" "$fileName"
else
time $ffmpeg -i "$fileName" -map 0 -c copy -metadata description="$url" "${fileName/.$extension/_NEW.$extension}" && sync && mv "${fileName/.$extension/_NEW.$extension}" "$fileName"
fi
touch -r $timestampFileRef "$fileName" && \rm $timestampFileRef
fi
chmod -w "$fileName"
echo
videoInfo "$fileName"
fi
let j++
done
done
echo
sync
set +x
return $downloadOK
}
function getRestrictedFilenamesFSD {
getRestrictedFilenamesFORMAT "(mp4[height=480]+m4a/mp4[height<=?480]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesFromFilesBEST {
getRestrictedFilenamesFromFilesFORMAT "best" "$@"
}
function getRestrictedFilenamesFromFilesFHD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?1080]/mp4/best)" "$@"
}
function getRestrictedFilenamesFromFilesFORMAT {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local listOfUrls=""
local format="$1"
shift
listOfUrls=$(getAliveURLsFromFile "$@")
getRestrictedFilenamesFORMAT "$format" $listOfUrls
trap - INT
}
function getRestrictedFilenamesFromFilesFSD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?480]/mp4/best)" "$@"
}
function getRestrictedFilenamesFromFilesHD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?720]/mp4/best)" "$@"
}
function getRestrictedFilenamesFromFilesLD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?240]/mp4/best)" "$@"
}
function getRestrictedFilenamesFromFilesSD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?360]/mp4/best)" "$@"
}
function getRestrictedFilenamesFromFilesVLD {
getRestrictedFilenamesFromFilesFORMAT "mp4[height<=?144]/mp4/best)" "$@"
}
function getRestrictedFilenamesHD {
getRestrictedFilenamesFORMAT "(mp4[height<=?720]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesLD {
getRestrictedFilenamesFORMAT "(mp4[height<=?240]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesSD {
getRestrictedFilenamesFORMAT "(mp4[height<=?360]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getRestrictedFilenamesVLD {
getRestrictedFilenamesFORMAT "(mp4[height<=?144]/mp4/best)" $@ # because of the "eval" statement in the "youtube_dl" bash variable
}
function getVideoURLsFromVideoIDs {
local service="$1"
shift
getFullURLsFromIDs "$service" $@
}
function reget {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local url=$1
local oldFile="$2"
local newFileName="$(youtube-dl -f 18 --get-filename $url || youtube-dl --get-filename $url)"
mv -v "$oldFile" "$newFileName"
head -n -1 "$newFileName" > "$newFileName.part"
rm "$newFileName"
youtube-dl -f 18 "$url" || youtube-dl $url
mp4tags -m "$url" "$newFileName" && chmod -w "$newFileName" && echo "=> Done."
trap - INT
}
function sizeOf {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local size
local totalExpr="0"
local format=18
echo $1 | \grep -q ^http || {
format=$1
shift
}
for url
do
size=$(\curl -sI "$(youtube-dl -gf $format $url)" | \sed "s/\r//g" | awk 'BEGIN{IGNORECASE=1}/Content-?Length:/{print$2/2^20}')
totalExpr="$totalExpr+$size"
echo "$url $size Mo"
done
total=$(perl -e "printf $totalExpr")
echo "=> total = $total Mo"
trap - INT
}
function updateYTMetadataFromFiles {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
for file
do
echo "=> file = $file"
echo $file | grep -q __ || continue
id=$(echo $file | awk -F"__|[.]" '{print$(NF-1)}')
format=$(echo $file | awk -F__ '{print$(NF-1)}')
youtube-dl -qs -- "http://www.youtube.com/watch?v=$id" 2>/dev/null && url="http://www.youtube.com/watch?v=$id"
chmod +w $file
[ $format = 22 ] && getRestrictedFilenamesHD $url || getRestrictedFilenamesSD $url
done
trap - INT
}
function updateYoutubeDescription {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
youtubeURLPrefix=https://www.youtube.com/watch?v=
dailymotionURLPrefix=https://www.dailymotion.com/video/
format="mp4"
i=0
for url
do
let i++
echo "=> Downloading url #$i/$# ..."
echo "=> url = $url"
youtube-dl -f "$format" -qs -- $url 2>&1 | \grep --color -A1 ^ERROR: && echo && continue
if ! echo $url | \egrep -wq "https?:"
then
if youtube-dl -e $youtubeURLPrefix$url >/dev/null 2>&1
then
url=$youtubeURLPrefix$url
elif youtube-dl -e $dailymotionURLPrefix$url >/dev/null 2>&1
then
url=$dailymotionURLPrefix$url
fi
fi
if echo $url | \egrep -q "youtube|youtu.be|tv2vie"
then
format=18
elif echo $url | grep -q dailymotion
then
#format=standard
#format=hq
format=480
fi
echo "=> url = $url"
echo
fileName=$(youtube-dl -f $format --get-filename "$url" || youtube-dl --get-filename "$url")
echo "=> fileName = <$fileName>"
echo
# url=$1
local descFileName=$(mktemp)
youtube-dl --get-description $url > $descFileName
mp4tags -c "$(<$descFileName)" "$fileName" && \rm $descFileName
mp4tags -m "$url" "$fileName" && chmod -w "$fileName" && echo "=> Done."
echo
command ffprobe -hide_banner "$fileName"
echo
test -f $descFileName && \rm -v $descFileName
done
sync
trap - INT
}
function ytdlFormats {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
test $# = 0 && {
echo "=> Usage: $FUNCNAME [--filterFormats] url1 url2 ..." >&2
return 1
}
local filterFormats="."
echo $1 | \grep -q -- "^--" && { filterFormats=${1:2};shift; }
time youtube-dl -F "$@" | egrep -vw "information|manifest|android player|automatic captions|Available formats|Checking .* video format URL" | \egrep "$filterFormats|Downloading|format code extension resolution note"
trap - INT
}
function ytdlpFormats {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
test $# = 0 && {
echo "=> Usage: $FUNCNAME [--filterFormats] url1 url2 ..." >&2
return 1
}
local filterFormats="."
echo $1 | \grep -q -- "^--" && { filterFormats=${1:2};shift; }
time yt-dlp -F "$@" | egrep -vw "information|manifest|android player|automatic captions|Available formats|Checking .* video format URL" | \egrep "$filterFormats|Downloading|format code extension resolution note"
trap - INT
}
function ytdlGetFormat {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [format] url1 url2 ..." >&2
return 1
}
local format=${1:-mp4[height<=?360]/mp4/best}
test $# -ge 2 && shift
for url
do
$ytdlnoconfig -f "$format" "$url" -j | jq -r '"=> url = " + .webpage_url + "\tresolution = " + (.width|tostring)+"x"+(.height|tostring) + "\tformat = " + .format_id' || echo
done | column -c $COLUMNS -ts ' '
}
function ytdlGetThumbNail {
$local url jsonResult thumbnailID thumbnailExtractor thumbnailURL thumbnailBaseName thumbnailExtension retCode
for url
do
jsonResult="$(youtube-dl -j "$url" | jq -r 'del(.formats, .tags, .automatic_captions, .requested_subtitles)')"
echo "$jsonResult" | jq -r $'.id, .extractor, [ .thumbnails[-1].url ][-1], .title | gsub("[: \']";"_") | gsub("[#|,()]";"") | gsub("&";"_and_")' | while read thumbnailID && read thumbnailExtractor && read thumbnailURL && read thumbnailBaseName;
do
if [ -n "$thumbnailID" ];then
set -o | \grep xtrace.*on && echo "=> thumbnailBaseName = <$thumbnailBaseName>"
thumbnailExtension="${thumbnailURL/*./}"
thumbnailExtension="${thumbnailExtension/\?*/}"
thumbnailBaseName+=__${thumbnailID}__${thumbnailExtractor}_com.$thumbnailExtension
if \wget -c -nv -O "$thumbnailBaseName" "$thumbnailURL";then
\du -h "$thumbnailBaseName"
else
retCode=$?
set +x
[ $retCode != 8 ] && echo "${jsonResult}" | wc -c | \numfmt --to=iec-i --suffix=B --format=%.1f && return $retCode
fi
unset thumbnailBaseName thumbnailID
fi
done
done
}
function ytdlpGetThumbNail {
$local url jsonResult thumbnailID thumbnailExtractor thumbnailURL thumbnailBaseName thumbnailExtension retCode
for url
do
jsonResult="$(yt-dlp -j "$url" | jq -r 'del(.formats, .tags, .automatic_captions, .requested_subtitles)')"
echo "$jsonResult" | jq -r $'.id, .extractor, [ .thumbnails[] | select(.preference == -1).url ][-1] , ( .title | gsub("[: \']";"_") | gsub("[#|,()]";"") | gsub("&";"_and_") )' | while read thumbnailID && read thumbnailExtractor && read thumbnailURL && read thumbnailBaseName;
do
if [ -n "$thumbnailID" ];then
set -o | \grep xtrace.*on && echo "=> thumbnailBaseName = <$thumbnailBaseName>" && echo "=> thumbnailURL = <$thumbnailURL>"
thumbnailExtension="${thumbnailURL/*./}"
thumbnailExtension="${thumbnailExtension/\?*/}"
thumbnailBaseName+=__${thumbnailID}__${thumbnailExtractor}_com.$thumbnailExtension
if \wget -c -nv -O "$thumbnailBaseName" "$thumbnailURL";then
\du -h "$thumbnailBaseName"
else
retCode=$?
set +x
[ $retCode != 8 ] && echo "${jsonResult}" | wc -c | \numfmt --to=iec-i --suffix=B --format=%.1f && return $retCode
fi
unset thumbnailBaseName thumbnailID
fi
done
done
}
function ytdlGetVideoIDsFromPlayListURL {
test $# = 0 && {
echo "=> Usage: $FUNCNAME playListURL" >&2
return 1
}
local playListURL="$1"
test "$playListURL" && $ytdlnoconfig --ignore-errors --flat-playlist -j "$playListURL" | jq -r .id
}
function ytdlGetFirstURL {
local youtube_dl="command youtube-dl"
local url="$1"
set -o pipefail
url="$($youtube_dl -j --playlist-items 1 "$url" | jq -r .webpage_url || echo "$url")"
set +o pipefail
echo "$url"
}
function ytdlGetSize {
type jq >/dev/null || { echo -1;return 1; }
local ytdlnoconfig="youtube-dl --ignore-config"
local firstArg=$1 format=best size=0 url=""
test $# = 0 && {
echo "=> Usage: $FUNCNAME [format=best] url" >&2
return 1
} || {
echo $firstArg | \egrep -q "^https?://" || { format=$firstArg;shift; }
# read size url <<< $($ytdlnoconfig -j -f $format "$@" | jq -r '[ .filesize ,.url | tostring ] | join(" ")')
while read size url
do
:
done < <($ytdlnoconfig -j -f $format "$@" | jq -r '[ .filesize ,.url | tostring ] | join(" ")')
[ "$size" = null ] && size=$(\curl -sI "$url" | \sed "s/\r//g" | awk -F ": " 'BEGIN{IGNORECASE=1}/^Content-?Length/{printf$2}')
echo $size
}
}
function ytdlpGetSize {
type jq >/dev/null || { echo -1;return 1; }
local ytdlpnoconfig="yt-dlp --ignore-config"
local firstArg=$1 format=best size=0 url=""
test $# = 0 && {
echo "=> Usage: $FUNCNAME [format=best] url" >&2
return 1
} || {
echo $firstArg | \egrep -q "^https?://" || { format=$firstArg;shift; }
# read size url <<< $($ytdlpnoconfig -j -f $format "$@" | jq -r '[ .filesize ,.url | tostring ] | join(" ")')
while read size url
do
:
done < <($ytdlpnoconfig -j -f $format "$@" | jq -r '[ .filesize ,.url | tostring ] | join(" ")')
[ "$size" = null ] && size=$(\curl -sI "$url" | \sed "s/\r//g" | awk -F ": " 'BEGIN{IGNORECASE=1}/^Content-?Length/{printf$2}')
echo $size
}
}
function ytdlPlaylist2M3U {
type jq >/dev/null || { return 1; }
local ytdlnoconfig="youtube-dl --ignore-config"
[ $# != 0 ] && echo "#EXTM3U"
for playList
do
$ytdlnoconfig -j "$playList" | jq -r '"#EXTINF:-1,"+.title,.webpage_url'
done
}
function ytdlPlaylist2URLS {
type jq >/dev/null || { return 1; }
local ytdlnoconfig="youtube-dl --ignore-config"
for playList
do
$ytdlnoconfig -j "$playList" | jq -r '.webpage_url+" # "+.title'
done
}
function ytdlSDFormats {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
local format=""
for url
do
echo "=> url = $url" 1>&2
format=$(youtube-dl -F $url | egrep -vw "only|hls-[0-9]+" | egrep '(webm|mp4|flv) .*([0-9]+x[0-9]+)|(unknown)' | egrep -wv "22|hd|http-720" | sort -k 2,2 -k 3,3rn)
echo "=> format :" 1>&2
echo "$format"
done
trap - INT
}
function ytdlUpdate {
if [ $# -gt 1 ];then
echo "=> Usage: $FUNCNAME [ytdlGitURL]"
return 1
elif [ $# = 1 ];then
local ytdlGitURL=$1
else
local ytdlGitURL=https://github.com/ytdl-org/youtube-dl
local ytdlPyPI_URL=https://pypi.org/pypi/youtube-dl
fi
local sudo=""
if $isAdmin
then
sudo="command sudo -H"
fi
local package=youtube-dl
local youtube_dl="$(type -P $package)"
if [ "$youtube_dl" ];then
local ytdlCurrentRelease=$($package --version)
echo "=> The current version of $package is <$ytdlCurrentRelease>."
echo "=> Searching for the latest release on $ytdlPyPI_URL ..." >&2
local ytdlLatestRelease=$(\curl -qLs $ytdlPyPI_URL/json | jq -r .info.version)
if [ -z "$ytdlLatestRelease" ];then
# Le repo. youtube-dl avait ete interdit par la R.I.A.A. le 23/10/2020 cf. https://github.com/github/dmca/blob/master/2020/10/2020-10-23-RIAA.md mais il est reouvert: https://github.blog/2020-11-16-standing-up-for-developers-youtube-dl-is-back/
set -o pipefail
echo "=> Couldn't find the latest release on $ytdlPyPI_URL, checking the $ytdlGitURL repository ..." >&2
local ytdlLatestRelease=$(\git ls-remote --tags --refs $ytdlGitURL | awk -F/ '{print$NF}' | sort -rV | head -1)
set +o pipefail
else
if echo $ytdlLatestRelease | cut -d. -f4 | \grep . -q;then
ytdlLatestRelease=$(printf "%04d.%02d.%02d.%d" $(echo $ytdlLatestRelease | cut -d. -f1-4 | tr . " "))
else
ytdlLatestRelease=$(printf "%04d.%02d.%02d" $(echo $ytdlLatestRelease | cut -d. -f1-3 | tr . " "))
fi
echo "=> Found the <$ytdlLatestRelease> version." >&2
fi
local ytdlCurrentRelease=$($youtube_dl --version)
if [ "$ytdlLatestRelease" != $ytdlCurrentRelease ];then
local ytdlPythonVersion=$($youtube_dl --ignore-config -v 2>&1 | awk -F "[ .]" '/debug. Python/{printf$3"."$4}')
$sudo pip$ytdlPythonVersion install -U $package
ytdlTestURLs="https://youtu.be/vWYp2iGMDcM https://www.dailymotion.com/video/x5850if"
if ! yt-dl -q -F $ytdlTestURLs >/dev/null;then
echo "=> Rolling back to $package version <$ytdlCurrentRelease> ..."
set -x
$sudo pip$ytdlPythonVersion install $package==$ytdlCurrentRelease
set +x
fi
else
echo "=> [$FUNCNAME] INFO : You already have the latest release, which is $ytdlLatestRelease." >&2
fi
else
echo "=> [$FUNCNAME] INFO : $package is not installed, installing $package ..." >&2
$sudo pip3 install -U $package
fi
}
function ytdlpUpdate {
if [ $# -gt 1 ];then
echo "=> Usage: $FUNCNAME [ytdlGitURL]"
return 1
elif [ $# = 1 ];then
local ytdlpGitURL=$1
else
local ytdlpGitURL=https://github.com/yt-dlp/yt-dlp
local ytdlpPyPI_URL=https://pypi.org/pypi/yt-dlp
fi
local sudo=""
if $isAdmin
then
sudo="command sudo -H"
fi
local package=yt-dlp
local yt_dlp="$(type -P $package)"
if [ -n "$yt_dlp" ];then
local ytdlpCurrentRelease=$($package --version)
echo "=> The current version of $package is <$ytdlpCurrentRelease>."
echo "=> Searching for the latest release on $ytdlpPyPI_URL ..." >&2
local ytdlpLatestRelease=$(\curl -qLs $ytdlpPyPI_URL/json | jq -r .info.version)
if [ -z "$ytdlpLatestRelease" ];then
set -o pipefail
echo "=> Couldn't find the latest release on $ytdlpPyPI_URL, checking the $ytdlpGitURL repository ..." >&2
local ytdlpLatestRelease=$(\git ls-remote --tags --refs $ytdlpGitURL | awk -F/ '{print$NF}' | sort -rV | head -1)
set +o pipefail
else
if echo $ytdlpLatestRelease | cut -d. -f4 | \grep . -q;then
ytdlpLatestRelease=$(printf "%04d.%02d.%02d.%d" $(echo $ytdlpLatestRelease | cut -d. -f1-4 | tr . " "))
else
ytdlpLatestRelease=$(printf "%04d.%02d.%02d" $(echo $ytdlpLatestRelease | cut -d. -f1-3 | tr . " "))
fi
echo "=> Found the <$ytdlpLatestRelease> version." >&2
fi
if [ "$ytdlpLatestRelease" != $ytdlpCurrentRelease ];then
local ytdlPythonVersion=$($yt_dlp --ignore-config -v 2>&1 | awk -F "[ .]" '/debug. Python/{printf$3"."$4}')
$sudo pip$ytdlPythonVersion install -U $package
ytdlpTestURLs="https://youtu.be/vWYp2iGMDcM https://www.dailymotion.com/video/x5850if https://ok.ru/video/2091889462009"
echo "=> Checking if $package can parse these URLs : $ytdlpTestURLs ..."
if ! time yt-dlp -q -F $ytdlpTestURLs >/dev/null;then
echo "=> At least one of them failed parsing so rolling back to $package version <$ytdlpCurrentRelease> ..."
set -x
$sudo pip$ytdlPythonVersion install $package==$ytdlpCurrentRelease
set +x
else
echo "=> Success."
fi
else
echo "=> [$FUNCNAME] INFO : You already have the latest release, which is $ytdlpLatestRelease." >&2
fi
else
echo "=> [$FUNCNAME] INFO : $package is not installed, installing $package ..." >&2
$sudo pip3 install -U $package
fi
}
function ytget {
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
youtubeURLPrefix=https://www.youtube.com/watch?v=
dailymotionURLPrefix=https://www.dailymotion.com/video/
format="mp4"
echo "=> There are $# urls to download ..."
for url
do
let i++
echo "=> Downloading url #$i/$# ..."
echo "=> url = $url"
youtube-dl -f "$format" -qs -- "$url" 2>&1 | \grep --color -A1 ^ERROR: && echo && continue
if ! echo $url | \egrep -wq "https?:"
then
if youtube-dl -e $youtubeURLPrefix$url >/dev/null 2>&1
then
url=$youtubeURLPrefix$url
elif youtube-dl -e $dailymotionURLPrefix$url >/dev/null 2>&1
then
url=$dailymotionURLPrefix$url
fi
fi
if echo $url | \egrep -q "youtube|youtu.be|tv2vie"
then
format=18
elif echo $url | grep -q dailymotion
then
#format=standard
#format=hq
format=480
fi
echo
fileName=$(youtube-dl -f $format --get-filename "$url" || youtube-dl --get-filename "$url")
echo "=> fileName = <$fileName>"
echo
if [ -f "$fileName" ] && [ ! -w "$fileName" ]
then
echo "${colors[yellowOnBlue]}=> The file <$fileName> is already downloaded, skipping ...$normal" >&2
echo
continue
fi
youtube-dl -f $format "$url" || youtube-dl "$url"
mp4tags -m "$url" "$fileName"
chmod -w "$fileName"
echo
command ffprobe -hide_banner "$fileName"
echo
done
\rm -v *.description
sync
trap - INT
}
trap - INT
set +x
test "$debug" -gt 0 && echo "=> END of $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal"