· 4 years ago · Jun 19, 2021, 12:44 AM
1"""
2With this python script you can download youtube videos in m4a format.
3
4You must have python installed to be able to use this script.
5
6Features:
71. Download a single song
82. Download a whole playlist
93. Download a whole album
10
11You can either download in:
121. Default location: "C:\Music" for Windows, "/home/Music" for Linux
132. Custom location(You can input custom absolute path)
14
15To be able to start using this script, you have to first edit line 7 with your youtube data api key, you can read more about it here: https://developers.google.com/youtube/v3/getting-started
16
17If you're getting errors that pafy is not installed, you have to install pafy manually via pip: pip install pafy
18"""
19
20import pafy
21import os
22import sys
23import subprocess
24import platform
25
26# Set yt_api_key
27api_key = "Add your api key here"
28pafy.set_api_key(api_key)
29
30# Install dependency packages function
31def install(package):
32 subprocess.check_call([sys.executable, "-m", "pip", "install", package])
33
34# Handle download location
35def down_location(type, playlist_name=""):
36 def_loc_line = None
37 if type == 1:
38 def_loc_line = "\Single songs"
39 elif type == 2:
40 def_loc_line = "\Playlists"
41 elif type == 3:
42 def_loc_line = "\Albums"
43
44 print("Use default download location or a custom one? (Just type 1 or 2)")
45 print("1. Default location")
46 print("2. Custom location")
47 location = int(input())
48 if location == 1:
49 current_os = platform.system()
50 if current_os == "Windows":
51 if not os.path.exists("C:\Music"):
52 os.mkdir("C:\Music")
53 if not os.path.exists("C:\Music" + def_loc_line):
54 os.mkdir("C:\Music" + def_loc_line)
55 os.chdir("C:\Music" + def_loc_line)
56 else:
57 if not os.path.exists("/home/Music"):
58 os.mkdir("/home/Music")
59 if not os.path.exists("/home/Music" + def_loc_line):
60 os.mkdir("/home/Music" + def_loc_line)
61 os.chdir("/home/Music" + def_loc_line)
62 if playlist_name != "":
63 if not os.path.exists(playlist_name):
64 os.mkdir(playlist_name)
65 os.chdir(playlist_name)
66 elif location == 2:
67 custom_dir = input("Enter absolute path of custom directory \n")
68 os.chdir(custom_dir)
69 else:
70 print("Invalid input")
71
72def down_playlist(down_type):
73 url = input("Enter playlist url: \n")
74 playlist = pafy.get_playlist2(url)
75 down_location(down_type, playlist.title)
76
77 curr_song = 1
78 for song in playlist:
79 try:
80 print(
81 f"Currently downloading song {curr_song}/{len(playlist) + 1} - [{int((curr_song / len(playlist)) * 100)} %]")
82 song_video = pafy.new(song.watchv_url)
83 audiostreams = song_video.audiostreams
84 audiostreams[2].download(song_video.title + ".m4a")
85 curr_song += 1
86 except:
87 pass
88
89
90if __name__ == '__main__':
91 # Install pafy package
92 install('pafy')
93
94 # Main
95 print("Choose what type of download you're going to use: (Just type 1, 2 or 3)")
96 print("1. A single song")
97 print("2. A playlist")
98 print("3. An album")
99 down_type = int(input())
100
101 if down_type == 1:
102 url = input("Enter video url: \n")
103 video = pafy.new(url)
104 down_location(down_type)
105 audiostreams = video.audiostreams
106 audiostreams[2].download(video.title + ".m4a")
107
108 elif down_type == 2:
109 down_playlist(down_type)
110
111 elif down_type == 3:
112 down_playlist(down_type)
113
114 else:
115 print("Invalid input")
116