-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytm.py
131 lines (110 loc) · 3.7 KB
/
ytm.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
#!/usr/bin/env python
import os
import requests
import time
import sys
def download_youtubedl():
if os.name == "nt":
if not(os.path.exists("ytd.exe")):
durl = "https://youtube-dl.org/downloads/latest/youtube-dl.exe"
r = requests.get(durl, allow_redirects=True)
open('ytd.exe', 'wb').write(r.content)
else:
if not(os.path.exists("ytd")):
durl = "https://youtube-dl.org/downloads/latest/youtube-dl"
r = requests.get(durl, allow_redirects=True)
open('ytd', 'wb').write(r.content)
def use_ytdl_p(url):
if os.name == "nt":
os.system("ytd.exe -f bestaudio -i -o %(playlist_title)s/%(title)s.%(ext)s " + url)
else:
os.system("python ytd -f bestaudio -i -o '%(playlist_title)s/%(title)s.%(ext)s' " + url)
def use_ytdl(url):
if os.name == "nt":
os.system("ytd.exe -f bestaudio -i -o %(title)s.%(ext)s " + url)
else:
os.system("python ytd -f bestaudio -i -o '%(title)s.%(ext)s' " + url)
def editpath(defpath):
global path
if defpath == "":
path = os.getcwd() + "/"
else:
if defpath[len(path)-1]!="/":
path += "/"
def dirlist(path):
pathlist=[]
for fileName in os.listdir(path):
if os.path.isdir(path+fileName):
pathlist.extend(dirlist(path+fileName+"/"))
else:
pathlist.append(path+fileName)
return pathlist
def convertToMp3(fileName):
if os.name == "nt":
os.system("ffmpeg.exe -i " + '"' + fileName + '" ' + '"' + fileName[:str.rfind(fileName,".")] + '.mp3"')
else:
os.system("ffmpeg -i " + '"' + fileName + '" ' + '"' + fileName[:str.rfind(fileName,".")] + '.mp3"')
def lconvertToMp3(fileName):
if os.name == "nt":
os.system("ffmpeg.exe -i " + '"' + fileName + '" ' + '"' + fileName[:str.rfind(fileName,"/")] + str.lower(fileName[str.rfind(fileName,"/"):str.rfind(fileName,".")]) + '.mp3"')
else:
os.system("ffmpeg -i " + '"' + fileName + '" ' + '"' + fileName[:str.rfind(fileName,"/")] + str.lower(fileName[str.rfind(fileName,"/"):str.rfind(fileName,".")]) + '.mp3"')
def checkffmpeg(path):
if os.name == "nt":
if not(os.path.exists(path + "ffmpeg.exe")):
print("Download and put ffmpeg.exe into this folder. You can download ffmpeg.exe here: https://cloud.mail.ru/public/4swb/3T3VJ4srN")
def args(args, urls):
global convert
global delorig
global lowreg
for x in args:
if "http" in x:
if "&" in x:
urls.append(x[:str.find(x, "&")])
else:
urls.append(x)
if "-path=" in x:
os.chdir(x[str.find(x, "=")+1:])
if "-convert=" in x:
print(x[str.find(x, "=")+1:])
convert = x[str.find(x, "=")+1:]
if "-delorig=" in x:
delorig = x[str.find(x, "=")+1:]
if "-lowreg=" in x:
lowreg = x[str.find(x, "=")+1:]
convert="T"
delorig="T"
lowreg="F"
path=""
pathlist=[]
pathlist1=[]
pathlist2=[]
urls=[]
args(sys.argv, urls)
editpath(path)
download_youtubedl()
pathlist1 = dirlist(path)
for x in urls:
if "playlist" in x or "channel" in x:
use_ytdl_p(x)
else:
use_ytdl(x)
pathlist2 = dirlist(path)
for y in pathlist2:
if not(y in pathlist1):
pathlist.append(y)
for z in pathlist:
z.replace(" ", "$20")
if convert=="T":
checkffmpeg(path)
for x in pathlist:
print(x)
if lowreg=="T":
lconvertToMp3(x)
else:
convertToMp3(x)
for z in pathlist:
z.replace("$20", " ")
if delorig=="T":
for x in pathlist:
os.remove(x)