-
Notifications
You must be signed in to change notification settings - Fork 1
/
video-downloader.sh
executable file
·113 lines (101 loc) · 3.25 KB
/
video-downloader.sh
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
#!/bin/bash
echo -e "\e[01;32m================================================="
echo -e "| |"
echo -e "| \e[01;91mVideo Downloader\e[01;32m |"
echo -e "| |"
echo -e "=================================================\e[0m"
echo -e " \e[01;34mby: Renato Pacheco\e[00m"
echo -ne "\e[01mInsert link list: \e[0m"
read chunk
# Check if URL is valid
check_url=$(curl -s -o /dev/null -w "%{http_code}" $chunk)
if [ $check_url == 000 ]
then
echo -e "\e[01;91mInvalid URL:\e[0m $chunk\n\e[01;91mClosing...\e[0m\n"
exit 1
fi
echo -ne "\e[01mInsert name of file: \e[0m"
read file
# Change spaces for underline (anti-dumb-users)
file=$(echo $file | sed 's/ /_/g;s/[^a-z|A-Z|0-9|_]//g')
dir=temp
# Delete $dir if exists (avoiding to mix old contents)
if [ ! -d $dir ]
then
mkdir $dir
else
echo -e "\e[01;93mDeleting \"$dir\"...\e[0m\n"
rm -rf $dir
mkdir $dir
fi
# Downloading list (by URL at $chunk)
echo -e "\e[01mDownloading list...\e[0m"
lista="lista.m3u8"
wget -q $chunk -O $dir/$lista
# Checking if m3u8 file is valid
if [ -z $(egrep "^#EXTM3U" $dir/$lista) ]
then
echo -e "\e[01;91mThis list is not valid. Closing...\e[0m\n"
exit 1
fi
# Calculating number of segments
lenseg=$(grep '^https://' $dir/$lista | wc -l)
# Downloading video segments
echo -e "\e[01mDownloading video's segments...\e[0m\n"
cont=1
while [ $cont -lt $lenseg ]
do
for i in $(grep '^https://' $dir/$lista)
do
# Progress bar while downloading
perok=$(echo "$cont * 100 / $lenseg" | bc)
pernotok=$(echo "100 - ($cont * 100 / $lenseg)" | bc)
echo -ne "\e[01m[$(printf '#%.0s' $(seq 1 $perok))""$(printf ' %.0s' $(seq 1 $pernotok))]($perok%)\e[0m\r"
sleep 1
wget -q $i -O $dir/seg$cont && cont=$((cont+1))
done
done
echo -e "\n\n\e[01mDownload completed!\e[0m"
echo -e "\e[01mDecoding and contatenating all of segments in one file...\e[0m"
filekey=$(egrep -o 'https://(.*)encryption.key(.*)hmac=([0-9a-f])+' $dir/$lista)
iv=$(egrep -o 'IV=([0-9a-z]+)' $dir/$lista | sed 's/IV=0x//')
for i in $(seq 1 $lenseg)
do
# If key exists, it's needed to decrypt all of segments
if [[ $filekey != "" ]]
then
# If IV exists in URL, keep that in var iv, if not, every segment file has its IV, corresponding with segment number and padding zeros in
# the left until reach 32 digits. Ex.: 00000000000000000000000000000001 corresponding with segment 1.
if [ -z $iv ]
then
if [ $(echo -n $i|wc -c) -eq 1 ]
then
j=$(printf '0%.0s' {1..31})
elif [ $(echo -n $i|wc -c) -eq 2 ]
then
j=$(printf '0%.0s' {1..30})
else
j=$(printf '0%.0s' {1..29})
fi
iv=$j$i
fi
# Downloading encryption key of video segments
key=$(wget -q $filekey -O - | xxd -p 2> /dev/null)
# Decrypting every segment with key and IV
openssl aes-128-cbc -d -K $key -iv $iv -nosalt -in $dir/seg$i -out $dir/seg$i.mp4
# Concatenating all of segments in one file
cat $dir/seg$i.mp4 >> $file.mp4
fi
# Concatenating all of segments in one file
cat $dir/seg$i >> $file.mp4
done
echo -e "\e[01mVideo \e[01;93m$file.mp4\e[0m \e[01mcreated!\e[0m\n"
echo -ne "\e[01mDelete temp files? [S/n] \e[0m"
read ask
if [[ $ask == "s" || $ask == "S" || $ask == "" ]]
then
rm -rf $dir
echo -e "\e[01;93mTemp files deleted!\e[0m"
else
echo -e "\e[01;93mTemp files unchanged in $(pwd)/$dir\e[0m"
fi