Play Video With Python and GPIO




About: Guy in his 30's who has always had an interest in building things since a kid. I build hardware durning the summer and software durning the winter. I know a lot about a little and a little about a lot. The w...

Hello again. Today I am going to show an ongoing project I am working on. My company has bestowed upon me the pleasure to build a demo kit for one of our product lines. I work in the low voltage industry that includes fire alarm, security, video surveillance and structured cable (voice and data). While designing the demo kit, it was brought to my attention that there are many situations that we are unable to simulate at trade shows or at a customer site, especially when we are talking about releasing chemical agent or setting off sirens.

Anyways, a coworker came up to me and said that it would be cool if we could have video play when an alarm occurs on a panel. Immediately, I thought of a Raspberry Pi. Virtually all security and fire alarm panels are nothing more than microcontrollers on steroids, thus have no video capabilities. They do have IO though....lots and lots of IOs.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: BOM

Here is the bill of material you will need for this project:


  • Raspberry Pi (I used a Model B)
  • SD card or micro SD card with adapter (I used a class 10 16gb micro SD card with adapter)
  • Micro USB power cable (700ma minimum)
  • Keyboard and mouse
  • GPIO ribbon cable and cobbler
    • you can use female to male jumpers if you have them
  • Breadboard
  • Jumper wires
  • HDMI cable
  • TV
  • Computer
  • SD card reader
  • Internet connection (i used a usb wifi adapter but a hardwired connection is always better)


  • Raspbian Jessie (my version is from 11/21/2015)
  • omxplayer (built in to Raspbian)
  • Python 3 (built in to Raspbian)

Step 2: Setting Up the Pi


First things first, we need to set up the SD card with the OS for the Raspberry Pi.

On your computer, go to to download Raspbian.

Once you have the file downloaded, unzip the file.

Follow the instructions Here based on your type of computer to install Raspbian onto your SD card.

I have followed the instructions for Windows and Mac and they have all worked great. I will try the Linux version when my Linux boxes are freed up from their duties.


Now, insert your SD card into your Pi and power it up. Make sure you have your keyboard, mouse, TV and internet connection all plugged in as well.


Go to the Menu dropdown

Menu --> Preferences --> Raspberry Pi Configuration

  • Under the System tab, click the Expand Filesystem button
  • Under the Performance tab, you can overclock your Pi if you want to. I have mine selected to Modest(800MHz). Be CAREFUL when selecting the overclock speed. If you go too much without heatsinks or some form of cooling, you can damage your Raspberry Pi. You have been warned.
  • Under the Localisation tab, set your Locale, Timezone and Keyboard layout to match where you live.

Click OK and reboot your Pi.

After your Pi has rebooted, open the Terminal.:

Enter "sudo apt-get update"

When that is complete, enter "sudo apt-get upgrade"

If there are any upgrades needed, you will be prompted. Press "y" to install them. Reboot your Pi after that.

Now your Pi is set up and configured for what we need. On to the next task.

Step 3: Videos and Python

Before we can start to code, we need our material. To do this, you need some video files. As I am not a video editor in any sense of the word, I am leaving the creation of the required videos for the end-goal project to someone else in my company. I used .mp4 files as they are virtually universally played.

To test my code functionality, I transferred some music videos onto my Pi with a USB flash drive and saved them to the Videos folder.

There are other ways to transfer files to your Pi. One method is FTP. I did not use that method, but there are many good tutorials on it on this site and google.

Once you have your video files on your Pi, it is time to get down to coding.

Go to Menu --> Programming --> Python 3 (IDLE)

In Python 3, Go to File -- New File

Save that file as ""

Now for the code:

import the needed libraries

import RPi.GPIO as GPIO
import os import sys from subprocess import Popen

Set GPIO pin format


Setup the GPIO buttons

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)

Setup movie destination variable

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

Make boolean variables

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

quit_video = True
player = False

Now to put it all to work

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

Now you should be able to run and start triggering your videos via your GPIOs.

Step 4: And....Action

It is time to put our code to use. If you want to run it through IDLE, press F5.

You can also run through the terminals by entering:

pi@raspberrypi:~$ python3

Now it it time for the test.

Start shorting out your GPIOs and watch the videos play!

How it works

When you short out GPIO 17, the appropriate video will play. If you short out the GPIO 18 while GPIO 17's movie is playing, the video will stop and start playing GPIO 18's video. If all GPIOs are not shorted, omxplayer will close.

Step 5: What's Next

Now that this part if done, the next step is to have this script run at startup.

After that, who knows? I just got this crazy idea to utilize a a windows box that will be connected to the TV/Monitor to host the videos and send the signals from the Pi via wifi. Who knows? Maybe another 'ible?

Thank you for viewing my instructable. If you like it, please vote for me.

Digital Life 101 Challenge

Participated in the
Digital Life 101 Challenge

Full Spectrum Laser Contest 2016

Participated in the
Full Spectrum Laser Contest 2016

Raspberry Pi Contest 2016

Participated in the
Raspberry Pi Contest 2016

