ThothLoki
Published © GPL3+

Play Video With Python and GPIO

This project will show you how I made a Raspberry Pi play videos based on a GPIO input.

EasyFull instructions provided2 hours27,640
Play Video With Python and GPIO

Things used in this project

Hardware components

Raspberry Pi 1 Model B+
Raspberry Pi 1 Model B+
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×5
Wifi Dongle
×1
Keyboard and Mouse
×1

Story

Read more

Schematics

Fritzing Layout

Fritzinggpio

Code

Code snippet #1

Plain text
import Rpi.GPIO as GPIO
import sys
import os
from subprocess import Popen

Code snippet #2

Plain text
GPIO.setmode(GPIO.BCM)

Code snippet #3

Plain text
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Code snippet #4

Plain text
movie1 = ("/home/pi/Videos/movie1.mp4")
movie2 = ("/home/pi/Videos/movie2.mp4")

Code snippet #5

Plain text
last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

quit_video = True

Code snippet #6

Plain text
while True:
	#Read states of inputs
	input_state1 = GPIO.input(17)
	input_state2 = GPIO.input(18)
	quite_video = GPIO.input(24)

	#If GPIO(17) is shorted to ground
	if input_state1 != last_state1:
		if (player and not input_state1):
			os.system('killall omxplayer.bin')
			omxc = Popen(['omxplayer', '-b', movie1])
			player = True
		elif not input_state1:
			omxc = Popen(['omxplayer', '-b', movie1])
			player = True
	
	#If GPIO(18) is shorted to ground
	elif input_state2 != last_state2:
		if (player and not input_state2):
			os.system('killall omxplayer.bin')
			omxc = Popen(['omxplayer', '-b', movie2])
			player = True
		elif not input_state2:
			omxc = Popen(['omxplayer', '-b', movie2])
			player = True

	#If omxplayer is running and GPIO(17) and GPIO(18) are NOT shorted to ground
	elif (player and input_state1 and input_state2):
		os.system('killall omxplayer.bin')
		player = False

	#GPIO(24) to close omxplayer manually - used during debug
	if quit_video == False:
		os.system('killall omxplayer.bin')
		player = False

	#Set last_input states
	last_state1 = input_state1
	last_state2 = input_state2

Code snippet #7

Plain text
pi@raspberrypi:~$ python3 videoplayer.py

Credits

ThothLoki

ThothLoki

2 projects • 40 followers
Just a guy who plays around trying to find new ways to do things
Contact

Comments