Search code examples
pythonpython-3.xyoutubeyoutube-dlmplayer

Streaming a YouTube playlist on Raspberry Pi and Python


I'm working on a Voice Assistant based on the Raspberry Pi (2 B+), and one of the functions I was looking to integrate was to play my playlist whenever I want it to.

I tried out using youtube-dl and mplayer within os.system like so, inspired by this answer

youtube-dl --playlist-random -o - "https://www.youtube.com/playlist?list=PLFepKcct_CJG0mu-nb-HvQ52FRKTEO6hT" | mplayer -

I understand that youtube-dl will download the video and send the stream piped to mplayer which will play the music. What is happening is that the code is playing the first video flawlessly, but as soon as the video ends, mplayer just stops at whatever was the last frame. It's not continuing to the next video even though I can see youtube-dl is to downloading videos in the playlist.

Is there any way I can play multiple songs in a playlist using youtube-dl seamlessly?


Solution

  • You can try omxplayer instead... try this small sh script with named pipe first mkfifo /home/midia/omxfifo then save this script below script.sh and chmod +x:

    #!/bin/bash
    FileToPlay="$1"
    for LINK in $(cat $FileToPlay)
    do
        echo "Playing : $LINK"
    
        omxplayer -o hdmi --vol -2 $(youtube-dl -g -f mp4 $LINK)</home/midia/omxfifo>/dev/null 2>&1 & echo -n '' >/home/midia/omxfifo
    
    
        echo "$LINK" >> watched.log
    done
    

    call the script like this:

    ./script.sh file_with_yt_url.txt