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.
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:
First and foremost, I would like to cast a regular video, for example:
Youtube URL: https://www.youtube.com/watch?v=P0cSpPQ29TI
Casting a Channel by ID, for example:
Youtube Channel URL: https://www.youtube.com/channel/UCs5Y5_7XK8HLDX0SLNwkd3w
Casting a Channel by @username, like:
Youtube Channel URL by name: https://www.youtube.com/@code
Casting a Playlist by ID, like:
Youtube Playlist URL: https://www.youtube.com/playlist?list=PLvKOuwv1u7bGri7IBkLYhmhMRnvtAlc4N
Casting the Latest Video by hashtag, like:
Youtube Hashtag URL: https://www.youtube.com/hashtag/mentalnorazgibavanje
or a combination of Channel ID and search:
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:
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:
- The Playlist ID RSS is located at:
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 actually 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 further effort is futile:
youtube-dl "https://www.youtube.com/watch?v=yX6KoZ-fmQ4" --format=worst
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
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
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.
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
Playing Tune-In Radio Stations
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.