-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuTubeDwn.py
More file actions
executable file
·148 lines (118 loc) · 4.18 KB
/
uTubeDwn.py
File metadata and controls
executable file
·148 lines (118 loc) · 4.18 KB
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
# from pytubefix import YouTube
# pip install yt-dlp
import moviepy as mp
import os
import re
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
from pytubefix import YouTube
from pytubefix import request
from pytubefix import extract
from pytubefix.innertube import _default_clients
from pytubefix.exceptions import RegexMatchError
ssl._create_default_https_context = ssl._create_unverified_context
########################################
#YouTube interface
# returns the title of the video w/out illegal char for OS
# @url -> the youtube video url
def getTitle(url):
# # # tmp = YouTube(url, use_oauth=True, allow_oauth_cache=True).title
tmp = YouTube(url).title
#remove character not allowed on file system
tmp = re.sub(r'[^\w\s]', '', tmp)
return tmp
# download only the video
# @resParam => the resolution wanted
# @nameOfFile => name to assign only to the video (tmp if both = video + audio)
def downloadVideo(url,resParam,folderDestination="./tmp/", nameOfFile="video"):
print("Downloading the video")
try:
YouTube(url, use_oauth=True, allow_oauth_cache=True).streams.filter(res=str(resParam)+"p").first().download(output_path=folderDestination,filename=nameOfFile+'.mp4')
except Exception as e:
print("video or resolution aren't correct")
print("done!")
# download only the audio
def downloadAudio(url, folderDestination="./tmp/", nameOfFile="audio"):
print("Downloading the audio")
try:
yt = YouTube(url)
stream = yt.streams.filter(only_audio=True).first()
if stream is None:
print("No audio stream found")
return
stream.download(
output_path=folderDestination,
filename=nameOfFile + ".mp4"
)
except Exception as e:
print("Error:", e)
print("done!")
# reutrns a list of resolutions available of the video
def getResolutions(url):
availableRes= set()#remove the duplicated
for i in YouTube(url, use_oauth=True, allow_oauth_cache=True).streams.all():
if(i.resolution!=None):
availableRes.add(int(i.resolution.split("p")[0]))
return sorted(availableRes)
##################################################
#file handling
# merge video and audio downloaded
def mergeVideoAudio(url):
print("Merging video and audio")
audio = mp.AudioFileClip("./tmp/audio.mp4")
video1 = mp.VideoFileClip("./tmp/video.mp4")
final = video1.with_audio(audio)
# Create output directory if it doesn't exist
if not os.path.exists("output"):
os.makedirs("output") # Correct indentation (same as the if statement)
final.write_videofile("output/"+getTitle(url)+".mp4",codec='libx264' ,audio_codec='libvorbis')
def flushTmp():
os.remove("./tmp/audio.mp4")
os.remove("./tmp/video.mp4")
##################################################
def addVideoToQueue(queueVideo):
url = input("\nInsert the video's url: ")
print("pick a resolution of" + str(getResolutions(url)))
resolution = input("Insert the resolution as a number (720/1080): ")
mode = input("\n-----------\n\tA for only audio\n\tV for only video\n\tB for both\n->")
queueVideo[url]={
"res": resolution,
"mode": mode
}
return queueVideo
def processQueue(queueVideo):
for i in queueVideo:
print("Processing: "+getTitle(i))
if(queueVideo.get(i).get("mode")=="A"):
#just the audio
downloadAudio(i,"./output/",getTitle(i))
elif(queueVideo.get(i).get("mode")=="V"):
#just the video
downloadVideo(i,queueVideo.get(i).get("res"),"./output/",getTitle(i))
else:
#both video and audio
downloadVideo(i,queueVideo.get(i).get("res"))
downloadAudio(i)
mergeVideoAudio(i)
flushTmp()
def showQueue(queueVideo):
for i in queueVideo:
print(getTitle(i))
print(queueVideo.get(i).get("res"))
if(queueVideo.get(i).get("mode")=="A"):
print("Audio only")
elif(queueVideo.get(i).get("mode")=="V"):
print("Video only")
else:
print("Audio and video")
def main():
print("Welcome on uTube Downloader ;)")
queueVideo = dict()
choice = 1
while int(choice)!=0:
queueVideo=addVideoToQueue(queueVideo)
choice = input("To add another video press 1, to download the queue press 0: ")
print(queueVideo)
processQueue(queueVideo)
print("Done!\n your files are in output folder, bye ;)")
main()