-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoupy.py
124 lines (85 loc) · 2.48 KB
/
youpy.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
from __future__ import unicode_literals
import youtube_dl
import os
import sys
from urllib.request import urlopen
import threading
import time
downloading = False
first = True
link =''
directory = "mp3"
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'mp3/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
}
def createFolder(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def sizePrinter(title):
global downloading, first
s = 0
# Already in mp3 format
if first and os.path.exists(directory + "\\" + title + '.mp3'):
s = int(((os.path.getsize(directory + "\\" + title + '.mp3'))))
if s > 10:
return
first = False
title = title.replace('|','_')
title = title.replace('/','_')
title = title.replace('\\','_')
while not os.path.exists(directory + "\\" + title + '.webm'):
time.sleep(.1)
if os.path.exists(directory + "\\" + title + '.webm'):
totalSize = int(((os.path.getsize(directory + "\\" + title + '.webm')) * 2.46))
while not os.path.exists(directory + "\\" + title + '.mp3'):
time.sleep(.1)
time.sleep(.6)
print('[convert] Converting: ' + directory + "\\" + title + '.webm'+ ' to .mp3')
while downloading:
size = -1
if os.path.exists(directory + "\\" + title + '.mp3'):
size = (os.path.getsize(directory + "\\" + title + '.mp3'))
if size != totalSize:
per = round((size/totalSize * 100),1)
print ('[convert] Converted: ' + str(per)+"%" + ' | ' + str(size) + ' of ' + str(totalSize), end="\r")
time.sleep(.1)
# Back to previous line
#sys.stdout.write("\033[F")
# Clear line
#sys.stdout.write("\033[K")
print('[convert] converted: 100%')
print('Done!')
def download():
global downloading
ytdl = youtube_dl.YoutubeDL()
info = ytdl.extract_info(link, download=False)
title = info['title']
formats = info['formats']
downloading = True
x = threading.Thread(target=sizePrinter,args=(title,))
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
x.start()
ydl.download([link])
downloading = False
def getInput():
global link
link = input("Give me a youtube link pls: ")
def main():
print("Youtube MP3 music getter v0.2 by Bhuba")
getInput()
print('Loading...')
createFolder(directory)
download()
script_dir = os.path.dirname(os.path.realpath(__file__))
print(script_dir)
rel_path = "mp3"
abs_path = os.path.join(script_dir, rel_path)
os.startfile(abs_path)
if __name__ == "__main__":
main()