Youtube support for automated casting to Google Home

Through some effort, and help on reddit, I found that you can send a youtube URL (sorta) to your google home using the following automation.

- service: media_player.play_media
  data:
    entity_id: media_player.google_home
    media_content_id: https://r2---sn-5uaezn6s.googlevideo.com/videoplayback?nh=IgpwcjAyLmF0bDIyKgkxMjcuMC4wLjE&signature=8EE28CECD309F9772452B19BE0ECB7C3FDBF92EF.09F228C2A978D0CF41BF168363BCBEAE2A5B82D9&source=youtube&upn=1hX_AQQxUlo&requiressl=yes&mime=video%2Fmp4&ratebypass=yes&aol=yes&lmt=1471075243852831&ip=2602%3A306%3Ac4ea%3A4e20%3A6374%3A15f%3Add2%3Ac344&expire=1484819790&mm=31&ipbits=0&mn=sn-5uaezn6s&pl=52&mt=1484798144&dur=36001.657&mv=m&initcwndbps=5000000&ms=au&itag=22&sparams=aol%2Cdur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cnh%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&id=o-AGaeR8fidCRavdtmBFTIqi41yoRZdZGMbnKtfuBfHt-H&ei=7jiAWOzCNoGKqwWhlonwBg&key=yt6
    media_content_type: audio/mp4

However, you can’t currently cast a normal youtube url. In order to find the direct link that the google home (and maybe all cast devices?) require, you need to use something like youtube-dl. GitHub - ytdl-org/youtube-dl: Command-line program to download videos from YouTube.com and other video sites

To get the url from youtube-dl, you can

sudo pip install --upgrade youtube_dl
youtube-dl -g --skip-download https://www.youtube.com/watch?v=RzACrNBYKFE&index=1&t=19s&list=PLq5sdyyflMdaurq02V-p1wr5tjKKmuOMX

Once you get the URL from that command, you use it in your configuration. BUT the url expires and you have to pull it at least once per day, which defeats the purpose of the automation. :stuck_out_tongue:

I propose skipping the manual step, and allowing a configuration like this.

- service: media_player.play_media
  data:
    entity_id: media_player.google_home
    media_content_id: https://www.youtube.com/watch?v=RzACrNBYKFE&index=1&t=19s&list=PLq5sdyyflMdaurq02V-p1wr5tjKKmuOMX
    media_content_type: audio/youtube

When the automation runs, it would recognize the youtube content type, run the python youtube-dl lib to get the real url, and send that to the cast device. Preferably with some caching. :smiley:

Let me know if I missed a better way to do this with current config, I have been working on automatically casting sleep music to google home for a while now and I’m almost there.

Thanks!

I got a decent start on this, and it works, but I’d need to do a lot more testing before I’d feel comfortable with a pull request.

It’s an update to cast.py / play_media to pull the url via youtube-dl when media_type = youtube. My Python is super sloppy so I’m hoping it inspires someone else. :smiley:

credit goes to Stack :smiley: http://stackoverflow.com/questions/18054500/how-to-use-youtube-dl-from-a-python-program and youtube-dl https://rg3.github.io/youtube-dl/

2 Likes

HASS uses pychromecast which already has a youtube controller taking a youtube video id. Using that could be a simpler solution.

1 Like

That’s a good point. @balloob I assume you would prefer this in the youtube controller for pychromecast?

Any progress on this?

Voted as my most desired feature addition. If this can be a bit more generic to allow us to cast YouTube URLs to any Casting endpoint that would be great!

I have just added pull request to home assistant repo (https://github.com/home-assistant/home-assistant/pull/8369) to add support for this feature

2 Likes