<reponame>poseidon-code/mp3-download-script-v2<gh_stars>1-10
import sys
import os
import concurrent.futures
from os import system
import youtube_dl
import getopt

class color:
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    ENDC = '\033[0m'


def get_download_path():
    if os.name == 'nt':
        import winreg
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')

def print_status():
    system('cls')
    print('*** Downloading', len(URLS), 'musics ***')
    print('*** Download Directory : ', download_path , '***')
    [print(item) for item in status]


def download(url):
    with youtube_dl.YoutubeDL(options) as mp3:
        info = mp3.extract_info(url, download=False)
        title = info.get('title', None)

        color_string = color.WARNING + '[downloading]\t' + color.ENDC + title
        status[URLS.index(url)] = color_string
        print_status()

        mp3.download([url])

        color_string = color.OKGREEN + '[finished]\t' + color.ENDC + title
        status[URLS.index(url)] = color_string
        print_status()



status = []
arguments = sys.argv[1:]

try:
    options, URLS = getopt.getopt(arguments, 'd:', ['dir='])
except:
    pass

download_path=get_download_path()
for option, value in options:
    if option in ['-d', '--dir']:
        download_path = value


options = {
        # PERMANENT options
        'quiet': True,
        'format': 'bestaudio/best',
        'ffmpeg_location': f'{os.path.abspath(os.getcwd())}/ffmpeg.exe', 
        'keepvideo': False,
        'outtmpl': f'{download_path}/%(title)s.bin',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '320'
        }],

        # OPTIONAL options
        'noplaylist': True
    }

for url in URLS:
    color_string = color.OKCYAN + '[starting]\t' + color.ENDC + url
    status.append(color_string)


with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(download, URLS)
