-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
237 lines (215 loc) · 9.12 KB
/
main.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
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
import http
import os
import os.path
import shutil
import time
import requests
import re
import wget
from pytube import YouTube
def main():
while True:
print("Welcome to the Youtube Downloader")
print("Choose input method! (FILE/TERMINAL)")
source = input(">>>").upper()
if source == "FILE":
print("Checking for input.txt")
if not os.path.isfile("input.txt"):
open("input.txt", "x")
print("File not found. Please input your links in the input.txt file!")
input("Press enter to quit")
quit()
else:
print("File has been found!")
links = readFromFile("input.txt")
youtubeList, facebookList = checkHost(links)
if len(youtubeList) != 0:
downloadYT(youtubeList)
else:
print("No Youtube Videos!")
if len(facebookList) != 0:
downloadFB(facebookList)
else:
print("No Facebook Videos!")
break
elif source == "TERMINAL":
masterList = []
while True:
print("Input Youtube or Facebook links. Enter blank to exit")
link = input(">>>")
if link == "":
break
else:
masterList.append(link)
youtubeList, facebookList = checkHost(masterList)
if len(youtubeList) != 0:
downloadYT(youtubeList)
else:
print("No Youtube Videos!")
if len(facebookList) != 0:
downloadFB(facebookList)
else:
print("No Facebook Videos!")
break
else:
print("Input not recognized, please retry")
print("All files has been downloaded. Files can be found at '/viddownloader/downloads/' Press enter to exit")
input("All files has been downloaded. Press enter to exit")
quit()
def readFromFile(filename):
with open(filename) as file:
links = file.read().splitlines()
return links
def checkHost(linklist):
youtubeList = []
facebookList = []
unhandledList = []
for link in linklist:
if ("youtube" in link) or ("youtu" in link):
youtubeList.append(link)
elif ("facebook" in link) or ("fb" in link):
facebookList.append(link)
else:
unhandledList.append(link)
print("Found %d Youtube links, %d Facebook links, and %d unidentified objects" % (
(len(youtubeList)), (len(facebookList)), len(unhandledList)))
return youtubeList, facebookList
def downloadYT(links):
size = len(links)
current = 0
print("Starting YouTube Downloads!")
for link in links:
current += 1
tryCounter = 0
while True:
try:
video = YouTube(link)
break
except http.client.RemoteDisconnected:
tryCounter += 1
if tryCounter <= 5:
print("HTTP Connection failed, retrying %d out of 5 attempts in 15s..." % tryCounter)
time.sleep(15)
else:
print("Reconnection failed. Please try again later.")
quit()
title = clean(video.title)
doToken = doCheck(title)
dlMode = mode()
if dlMode == "AUDIO":
while True:
trycounter = 1
try:
print("Now downloading audio stream of %s from Youtube" % title)
video.streams.filter(mime_type='audio/mp4').order_by('abr').desc().first().download(
os.path.join(os.getcwd(), 'temp'), "audio")
print("Audio stream downloaded.")
break
except:
if trycounter <= 5:
print("A failure has occured, retrying in 15 seconds, attempt %d out of 5" % trycounter)
trycounter += 1
time.sleep(15)
else:
print("try limit failed, skipping this download.")
audio = os.path.join(os.getcwd(), 'temp', 'audio.mp4')
shutil.move(audio, os.path.join(os.getcwd(), 'videos', '%s.mp4' % title))
print("Finished processing %s, video %d out of %d" % (title, current, size))
elif dlMode == "ALL":
if doToken == True:
while True:
trycounter = 1
try:
print("Now downloading video stream of %s from Youtube" % title)
video.streams.filter(mime_type='video/mp4').order_by('resolution').desc().first().download(
os.path.join(os.getcwd(), 'temp'), "video")
print("Video stream downloaded.")
break
except:
if trycounter <= 5:
print("A failure has occured, retrying in 15 seconds, attempt %d out of 5" % trycounter)
trycounter += 1
time.sleep(15)
else:
print("try limit failed, attempting to download audio only.")
time.sleep(15)
break
while True:
trycounter = 1
try:
print("Now downloading audio stream of %s from Youtube" % title)
video.streams.filter(mime_type='audio/mp4').order_by('abr').desc().first().download(
os.path.join(os.getcwd(), 'temp'), "audio")
print("Audio stream downloaded.")
break
except:
if trycounter <= 5:
print("A failure has occured, retrying in 15 seconds, attempt %d out of 5" % trycounter)
trycounter += 1
time.sleep(15)
else:
print("try limit failed, skipping this download.")
time.sleep(15)
return None
video = os.path.join(os.getcwd(), 'temp', 'video.mp4')
audio = os.path.join(os.getcwd(), 'temp', 'audio.mp4')
temp = os.path.join(os.getcwd(), 'temp', 'temp.mp4')
os.mkdir(os.path.join(os.getcwd(), 'videos', '%s.mp4'))
if os.path.exists(video) and os.path.exists(audio):
print("Now concatenating video and audio streams of %s" % title)
os.system('ffmpeg -i %s -i %s -c:v copy -c:a aac %s' % (video, audio, temp))
shutil.move(temp, os.path.join(os.getcwd(), 'videos', '%s.mp4' % title))
os.remove(video)
os.remove(audio)
print("Finished processing %s, video %d out of %d" % (title, current, size))
elif os.path.exists(audio):
shutil.move(audio, os.path.join(os.getcwd(), 'videos', '%s.mp4') % title)
print("Finished processing %s, video %d out of %d. WARNING: NO VIDEO!" % (title, current, size))
def downloadFB(links):
print("Starting Facebook Downloads!")
size = len(links)
current = 1
for link in links:
try:
html = requests.get(link)
url = re.search('hd_src:"(.+?)"', html.text)[1]
wget.download(url, os.path.join(os.getcwd(), 'videos'))
print("Finished downloading %d out of %d" % (current, size))
current += 1
except TypeError:
print("This video is private and thus undownloadable :(, continuing!")
def clean(title):
title1 = title.replace("<", "").replace(">", "").replace("/", "").replace(":", "").replace('"', "")
title2 = title1.replace("|", "").replace("/", "").replace('?', "").replace("*", "")
return title2
def doCheck(title):
print("Checking if video has been downloaded...")
if (os.path.exists(os.path.join(os.getcwd(), 'videos', '%s.mp4' % title))):
print("Video %s has been downloaded. Do you want to re-download?(Y/N)" % title)
while True:
confirm = input(">>> ")
if (confirm == 'Y' or confirm == 'y'):
print("Downloading!")
doToken = True
break
elif (confirm == 'n' or confirm == 'N'):
print("Skipping download.")
doToken = False
break
else:
print("Input not recognized, please retry!")
else:
print("No previous download found, downloading!")
doToken = True
return doToken
def mode():
while True:
print("Please choose download mode! (AUDIO/ALL)")
mode = input().upper()
print(mode)
if mode == 'AUDIO' or mode == 'ALL':
return mode
else:
print("Input not recognized, please try again")
if __name__ == '__main__':
main()