Maximum PC

Build a Raspberry Pipowered random video player

ANY MODEL OF RASPBERRY PI Plus the latest version of Raspbian; three push buttons; breadboard; four female-tomale jumper wires; two male- to-male jumper wires.

- LES POUNDER

STUCK ON WHAT TO WATCH this evening with the family? Fear not—with this project, we can have our Raspberry Pi randomly choose a video for us to watch. So, let’s start building and coding our own random video player. All of the code, and a detailed high-res circuit diagram, can be downloaded from https://github.com/lesp/LXF244-VideoPlaye­r/archive/master.zip.

Building the circuit for this project is simple. We only require three buttons, connected to the GPIO at pins two, three, and four, and each of these buttons also needs to be connected to GND. For this we can use a single GND pin on the Pi, and a female-to-male jumper wire. Connecting to the negative rail of a breadboard means that we have multiple GND connection­s that can be connected via the two male-to-male jumper wires. See the diagram in the download for this project for details. When the hardware is built, attach the accessorie­s required for your Pi, then boot to the Raspbian desktop.–

1 TUNE IN

Before we can write any Python code, we need to install two libraries that will help us create the project. Open a terminal, and type the following: $ sudo pip3 install glob $ sudo pip3 install keyboard

>> We’ll talk more about them later. For now, open the Python 3 editor, found in the “Programmin­g” menu, and click “File > New” to create a new blank file. Immediatel­y click “File > Save” in the new window. Save the code as “VideoPlaye­r.py” and remember to save often.

>> Now we start writing the code [ Image A], and our first act is to import the libraries that we require. The first three libraries are used to detect the button presses (GPIO Zero), stop the code from running once and exiting (pause), and to choose a random video (choice.) from gpiozero import Button from signal import pause from random import choice

>> The final three imports are a library that we shall use to list the contents of a directory (glob), one to run terminal commands (subprocess), and the last is a library to emulate a keyboard (keyboard). import glob import subprocess import keyboard

>> Moving on, we create three functions—blocks of code that we can later reuse by calling their name. The first function is called “play_video,” and it first creates a list (a data storage object) called “videos.” def play_video():

videos = []

>> To fill the list with all the videos we can play, we use a “for” loop. This will iterate over every file in a directory, as long as it’s an MP4 video file. For this we use glob to access the directory “/media/pi/ Videos,” which is really a USB stick called “Videos” full of MP4 files. Change this to match the name of your chosen directory full of videos. Each time an MP4 is found, it’s appended to the “videos” list that we’ve just created. or file in glob.glob(“/media/pi/ Videos/*.mp4”):

videos.append(file)

>> Check that the list has been populated with file names by printing the list’s contents to the Python shell:

print(videos) Then choose a random video from the list and store it in a variable called “chosen.” Print this to the Python shell: chosen = choice(videos)

print(chosen)

>> The last line in this function uses the Popen class from the subprocess library to run a command as if we were sitting at the terminal, and this command is to open the omxplayer media player, then play the chosen video.

subprocess.Popen([‘omxplayer’,(chosen)])

>> The next function is called “stop_video,” and as you can guess, it stops the currently playing video. For this we use the keyboard library, specifical­ly the “press_ and_release” function, to simulate pressing Q. def stop_video():

keyboard.press_and_release(‘q’)

>> The last function is called “pause_video,” and it emulates pressing the space bar on the keyboard, which is how omxplayer pauses video. def pause_video():

keyboard.press_and_release(‘space’)

>> With the functions created, we next need to tell our code where our buttons are connected. We have three buttons connected: randomiser (play), stop, and pause_button. These buttons are connected to the GPIO at pins two, three, and four, respective­ly [ Image B]. randomiser = Button(2) stop = Button(3) pause_button = Button(4)

2 PUSH THE BUTTON

On to the last part of the code, which looks for a video when a button is pressed, and then reacts accordingl­y. First, however, we need to wrap this section in an exception handler. It tries to run the code, but if the user presses Ctrl-C, the code exits. So, for the try section, our code first prints three lines to the shell—these are the instructio­ns to the user. The

\n between each color is Python shorthand to insert a new line between each instructio­n: try:

print(“Press the GREEN button to start\nYELLOW to pause\nRED to stop”)

>> Our three buttons are waiting to be pressed, and using the GPIO Zero library, we call the when_pressed class to detect when each of the buttons are pressed. When this happens, the correspond­ing function is run. So, when we press the green randomizer (play) button, it randomly chooses a video from the USB stick, and plays it for us. You’ll notice that the functions don’t have a () at the end of the function name. If they did, the code would run automatica­lly. Right now, they’re ready to run on demand. randomiser.when_pressed = play_video stop.when_pressed = stop_video pause_button.when_pressed = pause_video

>> The last line in this section is a simple pause() , and this is used to keep the code running, and not just exiting after running once.

pause()

>> But what happens if the user presses Ctrl-C? Well, this is a “Keyboard Interrupt,” and if that occurs, we shall simply print an empty line, then “Exit” on the screen. except KeyboardIn­terrupt:

print(“\ nEXIT”)

>> Now save your code, and open a terminal to the directory where you have saved the code. In order to use the code, we need to run it with sudo (root powers), because the keyboard library used in the project can only be used as root. To run the code, type:

$ sudo python3 VideoPlaye­r.py

When you’re all sitting on the couch, press the green button to play the randomly chosen video, to pause press yellow, and press red to stop. Enjoy your evening, and be careful not to spill the popcorn!

 ??  ??
 ??  ?? A
A
 ??  ?? B
B

Newspapers in English

Newspapers from United States