Looping videos seamlessly OMXPlayer

http://www.sundh.com/blog/2013/10/loop-videos-seamlessly-omxplayer/

Looping videos seamlessly OMXPlayer

Ellen 16:30 on October 15, 2013

As I looked for different solutions for looping videos on the Raspberry Pi seamlessly I eventually came out with a solution that worked quited good for videos looping with sounds. Looking at the possiblities of using the existing example of hello_video/video.c to loop a video wasn’t an option as sound was not supported and syncing sound was not something I wanted to spend time on.

Instead I looked to jbaiter’s OMXPlayer library pyomxplayer build in python. It calls the OMXPlayer through the command line and with some testing I could run several instances of OMXPlayer at the same time. This meant that I could start one movie and pause a copy of the same movie in the background. When the first movie came to an end I could start the second movie which was an exact copy of the first one in order to get the seamless loop. No terminal windows showing and no gap between the movies. And so the loop goes on.

By having 2 different names of the same movie I was in complete control of the omxplayer processes running in the background to play another movie on top of this.

I start off loading both movies by calling making to OMXPlayer instances in pyomxplayer and pausing the second movie:

self.loop1= OMXPlayer(‘/home/pi/loop1.mp4′, ‘-o local’, start_playback=True, do_dict=False)
self.loop2 = OMXPlayer(‘/home/pi/loop2.mp4′, ‘-o local’, start_playback=True,do_dict=False)
self.loop2.toggle_pause()

In a loop I check when the first video reaches its end position and pause it just before to start the next movie. Since it already loaded, there is no buffering for it to play. Note that the end position needs to be set a little bit before the video exactly ends in order to not get out of the video screen mode.

position = self.loop1.position/1000000
if position > 12.8:
self.loop1.toggle_pause()

A few seconds later I kill the process of the first movie to start it and pause it for next time.

sleep(2)
os.system(‘pkill -9 -f “/usr/bin/omxplayer.bin -s /home/pi/loop1.mp4 -o local”’)
os.system(‘pkill -9 -f “/bin/bash /usr/bin/omxplayer -s /home/pi/loop1.mp4 -o local”’)
sleep(2)
self.loop1= OMXPlayer(‘/home/pi/loop1.mp4′, ‘-o local’, start_playback=True, do_dict=False)
self.loop1.toggle_pause()

I added a 2 seconds delay in order for the new movie to have time to show and to allow for some time to kill the process.

- See more at: http://www.sundh.com/blog/2013/10/loop-videos-seamlessly-omxplayer/#sthash.bx1AhAms.dpuf