-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (121 loc) · 5.2 KB
/
main.py
File metadata and controls
140 lines (121 loc) · 5.2 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
from pytubefix import YouTube
from ytmusicapi import YTMusic
import FreeSimpleGUI as sg
import subprocess
import sys
import os
sg.theme("DarkGrey3")
ytmusic = YTMusic()
DOWNLOAD_DIR = "downloaded_songs"
def Author(Song):
search = ytmusic.search(Song, "songs", None, 1, False)
return str(search[0]['artists'][0]['name'])
def Title(Song):
search = ytmusic.search(Song, "songs", None, 1, False)
return str(search[0]['title'])
def Link(Song):
search = ytmusic.search(Song, "songs", None, 1, False)
Link = "https://youtube.com/watch?v=" + str(search[0]['videoId'])
return Link
def GUI():
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "MP3":
file = Title(values[0]) + ".mp3"
yt = YouTube(Link(values[0])).streams.filter(only_audio=True).get_audio_only().download(output_path=values["-IN-"], filename=file)
sg.Popup("Done!")
window.close()
elif event == "WAV":
file = Title(values[0]) + ".wav"
yt = YouTube(Link(values[0])).streams.filter(only_audio=True).get_audio_only().download(output_path=values["-IN-"], filename=file)
sg.Popup("Done!")
window.close()
elif event == "FLAC":
file = Title(values[0]) + ".flac"
yt = YouTube(Link(values[0])).streams.filter(only_audio=True).get_audio_only().download(output_path=values["-IN-"], filename=file)
sg.Popup("Done!")
window.close()
elif event == "OGG":
file = Title(values[0]) + ".ogg"
yt = YouTube(Link(values[0])).streams.filter(only_audio=True).get_audio_only().download(output_path=values["-IN-"], filename=file)
sg.Popup("Done!")
window.close()
layout = [
[sg.Text("Music Downloader")],
[sg.Text("Enter the Musicname: "), sg.Input()],
[sg.Text("Where should the File be saved: "), sg.FolderBrowse(key="-IN-")],
[sg.Button("MP3"), sg.Button("WAV"), sg.Button("FLAC"), sg.Button("OGG")]
]
if len(sys.argv) > 2:
if sys.argv[2] == "--cli":
args = sys.argv[2:]
if "--list" in args:
liste = sys.argv[1]
isList = True
else:
name = sys.argv[1]
isList = False
if "--format=MP3" in args:
format = ".mp3"
elif "--format=WAV" in args:
format = ".wav"
elif "--format=FLAC" in args:
format = ".flac"
elif "--format=OGG" in args:
format = ".ogg"
else:
format = ".mp3"
if (isList):
try:
with open(liste, "r", encoding='utf-8') as r:
for line in r:
song = line.strip()
if song:
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
fileName = Title(song)
artist = Author(song)
yt = YouTube(Link(song)).streams.filter(only_audio=True).first()
out_file = yt.download(output_path=DOWNLOAD_DIR)
base, ext = os.path.splitext(out_file)
new_file = base + ' - ' + artist + '.mp3'
os.rename(out_file, new_file)
fixed_file = base + "_fixed.mp3"
ffmpeg_cmd = [
"ffmpeg",
"-i", new_file,
"-qscale:a", "2",
fixed_file
]
subprocess.run(ffmpeg_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
os.remove(new_file)
os.rename(fixed_file, new_file)
print(f"{fileName} sucessfully downloaded!\n")
except:
print("No such File")
else:
fileName = Title(name)
artist = Author(name)
file = fileName + " - " + artist + format
yt = YouTube(Link(name)).streams.filter(only_audio=True).get_audio_only().download(filename=file)
print(f"{fileName} - {artist} wurde heruntergeladen!")
sys.exit(0)
if len(sys.argv) > 1 and not len(sys.argv) > 3:
print("python3 main.py <Name/List> [Options] [CLI-Options]...")
print("Options:")
print(" --cli: Uses the CLI Application")
print("CLI-Options:")
print(" --list: Downloads each Song from a List .txt")
print(" --format=<File Format>: Choose your File Format. Default: MP3")
print(" MP3")
print(" WAV")
print(" FLAC")
print(" OGG")
print("Help:")
print(" --help: Displays help")
sys.exit(0)
window = sg.Window("Music Downloader", icon="Icon.ico", element_justification='c').Layout(layout)
GUI()
window.close()