Casting YouTube in Home Assistant

Casting YouTube in Home Assistant

My aim was to cast a Youtube video on Google devices with the help of Home Assistant, but it seems to be impossible, and I’ll explain why. Actually, it might be possible on a significantly faster host than RaspberryPi 3 and its weak SD card, but practically it’s not possible on them, right now.

During my experimentation, recommendations kept coming back for integrations like RESTful, Scrape, and the most important one: Media Extractor.

There is also a youtube integration under custom-components/youtube that gives information about the latest video on a channel, but it doesn’t bring me anything new that I don’t already have.

What I Want to Cast

There are several “entities” that I would like to cast, including the following:

  1. First and foremost, I would like to cast a regular video, for example:
    Youtube URL: https://www.youtube.com/watch?v=P0cSpPQ29TI

  2. Casting a Channel by ID, for example:
    Youtube Channel URL: https://www.youtube.com/channel/UCs5Y5_7XK8HLDX0SLNwkd3w

  3. Casting a Channel by @username, like:
    Youtube Channel URL by name: https://www.youtube.com/@code

  4. Casting a Playlist by ID, like:
    Youtube Playlist URL: https://www.youtube.com/playlist?list=PLvKOuwv1u7bGri7IBkLYhmhMRnvtAlc4N

  5. Casting the Latest Video by hashtag, like:
    Youtube Hashtag URL: https://www.youtube.com/hashtag/mentalnorazgibavanje
    or a combination of Channel ID and search:
    https://www.youtube.com/channel/UCloa3CgQMtGSeqjLkpQlEFg/search?query=mentalnorazgibavanje

Note that in the case of a “hashtag” search, I eventually found a playlist with a regular ID, which is fortunate as I couldn’t find a way to cast a playlist that included a search.

Uncover the Real Channel ID

Casting “@user” is essentially the same as casting by Channel ID, we just need to manually uncover the Channel ID that hides behind the @user nickname. Since there is no perfect way to do this, the closest approximate short solution is to load any video from that user and type into the JS console:

document.querySelectorAll('[itemprop="channelId"]')[0].content

This line should reveal the true Channel ID as it is located somewhere within the HTML element that has “itemprop” with a value of “channelId”, as described here.

Now the problem of casting is reduced to: video-by-id, channel-by-id, playlist-by-id, hashtag.

RSS Feeds If Needed

Although I haven’t found a specific use for them, since my problem isn’t in uncovering the URL but rather in actually streaming it, I will nonetheless list possible RSS feeds:

  • The Channel ID RSS feed is: https://www.youtube.com/feeds/videos.xml?channel_id=<id>
  • The Playlist ID RSS is located at: https://www.youtube.com/feeds/videos.xml?playlist_id=<id>

Media Extractor

Integrating the media_extractor is the only option available for YouTube, relying on ytdl-org/youtube-dl as the engine. Unfortunately, this seems to result in the video being downloaded before it is played, which is a significant drawback on my less powerful RPi3. Thus, there’s no way to directly stream videos from YouTube to the Cast device.

Seeking Minimal Bandwidth

It turns out that I can successfully play some videos, but only if they are of a minimum size. The “worst” format can be specified with the hope that the video file in that format is not large.

For example, the following video doesn’t have a “small” format that contains both video and audio, as the smallest is even 200M:

youtube-dl "https://www.youtube.com/watch?v=yX6KoZ-fmQ4" --list-formats

And such a video is simply impossible to play on my devices.

Since the backend is actually youtube-dl, it’s best to first check with the following command if there’s a typically smaller resolution, and if not, I think the further effort is futile:

youtube-dl "https://www.youtube.com/watch?v=yX6KoZ-fmQ4" --format=worst

For some videos there are smaller formats, like for example: video-yes, but for some there aren’t video-no.

Normally, playback of all types of media goes through the Home Assistant service call:

service: media_extractor.play_media
data:
  entity_id: media_player.nest_hub
  media_content_id: "https://www.youtube.com/watch?v=DKaHufYz1bA"
  media_content_type: video/youtube

The media_content_type can be “music”, “tvshow”, “video”, “episode”, “channel” or “playlist music”, whatever that means.

Opting for Smaller Files

You should add the following lines to the global configuration, which instructs our media player (Nest Hub) to utilize youtube-dl and download the worst and therefore, the smallest format, since the screen size is also small and therefore, there won’t be much difference:

media_extractor:
  customize:
    media_player.nest_hub:
      video: worst
No Dice

Regardless of the efforts I’ve made, everything was too slow. Perhaps, if I had replaced the SD card with an SSD disk or if the RPi3 was an RPi4, things would have been different. But as things stand, there’s no way I can play most podcasts from Youtube.

I even tried downloading just audio or video, but that too was slow to be usable.

About youtube-dl Forks

The latest one is yt-dlp/yt-dlp, that is a youtube-dl fork based on the now inactive blackjack4494/yt-dlc fork. Everyone is on scoop install yt-dlp

# to download video in best resolution and save subtitles in SRT format and in selected languages
yt-dlp --write-auto-subs --sub-langs "sr-Latn,sr,bs,hr,en" --convert-subs srt <youtube-url>

WARNING: “-f best” selects the best pre-merged format which is often not the best option. To let yt-dlp download and merge the best available formats, simply do not pass any format selection.


Casting URL to Nest Hub or Google TV?

An interesting way to cast a URL to one of the Google Chromecast devices. AlexxIT/DashCast: DashCast component for Home Assistant

Alternative is CATT? HomeAssistant and CATT (Cast All the Things!) Google Nest Hub as Dashboard with DashCast add-on - Share your Projects! - Home Assistant Community

Cast or share camera stream · AlexxIT/WebRTC Wiki


Playing Tune-In Radio Stations

Integration: music-assistant/hass-music-assistant

To find a radio URL on the Tune-In Radio page - it is explained here Basically, just search the page for “jp_audio” and you will find the stream URL.

date 09. Jul 2022 | modified 29. Dec 2023
filename: Hass.io » Media » Video