Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- With this python script you can download youtube videos in m4a format.
- You must have python installed to be able to use this script.
- Usage: python3 yt_download.py
- Features:
- Download a single song
- Download a whole playlist
- Download a whole album
- You can either download in:
- Default location: "C:\Music" for Windows, "/home/Music" for Linux
- Custom location(You can input custom absolute path)
- To 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
- Note for Linux users: Run the script in root mode, for example sudo python3 yt_download.py
- Note: After you use the script for the first time, feel free to comment out lines 13 & 14 for faster execution. This is not a necessary action, you can completely ignore this note if you feel lazy to do it.
- Note: If you get HTTP Error 404 or 403 error code, it's nothing to worry about, just re-run the script.
- """
- import os
- import sys
- import subprocess
- import platform
- # Install dependency packages function
- def install(package):
- subprocess.check_call([sys.executable, "-m", "pip", "install", package])
- # Install pafy package
- install('pafy')
- install('youtube-dl')
- import pafy
- # Set yt_api_key
- api_key = "Add your api key here"
- pafy.set_api_key(api_key)
- # Handle download location
- def down_location(type, playlist_name=""):
- def_path = None
- current_os = platform.system()
- if type == 1:
- if current_os == "Windows":
- def_path = "C:\Music\Single songs"
- else:
- def_path = "/home/Music/Single songs"
- elif type == 2:
- if current_os == "Windows":
- def_path = "C:\Music\Playlists"
- else:
- def_path = "/home/Music/Playlists"
- elif type == 3:
- if current_os == "Windows":
- def_path = "C:\Music\Albums"
- else:
- def_path = "/home/Music/Albums"
- print("Use default download location or a custom one? (Just type 1 or 2)")
- print("1. Default location")
- print("2. Custom location")
- location = int(input())
- if location == 1:
- if not os.path.exists(def_path):
- os.makedirs(def_path)
- os.chdir(def_path)
- if playlist_name != "":
- if not os.path.exists(playlist_name):
- os.mkdir(playlist_name)
- os.chdir(playlist_name)
- elif location == 2:
- custom_dir = input("Enter absolute path of custom directory \n")
- os.chdir(custom_dir)
- else:
- print("Invalid input")
- def down_playlist(down_type):
- url = input("Enter playlist url: \n")
- playlist = pafy.get_playlist2(url)
- down_location(down_type, playlist.title)
- curr_song = 1
- for song in playlist:
- try:
- print(
- f"Currently downloading song {curr_song}/{len(playlist) + 1} - [{int((curr_song / len(playlist)) * 100)} %]")
- song_video = pafy.new(song.watchv_url)
- audiostreams = song_video.audiostreams
- audiostreams[2].download(song_video.title + ".m4a")
- curr_song += 1
- except:
- pass
- if __name__ == '__main__':
- # Main
- while True:
- print("Choose what type of download you're going to use: (Just type 1, 2 or 3)")
- print("1. A single song")
- print("2. A playlist")
- print("3. An album")
- down_type = int(input())
- if down_type == 1:
- url = input("Enter video url: \n")
- video = pafy.new(url)
- down_location(down_type)
- audiostreams = video.audiostreams
- audiostreams[2].download(video.title + ".m4a")
- elif down_type == 2:
- down_playlist(down_type)
- elif down_type == 3:
- down_playlist(down_type)
- else:
- print("Invalid input")
- print("All songs downloaded successfully")
- con = input("Do you wish to continue downloading?[y\\n]\n")
- if con == 'y':
- continue
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement