Looping video playlist with Omxplayer on the Raspberry Pi

Looping video playlist with Omxplayer on the Raspberry Pi

The Raspberry Pi comes with an awesome little video player called Omxplayer that is very capable of playing back full 1080p video perfectly when encoded correctly in H.264/AAC. One problem is the current lack of playlist support in omxplayer, so this post explains how to create a bash script that will permanently loop through and play a directory of videos.

 

First install omxplayer:

sudo apt-get install omxplayer

Now create this script named for example “videoplayer.sh”:

#!/bin/sh

# get rid of the cursor so we don't see it when videos are running
setterm -cursor off

# set here the path to the directory containing your videos
VIDEOPATH="/mnt/storage/videos" 

# you can normally leave this alone
SERVICE="omxplayer"

# now for our infinite loop!
while true; do
        if ps ax | grep -v grep | grep $SERVICE > /dev/null
        then
        sleep 1;
else
        for entry in $VIDEOPATH/*
        do
                clear
                omxplayer $entry > /dev/null
        done
fi
done

Save the script, make it executable, and then run it as follows:

./videoplayer.sh

Obviously this is fairly basic – it just loops through a directory of videos and plays them one after another but in our case this was exactly what we wanted. It doesn’t even check that the files are actually video files although it would be easy to add this in. Regardless, it is hopefully a good starting point for more complex scripts.

Enjoy!

14 thoughts on “Looping video playlist with Omxplayer on the Raspberry Pi”

  1. Peter Lindahl
    HiI tried your script and it works like a charm but there was one little snag.
    The movie was not stretched over the entire screen so the upper and lower part of the screen could be seen.
    I changed an entry and got it to stretch all the way.
    Hope this helps someone. :)

    #!/bin/sh

    # get rid of the cursor so we don’t see it when videos are running
    setterm -cursor off

    # set here the path to the directory containing your videos
    VIDEOPATH=”/mnt/storage/videos”

    # you can normally leave this alone
    SERVICE=”omxplayer”

    # now for our infinite loop!
    while true; do
    if ps ax | grep -v grep | grep $SERVICE > /dev/null
    then
    sleep 1;
    else
    for entry in $VIDEOPATH/*
    do
    clear
    omxplayer -r $entry > /dev/null 
    done
    fi
    done

    Reply ↓
    1. ChrisPost author
      Hi Peter, glad you found it useful and thanks for the idea of using the -r switch. In our case we didn’t find it necessary but its good to know about anyway!
      Reply ↓
      1. Sam
        Great script, but I need a little help to modify it for something similar that I am doing.I run the script on startup so that it starts playing some videos in the specified folder, but when I update/change files in that folder over the network it only plays what was initially in the folder when the script started. I tried to delete files and I will play the files left in the folder. When all files are deleted it will just not do anything just a black screen.

        I’d like a modified script that will be able to loop and start playing new files from that folder if video files are added into the folder while the script is already running. Can anyone help me please? I’m no good at programming and scripting :(

        Reply ↓
      2. Pepelu
        For clear the screen, typing this line:sudo sh -c “TERM=linux setterm -foreground black -clear >/dev/tty0″

        Reply ↓
  2. HilaSupport
    Hi everyone and thanks!
    I am experimenting with the Raspberry Pi, and would like to shuffle the playlist items, so the next time the player runs the playlist, the items will run in a different sequence.
    I went over OMXPlayer command options and Key Bindings, but found nothing helpful. any idea?
    Reply ↓
  3. George
    Hello and thank you for sharing this. Looping video is what I bought the Raspberry Pi for, and this is pretty much perfect. I’ve used your script to launch a video at startup (by calling it from the autostart file in /etc/xdg/lxsession/LXDE), BUT the problem I have now is closing it!Would it be possible to add a simple command to quit the process? I know nothing about coding and have been feeling my way through this on forums, but I’m almost there! Could you help me with this?

    Thanks in advance, George

    Reply ↓
  4. Rene
    Hi,great script! We are using it for play videos in a loop in our store.

    But the problem is, that the audio output is only via the 3,5mm jack. Is there any way to give audio out via HDMI?

    thanks

    Reply ↓
  5. Moses
    Hey folks, Awesome Article it’s been great help finding this. I have a question/proble..
    I got everything working just how you explained above. When I SSH to my RPI and run the scripts as ./videoplayer.sh it plays the movies just fine but when I close the terminal window on the remote machine the video stops playing and I get a black screen.Any thoughts?

    Reply ↓
  6. Sunil
    Hi all,How can i Control the Video in omxplayer using c script?
    How can i pause,stop,play the video using Script?

    please help me…

    Thanks and regards
    sunil

    Reply ↓
  7. Omar
    Script plays only the first video and keeps looping on that video, but it doesnt play the rest of the videos in the folder I specified in the script.
    Reply ↓
  8. Luis C
    Supports web URL as address pathfor example:
    VIDEOPATH=”http://mydomain.com/videos”

    ??

    I have tested ‘omsplayer’ using web adress and works, but with simple command line
    example:
    omsplayer -o hdmi http://domain.com/videos/video.mp4

    So Im wonder if We can use HTTP adress as path…

    Reply ↓