Be the First to Share


    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    41 Discussions


    1 year ago


    This tuto is great. I just discover the wonderful world of Pi and python

    I'd like to make a "video jukebox" : You press a button, you start a video.

    I'm using your tuto, I added other buttons and it works great.

    To make it start automaticly, I directly plug the GPIO17 to ground. So it directly goes to the first video (wich is a message written white in black, like : "CHOOSE YOU VIDEO, press a button")


    At the end of a video, it goes back to the desktop

    I'd like the program to back to the first video (or to go back to the beginning of the program), like looping the program

    How can I do it ?

    AND about the gap when video selecting, do you have new ideas ? (maybe using the lite version of raspbian, that look less like a desktop computer)

    Thank you for your tuto and I hope I ear from you soon



    Reply 2 years ago

    The first song was “battle Born” by Five Finger Death punch and the second song was “The Kids Aren’t Allright” by The Offspring


    2 years ago


    This is a great Instructable, it really helped me! I have a few notes for you to correct (if I understand properly). I'm sure the downloadable python file is correct, but above, I typed it in by hand to learn more, and I note that two things are off, if you don't mind me saying so.

    1) The import of RPi.GPIO is incorrectly listed at Rpi.GPIO. Python is case-sensitive.

    2) The line

    player = False

    is missing from your boolean variables section above

    I do have a question while I'm here - Because my project will show to "guests", when they push the button to start the video (I have it set to momentary only), I don't want them to see the desktop anytime. So how can I make the screen BLACK before the video plays and after the video plays? Any idea?



    P.S. I am new to Instructables, I don't see how to "vote" for you. What can I do that will help you???

    2 replies

    Reply 2 years ago

    Thank you DrychronRed for the corrections. I have edited the ible to reflect the .py file.

    As for the black between videos, I am still at a loss on how to do that with OMXplayer. You could remove all of the icons from the desktop and set the background as black, but that is just a bandaid.

    Honestly, to do something like seamless video or black screen afterwards, I think you will need to use something other than OMXplayer. You may have better luck with VLC. I have been out of the game on my Pis for a while, so I don't know of all of the updates. Plus this project got scrapped about two months after the ible posted, so I did not pursue it much after that.


    Reply 2 years ago

    Maybe just play a video that has nothing but blackness? Video a piece of black card in the dark with dark thoughts on a dark night wearing nothing but black and it'll be quite black when shown as a video.

    You then need to set OMX to auto loop this video unless it spots the right input go low.....


    2 years ago

    Oh man, gonna have some fun trying to mangle this to work with my pi zero Capacitive HAT idea. :)

    1 reply

    Reply 2 years ago

    awesome! Can’t wait to see it


    2 years ago

    hi, how to go the audio in the video?

    omxc = Popen(['omxplayer', '-b', movie2])

    i tried in this line, but nothing :/

    7 replies

    Reply 2 years ago

    Sorry about the short replies earlier, I was on my phone. I am nowhere near a Pi right now, but you may want to try this:

    omxc = Popen(['omxplayer', '-b -o local', movie2])

    I am not sure it will work. I had the same issue when building this project, but was always using the hdmi audio so the default worked for me. Give it a try and let me know.


    Reply 2 years ago

    Thanks ThothLoki, I already have audio but hdmi which is good enough.

    Is it possible to have a looped video, and that it is interrupted to the short GPIO17, and when it finishes that it follows the previous video in loop?



    Reply 2 years ago

    I am sure you can with --loop
    Just set up 17 as a omxplayer kill button.

    Google python omxplayer loop and you should get your answer.

    Sorry I can't be much more help ron guy now. My Pi was turned into an Echo for another project


    Reply 2 years ago

    When i run

    Omxplayer -or local video1.mp4 sounds all good.

    But with the GPIO and I can not get sound in the same video


    3 years ago

    Hello i have a few Questions Sense I'm still new to raspberry pi. Oh by the way awesome Tut you have here. Is it possible To run a video file on start up automatically? Second Question is the file where do i put that at? The other question is I would like to use a remote control they is used for a TV is it possible to set one up so i can say press Channel 1 and it plays the first file. and so forth and also if i hit the stop button it goes back to playing that first file that booted up?

    1 reply

    Reply 2 years ago

    Thanks josephchrzempiec! Sorry about the late reply. I hope you are enjoying the raspberry pi world as much as I am. Now, on to your questions.

    Yes, you can get this to run at startup. I haven't set it up on mine that way, but google how to set up cron jobs.

    You can put the python file anywhere you can find it. You will just need to know the path to run it from the terminal or cron

    I do not see why you couldn't set up a remote to function that way. You just need to set up the hardware and assign to the correct GPIOs (some script editing will be required) All of that sounds like IF/THEN statements to me.

    Have a look at this site as well. It looks similar to mine but a lot cleaner.


    2 years ago


    i have connected 3 laser trigger to arduino and connected arduiono to pi using usb. whenever the laser is blocked the arduino sends char to pi serially. fro 1st laser the pi receives char a, for 2nd b and 3rd laser c. so can i play a video for each laser trigger ?