Last active
September 28, 2020 22:52
-
-
Save phaer/86bdcc3fb59cd3fcd9534bfe84d9fe5f to your computer and use it in GitHub Desktop.
A simple script to pipe URIs through youtube-dl and into musicpd.
This file contains 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 | |
""" | |
A simple script to pipe URIs through youtube-dl and into musicpd. | |
pip3 install python-mpd2 youtube-dl | |
""" | |
import sys | |
import mpd | |
import youtube_dl | |
mpd_host=('localhost', 6600) | |
ydl_opts = { | |
'format': 'bestaudio/audio' | |
} | |
if __name__ == '__main__': | |
if len(sys.argv) is not 2: | |
print("usage: {} <url>".format(sys.argv[0])) | |
sys.exit(1) | |
source_url = sys.argv[1] | |
client = mpd.MPDClient(use_unicode=True) | |
with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
info = ydl.extract_info(source_url, download=False) | |
url = info.get('url') | |
title = info.get('title') | |
source = info.get('extractor_key') | |
if not (url and title and source): | |
print("youtube-dl error.") | |
sys.exit(1) | |
client.connect(*mpd_host) | |
song_id = client.addid(url) | |
client.addtagid(song_id, 'title', title) | |
client.addtagid(song_id, 'album', source) | |
client.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment