Skip to content

Instantly share code, notes, and snippets.

@christianll9
Last active May 12, 2024 00:26
Show Gist options
  • Save christianll9/ef348a4fe181026288f6c7bcc9f0e98c to your computer and use it in GitHub Desktop.
Save christianll9/ef348a4fe181026288f6c7bcc9f0e98c to your computer and use it in GitHub Desktop.
How to stream permanently from a YouTube channel with a Raspberry Pi

Abstract

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.

Tutorial

  1. Install a useful OS. This tutorial assumes the Raspberry Pi OS (32-bit).
  2. Setup an internet connection.
  3. 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
    
  4. Setup a autostart script:
    mkdir $HOME/.config/autostart
    nano $HOME/.config/autostart/autostream.desktop
    In this file you have to paste following code, replace <youtube-link> and <seconds-until-retry> with values. Save it afterwards.
    [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'
    Explanation of the command:
    • while :; do [...] done: Infinity loop wrapper, which always restarts Streamlink, if the stream is closed.
    • $HOME/.local/bin/streamlink: Path of installed streamlink version
    • best: 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
  5. Make the script executable:
    chmod +x $HOME/.config/autostart/autostream.desktop
    
  6. Reboot the system:
    reboot
    
    Now the live stream should start automatically, if it is currently accessable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment