-
Notifications
You must be signed in to change notification settings - Fork 196
/
mdisk.py
206 lines (164 loc) · 6.76 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import requests
import json
import os
import subprocess
import threading
import shutil
import zipfile
# setting
currentFile = __file__
realPath = os.path.realpath(currentFile)
dirPath = os.path.dirname(realPath)
dirName = os.path.basename(dirPath)
# is Windows ?
if os.name == 'nt': iswin = "1"
else: iswin = "0"
# binary setting
if iswin == "0":
ytdlp = dirPath + "/binaries/yt-dlp"
aria2c = dirPath + "/binaries/aria2c"
ffmpeg = dirPath + "/ffmpeg/ffmpeg"
ffprobe = dirPath + "/ffmpeg/ffprobe"
ffmpeg_Lzip = dirPath + "/ffmpeg/ffmpeg_L.zip"
ffprobe_Lzip = dirPath + "/ffmpeg/ffprobe_L.zip"
if not os.path.exists(ffmpeg):
with zipfile.ZipFile(ffmpeg_Lzip,"r") as zip_ref:
zip_ref.extractall("ffmpeg")
os.remove(ffmpeg_Lzip)
if not os.path.exists(ffprobe):
with zipfile.ZipFile(ffprobe_Lzip,"r") as zip_ref:
zip_ref.extractall("ffmpeg")
os.remove(ffprobe_Lzip)
os.system(f"chmod 777 {ytdlp} {aria2c} {ffmpeg} {ffprobe} ffmpeg/qt-faststart")
else:
ytdlp = dirPath + "/binaries/yt-dlp.exe"
aria2c = dirPath + "/binaries/aria2c.exe"
ffmpeg = dirPath + "/ffmpeg/ffmpeg.exe"
ffprobe = dirPath + "/ffmpeg/ffprobe.exe"
ffmpeg_Wzip = dirPath + "/ffmpeg/ffmpeg.zip"
ffprobe_Wzip = dirPath + "/ffmpeg/ffprobe.zip"
if not os.path.exists(ffmpeg):
with zipfile.ZipFile(ffmpeg_Wzip,"r") as zip_ref:
zip_ref.extractall("ffmpeg")
os.remove(ffmpeg_Wzip)
if not os.path.exists(ffprobe):
with zipfile.ZipFile(ffprobe_Wzip,"r") as zip_ref:
zip_ref.extractall("ffmpeg")
os.remove(ffprobe_Wzip)
# 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(allinfo,message):
#setting
os.mkdir(str(message.id))
input_video = dirPath + f'/{message.id}/vid.mp4'
input_audio = dirPath + f'/{message.id}'
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', allinfo["source"]], capture_output=True, text=True)
with open(f"{message.id}.txt","w") as temp:
temp.write(result.stdout)
if iswin == "0": 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,allinfo["source"]),daemon=True)
audi.start()
# video download
subprocess.run([ytdlp, '--no-warning', '-k', '-f', vid_format, allinfo["source"], '-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'])
# check if video downloaded
if not os.path.exists(input_video):
print("Video Not Downloaded")
return allinfo["source"],-1,None
print("Video Downloaded")
# renaming
output = allinfo['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} -y -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}" '
return cmd,output,filename
def merge(message,cmd,output,filename):
tcmd = cmd
cmd = cmd + f'-c copy "{output}.mkv"'
print(cmd)
subprocess.call(cmd, shell=True)
# 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)
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()
print("Audio Downloaded")
# 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 info
def getinfo(link):
URL = f'https://diskuploader.entertainvideo.com/v1/file/cdnurl?param={link.split("/")[-1]}'
try: return requests.get(url=URL, headers=header).json()
except: return {}