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. The page here discribes my learning process.

XBMC

XBMC, a media center that has been adapted for the Rapsberry Pi seemed a nice starting point to get a video looping. But although it works great as a media center, it doesn't offer the video loop feature we hoped for. And it also loads unnecessary interfaces and menus we didn't need for this project. So we switched to a default Raspbian OS.

OMXplayer

Omxplayer is the default video player for the RasPi, but it doesn't have a “loop” option (yet).

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.

The “no loop” feature has been a problem for many users (just search for “OMXplayer loop” and you'll find countless forum posts). Many have opted for a bash script that checks if the omxplayer has finished playing and then relaunch it. 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 turning terminal font colors to black. But this isn't pretty, anyway.

Future?

I hope that in the near future, omxplayer will have that loop feature added to it. Which will solve many of these crude hacks. 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. And it's been waiting for a merge since March 15, 2013

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.

hello_video example

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.

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

You're done ;)