Last active
September 15, 2025 08:12
-
-
Save axel-angel/3abfb46435051c2e816efb1fe5e21a88 to your computer and use it in GitHub Desktop.
Convert spotify urls to youtube urls with Spotdl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # requirements: pip install spotdl | |
| import sys, os, json | |
| from spotdl import Spotdl | |
| from spotdl.types.song import Song | |
| from argparse import ArgumentParser | |
| from tqdm.auto import tqdm | |
| parser = ArgumentParser() | |
| parser.add_argument('input', nargs='+') | |
| parser.add_argument('--output', default=None) | |
| parser.add_argument('--config', default=os.environ['HOME']+'/.spotdl/config.json') | |
| args = parser.parse_args() | |
| with open(args.config, 'rt') as fd: | |
| j = json.load(fd) | |
| if args.output is None: | |
| output = sys.stdout | |
| else: | |
| output = open(args.output, 'wt') | |
| spotdl = Spotdl(client_id=j['client_id'], | |
| client_secret=j['client_secret'], | |
| downloader_settings=dict(simple_tui=True)) | |
| songs = [] | |
| for input in tqdm(args.input): | |
| if os.path.isfile(input): | |
| with open(input, 'rt') as fd: | |
| if input.endswith('.spotdl'): | |
| songs.extend(( Song.from_dict(x) for x in json.load(fd) )) | |
| else: | |
| songs.extend(( fd.readlines() )) | |
| else: | |
| songs.extend(spotdl.search([ input ])) | |
| for song in (p := tqdm(songs)): | |
| p.set_postfix(song=f"{song.artist} - {song.name}") | |
| for url in spotdl.get_download_urls([ song ]): | |
| output.write(url + '\n') | |
| output.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment