User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Video control in Python

Sat Jan 02, 2016 3:31 pm

Hi;

I am experimenting with video playback using python for a small project I am working on. Basically I want to be able to play a certain video clip dependent upon a action.

To start with I have been experimenting with a sort of screen saver video, this is a video that plays when nothing happens, the video loops.
When something happens, such as motion is detected, the looping video needs to pause or stop and a different video will be played, once the second video has finished, if nothing else has happened the first video will continue (if paused) or start again.

I have managed to get this working using two separate videos but when the first video pauses although I can hear the second video it is hidden by the first?? I think the problem can be solved using different video layers but can't work out how to do this?

Here is my code, the first video plays and loops and the second video starts when motion is detected, currently when motion is detected again the video says its playing again but does not appear to do so?

Code: Select all


#!/usr/bin/env python

#Import libraries

import time
from omxplayer import OMXPlayer
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
PIR_PIN = 7

GPIO.setup(PIR_PIN,GPIO.IN)

print("Starting visual front end")
wait_loop1 = OMXPlayer('/home/pi/Videos/Vid_Wait_Loop1.mp4',args=['--loop'])
vid1 = OMXPlayer('/home/pi/Videos/Vid_Name_Hammerstein.mp4')
wait_loop1.play()

try:
    print("Motion detection activated")
    time.sleep(2)
    print("Security alarm functioning")
    while True:
        if GPIO.input(PIR_PIN):
            wait_loop1.pause()
            print("Intruder Detected")
            vid1.play()
            print("Playing Vid_Name_Hammerstein")
            time.sleep(10)
            vid1.quit()

except KeyboardInterrupt:
        print("terminated by user")
        wait_loop1.quit()
        vid1.quit()
        GPIO.cleanup()

I started this a while ago but left it to move on to other things.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: Video control in Python

Sat Jan 02, 2016 5:08 pm

I made a few changes to my code, tidying up the PIR entries (following another guide) and added some window args for the videos as when testing the video would open full screen and I could not close it to check and correct syntax errors without killing the process via ssh!!

Anyway here is the code:

Code: Select all

#Central_AI_v1.2
#Bill Harvey
#30/12/15

#!/usr/bin/env python

#Import libraries
import os
import time
from omxplayer import OMXPlayer
import RPi.GPIO as GPIO
import picamera

PIR_PIN = 7

GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR_PIN, GPIO.IN, GPIO.PUD_DOWN)

PIR_previous_state = False
PIR_current_state = False

picam = picamera.PiCamera()

print("CENTRAL_AI Startup - running initial setup")
print("Starting Central visual front end")
loop1 = OMXPlayer('/home/pi/Videos/Vid_Wait_Loop1.mp4',args=['--win', '100 100 640 480','--loop'])
vid1 = OMXPlayer('/home/pi/Videos/Vid_Name_Hammerstein.mp4',args=['--win', '100 100 480 360'])

try:
    print("Motion detection activated")
    time.sleep(2)
    print("Security system functioning")
    loop1.play()
    while True:
        time.sleep(0.1)
        PIR_previous_state = PIR_current_state
        PIR_current_state = GPIO.input(PIR_PIN)
        if PIR_current_state != PIR_previous_state:
            new_state = 'HIGH' if PIR_current_state else "LOW"
            print("GPIO pin %s is %s" % (PIR_PIN, new_state))
            if PIR_current_state:
                loop1.pause()
                print("Intruder Detected")
                vid1.play()
                print("Playing Vid_Name_Hammerstein")
except KeyboardInterrupt:
    print("terminated by user")
    loop1.quit()
    vid1.quit()
    GPIO.cleanup()

I still have a problem, when motion is detected and the PIR_PIN goes HIGH the first video 'loop1' (the one that is looping) pauses and the second video 'vid1' plays on top. Once the vid1 finishes loop1 un pauses (not sure how as its not coded) but if motion is detected again the first video (the loop one) pauses again but the second video does not play.

The two print statements are output so I know that this part of the 'if' statement is being executed.

But I am not sure why the video is not re-appearing?
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Return to “Python”