1

my project is done and working as i want but there there is a bit of delay between transition of two videos arduino sends char "a,b,c,d" to raspberry pi serially. depending on the char received the raspberry pi plays the particular video using omxplayer. when there is transition from one video to another there is a slight delay and screen becomes blank for that while. so can this delay be removed and i can delay less transition of video. code:

import sys
import os
from subprocess import Popen
import serial

movie1 = ("/home/pi/sss5/vid1.mp4")
movie2 = ("/home/pi/sss5/vid2.mp4")
movie3 = ("/home/pi/sss5/vid3.mp4")

ser = serial.Serial('/dev/ttyUSB0',9600)

while True:
    data = ser.read()

    if data == "a":
        os.system('killall omxplayer.bin')
        print "a"
        omxc = Popen(['omxplayer','-b',movie1])

    elif data == "b":
        os.system('killall ommxplayer.bin')
        print "b"
        omxc = Popen(['omxplayer','-b',movie2])

    elif data == "c":
        os.system('killall omxplayer.bin')
        print "c"
        omxc = Popen(['omxplayer','-b',movie3])
  • 1
    Looking at the script, I don't see anything that explains this blackscreen and delay you describe that you can optimize. I imagine that the delay is actually the kill-Popen cycle, and the buffering needed by omxplayer before it can begin playing. A possible solutions that springs to mind is that VLC can run with a command line interface and you can control playback with that. Thus, if you switch from omxplayer to VLC, you may launch VLC once and then control the playback via a (probably more complicated) script. I'm not saying that VLC will fit the bill in other aspects, though. – jogco Jan 25 '17 at 8:18
  • thank you jogco, but without shifting to vlc and using only omxplayer the delay cant be removed?? – shramik Jan 25 '17 at 9:36
  • No you won't need to switch to VLC, see my answer below. – jogco Jan 25 '17 at 12:59
  • Great solution! You might want to know why this works, and it's because of h264 video compression. If you had uncompressed, raw video there would theoretically be no delay, but the bitrate would be huge and it would probably use too much memory bandwidth and CPU power to be viable. H264 stores a keyframe and then renders changes to the keyframe for a while before generating another keyframe. The consequence of this is that it has to load a number of frames before it can render any of them. Consequently when you tell the script to open and play a video file, it takes a few seconds to render any – Ed Kelly Jan 27 '17 at 10:34
1

Okay, got inspired and checked omxplayer out. I've worked some with VLC, but not omxplayer. It turns out that omxplayer has a DBUS interface with which it can be controlled.

But I also discovered that there is a --layer n option that can be useful. If I don't kill the omxplayers already running, starting the new player in a higher layer (higher n) puts that video on top, pretty much instantaneous. Then I kill of the previously started videos in the background. A big improvement. My quick and dirty code:

import sys
import os
from subprocess import Popen
import psutil

movie1 = "/home/pi/video1.mp4"
movie2 = "/home/pi/video2.mp4"

n = 0
FNULL = open(os.devnull,'w')

def getplayers():
    procs = []
    for p in psutil.process_iter():
        if p.name() == 'omxplayer.bin':
            procs.append(p)
    return procs

def killoldplayers(procs):
    for p in procs:
        p.kill()

while True:
    players = getplayers()

    data = raw_input("Some input please: ")
    n += 1

    if data == "a":
        cmd = "omxplayer --no-keys --layer %d %s "%(n,movie1)
        Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
        killoldplayers(players)

    elif data == "b":
        cmd = "omxplayer --no-keys --layer %d %s "%(n,movie2)
        Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
        killoldplayers(players)

    elif data == "q":
        killoldplayers(getplayers())
        FNULL.close()
        sys.exit(1)

Hence, the delay can be minimised.

  • but sir after running your script i am not getting any output. it just keep on asking "some input please" in the terminal. – shramik Jan 30 '17 at 7:42
  • Check the movie1 and movie2variables, I see that had a different path in my script – jogco Jan 31 '17 at 12:39
  • i had changed it to my destination path but still no result – shramik Feb 1 '17 at 5:37
  • Hmmm, what versions are you using? (Raspberry Pi, Rasbian, python, omxplayer, psutil)? I'm using a Pi 3, Rasbian 2016-02-26 (jessie), python 2.7.9, omxplayer f544084 and psutil 2.1.1-1 – jogco Feb 1 '17 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.