Infinite Video Loops on a Raspberry Pi

script I recently upgraded my office by moving to a great new location. As part of the move I added a ton of new space to accommodate our rapid growth and even added a “play area” for the staff so they can burn off some of the frustrations of the “daily grind” with a bit of Ping Pong and, very soon, Xbox. I also added a very nice reception area with a TV where I could run a continuous loop of some of our marketing videos.

I decided to go with a Raspberry Pi to run the movies since it’s low cost, low power and very easy to hide behind the TV. I installed Raspbian and since there’s no GUI player that I could find to queue up the movies to run in a loop, I wrote a simple bash script to run a loop using omxplayer.

#!/bin/bash

# the path to the directory containing my videos
VIDEOPATH="/home/pi/Desktop/videos/"
SERVICE="omxplayer"

# the infinite loop!
while true; do
for entry in $VIDEOPATH/*
do
omxplayer $entry > /dev/null
sleep 1;
done
done

This script seemed to work just fine but not long after I started it up, I ran into issues. The first problem was the gap in playback; it wasn’t seamless. While not a big issue, it looked ugly because I could see the desktop and the console window in the gap. To fix this I maximized the console and modified my script to turn off the cursor and set my text to black so all I could see was a black screen during the gap. Not the best solution, but I could live with it.

#!/bin/bash

cleanup()
# cleanup function
{
setterm -cursor on
setterm -foreground white -clear
return $?
}

control_c()
# run if user hits control-c
{
echo -en "\n*** Exiting! ***\n"
cleanup
exit $?
}

# trap keyboard interrupt (control-c)
trap control_c SIGINT

# main() loop
# get rid of the cursor so we don't see it when videos are running
setterm -cursor off
setterm -foreground black
setterm -clear

# the path to the directory containing my videos
VIDEOPATH="/home/pi/Desktop/videos/"
SERVICE="omxplayer"

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

Again, this seemed to work just fine but then I ran into my second issue. This time it was the screen going to sleep. A quick Google search reveals that this is a common problem and there are lots of suggestions for fixing it. I experimented with a few options and what worked for me was to modify the Light Display Manager configuration.

sudo nano /etc/lightdm/lightdm.conf

Now look for [SeatDefault] and insert this line below:

xserver-command=X -s 0 dpms

This modification absolutely resolved the sleep issue but then I ran into my third problem. For some reason the omxplayer process seems to hang. I see the desktop on the TV but no movie. When I look at my processes I see omxplayer running. There’s probably a more elegant fix, but I decided to take a brute force approach to resolving the issue. Rather than just play every video in my videos folder, this time I explicitly start each movie and use bash’s built-in control operator to fork processes. Doing this means my script keeps running even if the omxplayer process hangs. Then I sleep for the same duration as the video and issue a pkill command to nuke the omxplayer process just in case it’s hung.

#!/bin/bash

cleanup()
# example cleanup function
{
setterm -cursor on
setterm -foreground white -clear
return $?
}

control_c()
# run if user hits control-c
{
echo -en "\n*** Exiting! ***\n"
cleanup
exit $?
}

# trap keyboard interrupt (control-c)
trap control_c SIGINT

# main() loop
# get rid of the cursor so we don't see it when videos are running
setterm -cursor off
setterm -foreground black
setterm -clear

# the path to the directory containing my videos
VIDEOPATH="/home/pi/Desktop/videos/"
SERVICE="omxplayer.bin"

# the infinite loop!
while true; do
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
pkill $SERVICE
sleep 2;
else
omxplayer ${VIDEOPATH}movie1.mp4 > /dev/null &
sleep 100;
pkill $SERVICE >/dev/null >2&1
omxplayer ${VIDEOPATH}movie2.mp4 > /dev/null &
sleep 67
pkill $SERVICE >/dev/null >2&1
fi
done

My final version of the script has been working beautifully and has proven to be a solid solution for running movies in a continuous loop on the Raspberry Pi platform.

Until next time – GEEK OUT!

~GT~

Leave a Comment

Your email address will not be published. Required fields are marked *