Skip to content

Instantly share code, notes, and snippets.

@phaer
Last active September 28, 2020 22:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaer/86bdcc3fb59cd3fcd9534bfe84d9fe5f to your computer and use it in GitHub Desktop.
Save phaer/86bdcc3fb59cd3fcd9534bfe84d9fe5f to your computer and use it in GitHub Desktop.
A simple script to pipe URIs through youtube-dl and into musicpd.
#!/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