Raspberry Pi Video Loop

For a video installation by PinkSyrup, I was asked to configure a Raspberry Pi to play a HD 1080p 48s video in a perfect loop at startup. Although, this seems to be an easy task, we ran into some problems and found some temporary solutions. This page here was mostly describing my learning process.

But after a year, there has been a lot of development going on everywhere. And now there is multiple solutions available. Although the one and only that would make everyone happy is not working (yet).

First question: does your video needs sound or not?
If it does not have sound, jump immediatly to the hello_video solution. It's the quick and easy one that has been working since day one.

If you need sound, then there is multiple ways with different results and coding styles: Bash scripting, Python or C++.

OMXplayer

Omxplayer is the default video player for the RasPi.

The advantage of the OMXplayer is that you don't need to start an X server to play a video. It fully uses the GPU capabilites of the Raspi, which makes it a fast and reliable player for full HD video. It also plays sound with the video.

OMXPlayer has “loop” option.

omxplayer --loop  video.mp4

But (still not | or yes | or partly see issue #187) working with this version:

Build date: Sat, 22 Mar 2014 20:58:15 +0000
Version   : 39e6342 [master]

Developpment happens here: https://github.com/popcornmix/omxplayer

Bendenoz has been working on a fix to get the loop working. But I haven't had the time and energy to compile and test it.

Until this “loop” option is working in the official version, we'll have to come with other tricks.

March 2015 : Visited back the repo, and it seems the loop option works now, with some limitations and some bugs as indicated by some issues.

Bash scripting

This script will start an instance of omxplayer and will watch it. When omxplayer has finished playing the video clip, a new instance playing the same clip is created. And so on…

#!/bin/sh
 
SERVICE='omxplayer'
while true; do
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "running" # >> /dev/null
else
omxplayer -o hdmi /path/to/your/video/file/video.mp4 &
 
fi
done

But this raises 2 problems. First, you have about a one second delay between each loop. Second, the terminal is visible during that delay, which is quite visually disturbing. That second problem is often “solved” by hiding the terminal.

Python scripting

Ellen has come with a pythonic solution. She describes it here: http://www.sundh.com/blog/2013/10/loop-videos-seamlessly-omxplayer/

I have not taken the time to try it out. It uses pyomxplayer to control multiple instances of OMXPlayer started at once. The idea is that you would start one instance while maintaining another one on pause. Then watch the first instance and right before it reaches the end of the video, start the second one. And so on.

openFrameworks + ofxOMXPlayer

openFrameworks is a C++ framework for artists and coders. There is a version of it that you can install on RaspberryPi (with Raspbian).

And ofxOMXPlayer is an addon by Jason Van Cleave that brings the hardware accelerated video display to openFrameworks for RasPi.

You will need to install a lot of libraries and take some time to compile openFrameworks. But the basic examples that come with ofxOMXPlayer addon all work and have a video loop function working.

The advantage of using openFrameworks is that you can do more than just loop videos and have overlay text, logos, etc… But, though openFrameworks prepares a lot for its users, this requires some knowledge of C++.

hello_video

In the default installation of the Raspbian OS, you'll find many code examples written in C to start coding with the RasPi. Those examples are located at /opt/vc/src/hello_pi/

There is a hello_video example that plays h264 videos which can be modified to play a video in a loop (source). In the file video.c, you'll have to replace this line:

if(!data_len)
    break;

by this one:

if(!data_len)
    fseek(in, 0, SEEK_SET);

and compile it by running

make

Be sure to compile the /opt/vc/src/hello_pi/libs/ilclient/ first or you'll get an error.

With the hello_video.bin file that is generated, you'll only be able to play videos encoded in h264 with no sound. But you'll have a perfect loop, with no gap.

GStreamer

I've heard of a GST-OMX plugin for gstreamer. So it seems GStreamer would talk directly to the GPU and be a good candidate for video playback on the Raspi, with the added features that GStreamer can offer. I have yet to try this option. If you have some feedback, don't hesitate to fill the page with your findings.

Useful tricks

Hiding the terminal

A prettier solution to hide the terminal has been suggested to me by Jakob Wierzba:

setterm -blank 1

This command tells the terminal to hide itself after 1 minute of inactivity. This means the terminal will be again visible when needed by just pressing a key. Way more usefull and convenient that settings fonts to black. Insert this in your bash startup script.

You'll need to write this command in your /etc/rc.local file to work at startup.

Transcoding a video to h264

This hello_video example is quite strict on what type of video file can be played. If your video file is in a mp4 container, it won't play. The easiest solution to get a working file for me was to start with a list of jpeg files with a 1920 x 1080 resolution and to process it with ffmpeg. Here's the code that worked for me:

ffmpeg -i big_brother%04d.jpg -s hd1080 -vcodec libx264 big_brother.h264

(%04d means the files are numbered with four digits. Ex: big_brother0001.jpg, big_brother0002.jpg,…)

Playing on startup

To get the video to play as soon as the RasPi starts, I choosed to use Cron instead of init.d scripts. It's easier to set up and it works as expected. Here's how to set it up:

# create a crontab file
touch cronfile
# edit crontab file
crontab -e cronfile

Copy (and adapt) this line in your crontab file and save:

@reboot /path/to/your/script/./hello_video.bin /path/to/your/video/file/big_brother.h264

and restart the RasPi

sudo reboot