1

a bit of background info on what I am running: I have a Raspberry Pi which is listening to serial communication from a Wemos D1. When it receives a message it will play the relevant video file. When it is not playing movie1 or movie2 I want the tvbackground video to be playing on loop. Serial comms can be received at any point and the tvbackground needs to immediately stop playing to allow movie1 or movie2 to begin. When movie1 or movie2 has played through, I want tvbackground to automatically begin again on loop.

I have got things working almost exactly how I would like except for restarting the tvbackground clip. It either (1) Won't restart tvbackground once movie1 or movie2 has finished OR (2) If I add a break in, it begins playing movie1 and then immediately starts the tvbackground before movie1 has finished playing.

Can I prevent the python script from breaking the loop and restarting tvbackground until after movie1 has finished playing, or is there an easier solution?

Thank you!

#!/usr/bin/evn python3
import serial
import time
import sys
import os
from subprocess import Popen
import psutil

movie1 = "/home/pi/Videos/001_1080p.mp4"
movie2 = "/home/pi/Videos/002_1080p.mp4"
tvbackground = "/home/pi/Videos/tvbackground_1080p.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()

    #THIS WILL AUTOPLAY AND LOOP FOREVER, BUT WON'T REOPEN ONCE ANOTHER VIDEO PLAYS
    cmd = "omxplayer --loop --no-keys --layer %d %s "%(n,tvbackground)
    Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
    killoldplayers(players)

    if __name__ == '__main__':
        ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
        ser.flush()

        while True:
            players = getplayers()

            if ser.in_waiting > 0:
                line = ser.readline().decode('utf-8').rstrip()
                print(line)
                n += 1

                if(line=="One"):
                    cmd = "omxplayer --no-keys --layer %d %s "%(n,movie1)
                    Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
                    killoldplayers(players)
                    #IT NEEDS TO RESTART TVBACKGROUND AUTOMATICALLY HERE, BUT ONLY ONCE MOVIE1 HAS FINISHED

                elif(line=="Two"):
                    cmd = "omxplayer --no-keys --layer %d %s "%(n,movie2)
                    Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
                    killoldplayers(players)
                    #IT NEEDS TO RESTART TVBACKGROUND AUTOMATICALLY HERE, BUT ONLY ONCE MOVIE2 HAS FINISHED
-1

If you want python to pause for a few seconds try:

from time import sleep
sleep(10)

This makes python wait for 10 seconds before continuing the program. You can use this in any area of code. Hope it helps!

| improve this answer | |
  • 1
    The movies will be of varying length, I could put it to sleep if I go through and work out the length for each clip, but there will be a lot more than 2 movies so a bit time consuming. There may be a way to automatically get the clip duration, or check if a clip is currently playing in OMX. – Matt Apr 26 at 23:01
  • I wish I could help you more, but I never used OMX before. It's true that programming can take a lot of time. :) – UNKNOWN Apr 28 at 14:45

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.