-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass-ytdl.py
executable file
·132 lines (101 loc) · 3.47 KB
/
mass-ytdl.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
#!/usr/bin/env python3
import os
import subprocess
import sys
import time
import xlrd
def print_help():
print("Usage: ./mass-ydl <SPREADSHEET>")
print("Make sure <SPREADSHEET> is a path to a valid spreadsheet.")
def download(data):
data["Title"] = str(data["Title"]).replace("/", "-")
data["Artist"] = str(data["Artist"]).replace("/", "-")
output = f"output/{data['Artist']}/{data['Album']}/{data['Artist']} - {data['Title']}-R-.%(ext)s"
print(f"==> [..........] 0% Downloading: {data['Artist']} - {data['Title']}.ogg", end=" ")
if os.path.exists(f"output/{data['Artist']}/{data['Album']}/{data['Artist']} - {data['Title']}.ogg"):
print("- Already exists. Skipping.", end=" ")
return 0
print(f"\r==> [#.........] 10% Downloading: {data['Artist']} - {data['Title']}.ogg", end=" ")
process = subprocess.run(
[
"youtube-dl", "-x", "--audio-format", "vorbis",
data["Target URL"], "-o", output
],
stdout=open(os.devnull, "wb"),
stderr=open(os.devnull, "wb")
)
if process.returncode != 0:
return -1
print(f"\r==> [#######...] 70% Downloading: {data['Artist']} - {data['Title']}.ogg", end=" ")
finput = f"output/{data['Artist']}/{data['Album']}/{data['Artist']} - {data['Title']}-R-.ogg"
output = f"output/{data['Artist']}/{data['Album']}/{data['Artist']} - {data['Title']}.ogg"
cliargs = [
"ffmpeg", "-i", finput, "-acodec", "copy",
"-map_metadata", "-1",
"-metadata", "title=%s" % data['Title'],
"-metadata", "artist=%s" % data['Artist'],
"-metadata", "album=%s" % data['Album'],
]
if data["Track #"] != "" and data["out of"] != "":
cliargs.append("-metadata")
cliargs.append("track=%s/%s" % (data["Track #"], data["out of"]))
cliargs.append(output)
process = subprocess.run(cliargs, stdout=open(os.devnull, "wb"), stderr=open(os.devnull, "wb"))
if process.returncode != 0:
return -1
print(f"\r==> [#########.] 90% Downloading: {data['Artist']} - {data['Title']}.ogg", end=" ")
process = subprocess.run(
[
"rm", "-f", finput
],
stdout=open(os.devnull, "wb"),
stderr=open(os.devnull, "wb")
)
if process.returncode != 0:
return -1
print(f"\r==> [##########] 100% Downloading: {data['Artist']} - {data['Title']}.ogg", end=" ")
def main(args):
start = time.time()
# Argument checking.
if len(args) != 1 or "-h" in args or "--help" in args:
print_help()
return -1
else:
fname = args[0]
# Making sure the supplied path exists.
try:
assert os.path.exists(fname) and os.path.isfile(fname)
except:
print(f"Path {fname} is invalid.")
return -1
# Variables.
data = []
template = {}
headers = {}
spreadsheet = xlrd.open_workbook(fname).sheet_by_index(0)
# Creating a template.
for column in range(len(spreadsheet.row(0))):
header = spreadsheet.row(0)[column].value
template[header] = None
headers[column] = header
# Populating data.
for row in range(1, spreadsheet.nrows):
item = template.copy()
for column in range(len(spreadsheet.row(row))):
value = spreadsheet.cell(row, column).value
if type(value) == float:
if value == int(float(value)):
value = str(int(float(value)))
else:
value = "ERROR"
item[headers[column]] = value
data.append(item)
# Download the music from the video and set its metadata.
for item in data:
if download(item) == -1:
print("ERROR ENCOUNTERED! See alerts.txt.")
open("alerts.txt", "a+").write(str(item)+"\n")
print()
print(f"Done with {len(data)} items in {int(time.time()-start)} seconds elapsed.")
if __name__ == "__main__":
main(sys.argv[1::])