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!
Tagged with: art, exibition, omxplayer, python, RaspberryPi, videoart
Categorised as: Raspberry Pi
9 Comments
-
Hans Kornett says:
Hallo Julian,
You have done a ver nice project “Raspberry Pi playing a video in loop”
I have tried your python programm, but I get always the same errorFile “./omxloop”, line 8
for infile in glob.glob(os.path.join(path, ‘*.mp4′)):i have no experience in Python, so it would be nice if you could help me to run the programm:
Thanks Hans-
admin says:
Hi Hans
Glad you liked my post.
It seems I have forgotten to put in a couple of tabs.
Python is sensitive to whitespace, so when starting a while or for loop the stuff that belongs inside that loop must have one tab in front of it per loop. Like this:
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])
And by **TAB** i ofcourse mean one click of the tab button, the result is that the code belonging inside the loop is placed more to the right than the loop statement.Did this work for you?
-
-
Toitoine says:
Hello Julian !
This scrip works well but if I want to kill this how can I do this ? ;SThanks !
-
admin says:
Hehe, well I just unplugged the power. Dirty, but it works.
If you want to make the script not run, just delete it from /etc/init.d/ and reboot.
You still have ssh access while the script is running.
-
-
AbeDev says:
Thanks so much for figuring this out and putting this great little tutorial together.
I’m playing multiple short videos and between each one it goes back to the command window. Do you know any way to prevent this or minimize the transition?
Thanks!
-
admin says:
Well, I have had the same problem on some videos, while others transition just as planned.
Hopefully omxplayer will be updated to support looping or playlists, until then, this is the best I can do…-
AbeDev says:
Thanks for the follow-up and thanks again for putting together the awesome tutorial!
-
-
-
nike free says:
A good example ah.
nike free http://www.chaussurewaikikicher.com