-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdisk.py
192 lines (160 loc) · 6.3 KB
/
mdisk.py
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
import requests
import json
import os
import subprocess
import threading
import shutil
# setting
currentFile = __file__
realPath = os.path.realpath(currentFile)
dirPath = os.path.dirname(realPath)
dirName = os.path.basename(dirPath)
# is Windows ?
iswin = os.environ.get("WIN", "0")
# binary setting
if iswin == "0":
ytdlp = dirPath + "/binaries/yt-dlp"
aria2c = dirPath + "/binaries/aria2c"
ffmpeg = dirPath + "/ffmpeg/ffmpeg"
ffprobe = dirPath + "/ffmpeg/ffprobe"
os.system(f"chmod 777 {ytdlp} {aria2c} {ffmpeg} {ffprobe} ffmpeg/qt-faststart")
else:
if os.path.exists("ffmpeg/ffprobe.zip"):
os.system("unzip ffmpeg/ffprobe.zip -d ffmpeg/")
os.remove("ffmpeg/ffprobe.zip")
if os.path.exists("ffmpeg/ffmpeg.zip"):
os.system("unzip ffmpeg/ffmpeg.zip -d ffmpeg/")
os.remove("ffmpeg/ffmpeg.zip")
ytdlp = dirPath + "/binaries/yt-dlp.exe"
aria2c = dirPath + "/binaries/aria2c.exe"
ffmpeg = dirPath + "/ffmpeg/ffmpeg.exe"
ffprobe = dirPath + "/ffmpeg/ffprobe.exe"
# header for request
header = {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://mdisk.me/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
}
# actual function
def mdow(link,message):
#setting
os.mkdir(str(message.id))
input_video = dirPath + f'/{message.id}/vid.mp4'
input_audio = dirPath + f'/{message.id}'
#input
inp = link
fxl = inp.split("/")
cid = fxl[-1]
# resp capturing
URL = f'https://diskuploader.entertainvideo.com/v1/file/cdnurl?param={cid}'
try:
resp = requests.get(url=URL, headers=header).json()['source']
except:
shutil.rmtree(str(message.id))
return None,None,None
result = subprocess.run([ytdlp, '--no-warning', '-k', '--user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36', '--allow-unplayable-formats', '-F', resp], capture_output=True, text=True)
with open(f"{message.id}.txt","w") as temp:
temp.write(result.stdout)
os.system(f"sed -i 1,6d {message.id}.txt")
# getting ids
with open(f"{message.id}.txt", 'r') as file1:
Lines = file1.readlines()
os.remove(f"{message.id}.txt")
audids = []
audname = []
i = 1
vid_format = str(0)
for line in Lines:
line = line.strip()
if "audio" in line:
audids.append(line.split(" ")[0])
if "[" in line and "]" in line:
audname.append(line.split("[")[1].split("]")[0])
i = i + 1
else:
audname.append(f"Track - {i}")
i = i + 1
if "video" in line:
vid_format = line.split(" ")[0]
# threding audio download
audi = threading.Thread(target=lambda:downaud(input_audio,audids,resp),daemon=True)
audi.start()
# video download
subprocess.run([ytdlp, '--no-warning', '-k', '-f', vid_format, resp, '-o', input_video, '--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'--allow-unplayable-formats', '--external-downloader', aria2c, '--external-downloader-args', '-x 16 -s 16 -k 1M'])
print("Video Downloaded")
# renaming
output = requests.get(url=URL, headers=header).json()['filename']
filename = output[:1000]
output = output.replace(".mkv", "").replace(".mp4", "")
output = "".join( x for x in output if (x.isalnum() or x in "._-@ "))
output = output[:200]
# check if normal video
if len(audids) == 0:
foutput = f"{output}.mkv"
os.rename(input_video,foutput)
shutil.rmtree(str(message.id))
return foutput,0,filename
# merge
audi.join()
cmd = f'{ffmpeg} -i "{input_video}" '
leng = 0
for ele in audids:
out_audio = input_audio + f'/aud-{ele}.m4a'
cmd = cmd + f'-i "{out_audio}" '
leng = leng + 1
cmd = cmd + "-map 0 "
i = 1
while(i<=leng):
cmd = cmd + f"-map {i} "
i = i + 1
i = 1
for ele in audname:
cmd = cmd + f'-metadata:s:a:{i} language="{ele}" '
tcmd = cmd
cmd = cmd + f'-c copy "{output}.mkv"'
print(cmd)
subprocess.call(cmd, shell=True)
print('Muxing Done')
# cleaning
if os.path.exists(output+'.mkv'):
print('Cleaning Leftovers...')
shutil.rmtree(str(message.id))
foutput = f"{output}.mkv"
return foutput,1,filename
else:
print("Trying with Changes")
ffoutput = f" {output}.mkv"
cmd = f'{tcmd} -c copy "{ffoutput}"'
subprocess.call(cmd, shell=True)
print('Muxing Done')
if os.path.exists(output+'.mkv'):
print('Cleaning Leftovers...')
shutil.rmtree(str(message.id))
return ffoutput,1,filename
# multi-threding audio download
def downaud(input_audio,audids,resp):
threadlist = []
for i in range(len(audids)):
threadlist.append(threading.Thread(target=lambda:downaudio(input_audio,audids[i],resp),daemon=True))
threadlist[i].start()
for ele in threadlist:
ele.join()
# actual audio download
def downaudio(input_audio,ele,resp):
out_audio = input_audio + f'/aud-{ele}.m4a'
subprocess.run([ytdlp, '--no-warning', '-k', '-f', ele, resp, '-o', out_audio, '--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'--allow-unplayable-formats', '--external-downloader', aria2c, '--external-downloader-args', '-x 16 -s 16 -k 1M'])
# getting size
def getsize(link):
inp = link
fxl = inp.split("/")
cid = fxl[-1]
URL = f'https://diskuploader.entertainvideo.com/v1/file/cdnurl?param={cid}'
try:
size = requests.get(url=URL, headers=header).json()["size"]
return size
except:
return 0