Raspberry Pi playing a video in loop

http://www.mindemedia.no/wordpress/?p=16

Raspberry Pi playing a video in loop

So there is gonna be an art exibition where i work, its mainly video art, so they will need six projectors showing one piece of video each, and they are all gonna run in loop, all day long. There are several boxes that can play video, and still be cheaper than the mini-mac, but none as cheap as the raspberry pi. So i decided to see if i could make the Raspberry Pi do the job.

I use one Raspberry Pi, model B on each projector. They are all running Raspbian and I have used omxplayer, wich runs directly from the commandline, as the player and a python script to loop it. The python script is added to init.d so the video will start when the pi starts.

Here is a tutorial on how you can make your Pi into a video loop player.

What you need:

- Raspberry Pi, preferrably model B, (thats what i used anyway), with power supply and network connection.

- a computer to build the SD card and acess the pi through ssh (not really necessary)

Making the SD-card

The first thing you need to so is to setup the SD card, with Raspbian. Raspbian can be downloaded from raspberry.org

There is a good guide to how to get Raspbian on to a SD card on elinux.org

In raspi-config you might want to change the memory split, i have it at 128MB for the graphical.

Installing omxplayer

Once you have the Pi up and running. Log on through ssh (default user: pi pass: raspberry), if you use mac, go to terminal and write

ssh pi@<IP of the Pi>

The IP of the Pi is displayed in the end of the startup sequence just before the logindetails, or you can run #ifconfig

When in ssh, or directly on the pi with a keyboard, run

#sudo apt-get update This is just to make shure apt-get is up to date.

Omxplayer is included in Raspbian.

To be able to read from usb drive through command line, we need an application called usbmount.

#sudo apt-get usbmount

Reboot your pi (#sudo reboot). Once this is all installed and rebooted you will find the connected usb drive at /media/usb. Now we can put a movie file on a usb disc and put it in the pi and run the video by using omxplayer through command line. example:

omxplayer -o hdmi /media/usb/videofile.mp4

The -o hdmi makes the omxplayer output audio through the hdmi. If you want to output audio through the local output, replace it with -o local. omxplayer takes many other parameters as well, which are all described on their github page.

The Python script

Now the omxplayer does not support looping nor playlists at the moment, but thats where python saves the day.

We will write a python script that starts omxplayer with the videofile we want, and does it again when the video is finished. Another way to do this is through bash script, but then you have to return to command line between loops, so I chose to go with python.

Now, when in home directory or whatever directory you might like, cteate a file called omxloop

#sudo touch omxloop

then open it in nano

#sudo nano omxloop

Remember the sudo here, or you might not be allowed to save after you’re done.

#!/usr/bin/python
import sys
import subprocess
import os
import glob
path ='/media/usb/'
while(1):
**TAB** for infile in glob.glob(os.path.join(path, '*.mp4')):
**TAB** **TAB** a = subprocess.call( [ "omxplayer", "-o", "hdmi", infile])

Change ‘*.mp4′ to whatever format you will be viewing, and replace **TAB** with an actual tab.

When youre done writing the script, quit nano by pressing ctrl-X and save the file according to instructions

Next we have to make the file executable

#chmod +x omxloop

Test the script by running

#./omxloop

This should get the Pi to view some video, and play the files on your usb stick one after another.

Making the script run on boot.

The last thing we are going to do is to get the script to start every time the pi starts, that way the pi can view video without keyboard, mouse or web access.

First we copy the script file into the folder /etc/init.d

#sudo cp omxloop /etc/init.d/

Then we have to add it to update-rc.d

#update-rc.d omxloop defaults

Hook the pi up to a screen or projector, put the power in there and enjoy the show!