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!
Hi
I 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
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!
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 :(
For clear the screen, typing this line:
sudo sh -c “TERM=linux setterm -foreground black -clear >/dev/tty0”
how can i put that command “sudo sh -c “TERM=linux setterm -foreground black -clear >/dev/tty0″
into a .php command in shell exec? i have an issue with the black screen..
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?
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
Is there any tip for true video loop (last frame connecting first frame with no interval}?
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
Late reply… but for reference of later visitors to the thread…
using the ( -o HDMI ) argument you can force HDMI audio on an omxplayer call.
thanks this is very usefull solution.
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?
I would suggest installing “screen”, as it is intended to be used for keeping remote sessions alive. To install it (raspian) ‘sudo apt-get install screen’ .”screen” can be invoked simply by $ screen at the prompt just after connecting to your pi then you can start your movie $ omxplayer -o hdmi movie.mkv wait for the movie to play on your screen, you may then detach it by pressing CTRL-A and then CTRL-D in quick succession. Your ssh session can then drop and the movie will continue playing. Not a bad learning reference -> http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/ to get started. Plenty of good information regarding screen out there.
I’d suggest escaping $entry as a string to handle any filenames with spaces. This worked for me:
omxplayer “$entry” > /dev/null
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
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.
Supports web URL as address path
for 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…
“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?”
You have two options:
1. Add the script to /etc/rc.local and it will execute immediately after boot.
or
2. “sudo apt-det install screen -y” – this litthe program keeps the session open. Just launch it with “screen -q” from the ssh console and it will open a session-in-session, so after you close the remote connection the created session will still be there and the playing will not cease. To restore the session next time toy have to enter “screen -c” from the ssh console.
Hi,
Is there any way you could explain how to do this to a complete noob?
1) I have omxplayer on the pi.
2) I can copy your script nano:
sudo nano
3) paste in the script
Now i am utterly lost. I can press ^O but how/where do i put it?
How do i make it executable.
I would really appreciate any help you can give on this.
Thanks!
Ok, i found the answers myself. Here:
http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/scripts
Now i have another question. How do you make it run on startup?
I gave it a go but killed my raspbian install. I got it stuck in a loop that it could not get out of. It just had a black screen. :(
Nice script. I want to do something different. I want to be able to play certain files not all files in the folder. I wrote a txt file with the titles of the videos i want t play. They are seperated by new line. How can i play only the files in the txt file. Hope am not asking too much. I have no experience in scripting. Thanks
Hello,
great script!
I have two problems.
How can I make that the loop seamless? And how can I make that I have a full screen?
please help me!
Thanks
Luisa
thx for the script very helpful.
but i had a problem. the omxplayer run videos but after 1 hours omxplayer is running but no display on tv. i can’t know the reason of this problem i tested many solution gpu-ram and swap free memory.
i’m still having this problem
can any one help plz