-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3_download.py
68 lines (59 loc) · 2.07 KB
/
mp3_download.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
import os
import sys
import re
from moviepy.editor import *
from pytube import YouTube
from pytube import Playlist
def DIVIDE():
print("-------------------------------------------------")
def NORM_TITLE(unnorm):
normed_value = re.sub(r'\W+\s', '', unnorm)
return str(normed_value)
def GET_PLAYLIST():
global playlist
playlist_link=input("Paste the link to the YouTube playlist that you would like to download: ")
DIVIDE()
playlist = Playlist(playlist_link)
def SET_LOCATION():
global download_location
new_dir = NORM_TITLE(playlist.title)
download_location = os.environ.get("USERPROFILE") + "\\Music\\" + new_dir
def DOWNLOAD_PLAYLIST():
for video in playlist.videos:
global normed_video
normed_video = NORM_TITLE(video.title)
if not os.path.isfile(normed_video):
try:
print("Downloading: " + normed_video)
video.streams.first().download(output_path=download_location, filename=normed_video)
CONVERT(normed_video)
DIVIDE()
except:
print("DOWNLOAD FAILED: " + normed_video)
DIVIDE()
pass
def CREATE_FOLDER(dir):
if not os.path.exists(dir):
os.makedirs(dir)
def CONFIRM_DOWNLOAD():
print("DOWNLOAD LOCATION: " + download_location + "\n" + "PLAYLIST NAME: " + playlist.title)
confirmation = input("Would you like to download now? (Y/n)\n")
CREATE_FOLDER(download_location)
if confirmation == "y" or confirmation == "Y":
DIVIDE()
DOWNLOAD_PLAYLIST()
else: sys.exit()
def CONVERT(mp4file):
original_filename = download_location + "\\" + mp4file
try:
video_file = AudioFileClip(original_filename)
video_file.write_audiofile(original_filename + ".mp3")
video_file.close()
except:
print("CONVERT FAILED: " + original_filename)
pass
os.remove(original_filename)
##RUNTIME
GET_PLAYLIST()
SET_LOCATION()
CONFIRM_DOWNLOAD()