This tutorial explains, how you can program your Pi so that it automatically tries to connect to a specific YouTube live stream link at startup and always tries to reconnect if the connection is lost. My personal use case was for my grandpa to watch a live stream of his local church without the need to understand YouTube/internet. If you want a permanent stream of a specific channel, use the following link format: https://www.youtube.com/channel/<channel-id>/live
(ID can be found with online tools like YouTube Channel ID Finder). This Gist should also work with other streaming services, if they are supported by Streamlink.
- Install a useful OS. This tutorial assumes the Raspberry Pi OS (32-bit).
- Setup an internet connection.
- Install the latest stable version of Streamlink. Older versions have problems with the more recent YouTube API. For the installation the following command should work. More information can be found here.
pip3 install --user --upgrade streamlink
- Setup a autostart script:
In this file you have to paste following code, replace
mkdir $HOME/.config/autostart nano $HOME/.config/autostart/autostream.desktop
<youtube-link>
and<seconds-until-retry>
with values. Save it afterwards.Explanation of the command:[Desktop Entry] Type=Application Exec=bash -c 'while :; do $HOME/.local/bin/streamlink <youtube-link> best -p vlc -a "-f" --retry-streams <seconds-until-retry>; sleep <seconds-until-retry>; done'
while :; do [...] done
: Infinity loop wrapper, which always restarts Streamlink, if the stream is closed.$HOME/.local/bin/streamlink
: Path of installed streamlink versionbest
: Best video quality-p vlc -a "-f"
: Use VLC player in fullscreen--retry-streams <seconds-until-retry>
: If stream can not be found, time to wait until a reconnect is tried.sleep <seconds-until-retry>
If stream is closed or connection is lost, time to wait until streamlink is restarted
- Make the script executable:
chmod +x $HOME/.config/autostart/autostream.desktop
- Reboot the system:
Now the live stream should start automatically, if it is currently accessable.reboot