Friday, February 22, 2013

Using a Raspberry Pi as Digital Signage

I developed a digital signage application for my employer some time ago, but we ended up using Mac Mini's at the time. With the advent of the Raspberry Pi computer I thought it might make a good candidate for DS, but I had a heck of a time when it first came out getting any kind of movie playlist working.

I tried VLC, some other media players, trying to find a way to make the built in omxplayer play a playlist, nothing. Nada. Nothing worked. I could not figure out how to make an unattended digital signage device.

So I gave up. For a couple months.

When I decided to revisit the issue, a little research showed that nobody had really solved the playlist issue, so I thought I'd try to find another way around it. And I did.

It was always easy enough to just have a script that plays videos in sequence using omxplayer, however this solution wasn't quite perfect because the stream would have a 1 second pause that would show the desktop while it switched videos. The solution turned out to be simply turning on the screen saver. May sound easy, but it took me a while to figure it out!

So other than a slight pause between videos, this solution works perfectly. Here's the steps to get it going:

WARNING! If you do this, the screen saver will kick in every 1 second of idle time, even if you move the mouse. Make sure you have SSH enabled so you can remote into the system to disable this later if you need to!

1. Set up Raspbian on your Pi device according to their instructions.
2. Make sure you're signed on as pi, not root, then from a command line enter:
sudo apt-get install x11-xserver-utils
mkdir /home/pi/digitalsignage
mkdir /home/pi/digitalsignage/movies
nano /home/pi/digitalsignage/piScreensaver.py

Here I'm using a python script to use the X11 xset command to turn the screen saver on:

import os,sys,time

def SetScreensaver():
    time.sleep(15) # wait for the system to finish booting
    for x in range(0,5): 

        # try turning it on once every minute for the next 5 
        # minutes just in case!
        os.system('xset -display :0.0 s 1 60 -dpms')

        # The -dpms turns the power save option off
        time.sleep(60)
    return
               
if __name__ == '__main__':
    SetScreensaver()
    sys.exit()

Press CTRL-O [ENTER] and CTRL-X to save and exit.

Then we create the player module:
nano /home/pi/digitalsignage/piPlayer.py

Enter this text:
import os,sys,time

def RunPlaylist():
    while 1:
        path    =    '/home/pi/digitalsignage/current.m3u' 

        file    =    open(path,'r')
        files   =    file.readlines()
        file.close()
        os.chdir('/home/pi/digitalsignage/movies')
        for file in files:
            filename = file.strip()
            if '.mp4' in filename:
                os.system('/usr/bin/omxplayer /home/pi/digitalsignage/movies/%s>/dev/null' % filename)
    return 

              
if __name__ == '__main__':
    RunPlaylist()
    sys.exit()


Press CTRL-O [Enter] then CTRL-X to save and exit again.

Next you have to copy your MP4 video files to the /home/pi/digitalsignage/movies folder.
Once done, create the playlist. Let's say for this example we have "video1.mp4" and "weather.mp4", and you've uploaded them to the /home/pi/digitalsignage/movies folder. Type:

nano /home/pi/digitalsignage/current.m3u

Enter the following:
video1.mp4
weather.mp4

Press CTRL-O [Enter] then CTRL-X to save and exit.

Finally, modify the startup:

crontab -e

And add these two lines to the bottom:

@reboot /usr/bin/python /home/pi/digitalsignage/piScreensaver.py
@reboot /usr/bin/python /home/pi/digitalsignage/piPlayer.py


Press CTRL-O [Enter] then CTRL-X to save and exit.

Then reboot:

sudo shutdown -r now

That's it! When your Pi reboots, it will come up with a full video playlist player and start automatically.

Now, I've actually wrapped some other functionality around it for my employer, such as having a web-based management program that will load new videos to the Pi then restart the playlist, that sort of thing, but if you're looking to set up the playlist functionality this should hopefully help!

21 comments:

  1. Thanks for the sharing information about the video playing in digital signage.

    ReplyDelete
  2. Thank you for sharing the information. Explore wide arrays of innovative digital signsavailable over the online stores. Reach out to their websites to know more about the services offered at reasonable prices.

    ReplyDelete
  3. is it possible to watch videos that are hosted online ? in ur code i do not see any way that i can send some commands to Omxplayer, is this possible?

    ReplyDelete
    Replies
    1. ha now i see it... hmm very interesting, but again wont the omxplayer play online videos anyways?

      Delete
  4. Sure, it's easy.

    omxplayer url-to-video

    Example:

    $ omxplayer http://www.youtube.com/watch?v=abcdefg123545

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  5. A very creative solution. I've been pondering this issue for weeks. Thanks for sharing.

    ReplyDelete
  6. In the nearly a year we've been running this, it's proved to be an extremely stable solution too. I never have to reboot the units, unlike the Macs that were running something comparable.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. what i meant is the ability to go online and get a .m3u playlist instead of one locally

      Delete
    3. Yes you can set up a cronjob to retrieve playlists say every hour.

      Delete
  7. My challenge now is to automatically update the playlist while its already in the loop and also be able to set up videos to play at a particular time

    ReplyDelete
  8. The solution I came up with is a little different; I always let the playlist finish, but then as the loop starts again I force a reload of the playlist so at least it will dynamically load new content without interrupting any videos.

    ReplyDelete
    Replies
    1. How were you able to force it reload

      Delete
    2. Since it's a python program controlling the playlist (just a loop feeding a list of files, one at a time, to the omxplayer app) the program was able to finish one loop then reread the playlist from disk.

      Delete
  9. Your information that you have post is really great it has full of information about digital signage to know how to promote your starting small business. Thanks a lot and hope that you will post more site that are related to this site. If you want to know some more about digital signage and wanna buy for yourself the best digital signage software, just click here.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. This is a great article loaded with unique information on Digital Signage. I really enjoyed the content and agree with your views. Thank you for sharing.

    ReplyDelete
  12. Well written post! Digital Signage are about creating the most impactful experience for the customer to attract attention, provide information or support the selling process.
    chromebox digital signage

    ReplyDelete
  13. Displays and signages are most commonly used by different companies to communicate with their customers. These communications may be related to the recent launched product, services or any announcements. They are also used to promote & advertise different products in an effective manner.

    display signage software

    ReplyDelete