Last active
May 12, 2019 18:32
-
-
Save Fulgen301/99e9f554c7ae9087f6d4bd6ac91c2569 to your computer and use it in GitHub Desktop.
YouTube to MPD-suitable URL
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 | |
import youtube_dl, re, sys | |
from mpd import MPDClient | |
class SilentLogger(object): | |
def debug(self, msg): | |
pass | |
def warning(self, msg): | |
pass | |
def error(self, msg): | |
raise Exception(msg) | |
url = re.match(r"(https:\/\/(music|www|m)\.youtube\.com\/watch\?v=.*?)(&.*|$)", sys.argv[1]) | |
if not url: | |
sys.exit(1) | |
with youtube_dl.YoutubeDL({"format" : "bestaudio/best", "logger" : SilentLogger()}) as ydl: | |
result = ydl.extract_info(url[1], download=False) | |
client = MPDClient() | |
client.timeout = 10 | |
client.connect("localhost", 6600) | |
songid = client.addid(result["url"]) | |
if len(result["tags"]): | |
client.addtagid(songid, "artist", result["tags"][0]) | |
if len(result["tags"]) >= 2: | |
client.addtagid(songid, "title", result["tags"][1]) | |
if len(result["tags"]) >= 3: | |
client.addtagid(songid, "album", result["tags"][2]) | |
else: | |
client.addtagid(songid, "artist", result["uploader"]) | |
client.addtagid(songid, "title", result["title"]) | |
client.close() | |
client.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment