rj1
log | files | refs

ytmpd (741B) - raw


#!/usr/bin/env python3
import sys
from mpd import MPDClient
from yt_dlp import YoutubeDL

if len(sys.argv) < 2:
    print("ytmpd: missing url")
    sys.exit(1)

source_url = sys.argv[1]
mpd = MPDClient()

with YoutubeDL({"format": "bestaudio/audio"}) as youtube:
    info = youtube.extract_info(source_url, download=False)
    url = info.get("url")
    title = info.get("title")
    source = info.get("extractor_key")

    mpd.connect("localhost", 6600)
    song_id = mpd.addid(url)
    mpd.addtagid(song_id, "title", title)
    mpd.addtagid(song_id, "album", source)

    # cli parameter to play the song immediately
    if len(sys.argv) > 2 and sys.argv[2] == "--play":
        mpd.playid(song_id)
        mpd.play()

    mpd.disconnect()