Build a Rasp­berry Pipow­ered ran­dom video player

ANY MODEL OF RASP­BERRY PI Plus the lat­est ver­sion of Rasp­bian; three push but­tons; bread­board; four fe­male-tomale jumper wires; two male- to-male jumper wires.

Maximum PC - - R&D - LES POUNDER

STUCK ON WHAT TO WATCH this evening with the fam­ily? Fear not—with this project, we can have our Rasp­berry Pi ran­domly choose a video for us to watch. So, let’s start build­ing and cod­ing our own ran­dom video player. All of the code, and a de­tailed high-res cir­cuit di­a­gram, can be down­loaded from https://github.com/lesp/LXF244-VideoPlayer/ar­chive/mas­ter.zip.

Build­ing the cir­cuit for this project is sim­ple. We only re­quire three but­tons, con­nected to the GPIO at pins two, three, and four, and each of these but­tons also needs to be con­nected to GND. For this we can use a sin­gle GND pin on the Pi, and a fe­male-to-male jumper wire. Con­nect­ing to the neg­a­tive rail of a bread­board means that we have mul­ti­ple GND con­nec­tions that can be con­nected via the two male-to-male jumper wires. See the di­a­gram in the down­load for this project for de­tails. When the hard­ware is built, at­tach the ac­ces­sories re­quired for your Pi, then boot to the Rasp­bian desk­top.–

1 TUNE IN

Be­fore we can write any Python code, we need to in­stall two li­braries that will help us cre­ate the project. Open a ter­mi­nal, and type the fol­low­ing: $ sudo pip3 in­stall glob $ sudo pip3 in­stall key­board

>> We’ll talk more about them later. For now, open the Python 3 ed­i­tor, found in the “Pro­gram­ming” menu, and click “File > New” to cre­ate a new blank file. Im­me­di­ately click “File > Save” in the new win­dow. Save the code as “VideoPlayer.py” and re­mem­ber to save of­ten.

>> Now we start writ­ing the code [ Im­age A], and our first act is to im­port the li­braries that we re­quire. The first three li­braries are used to de­tect the but­ton presses (GPIO Zero), stop the code from run­ning once and ex­it­ing (pause), and to choose a ran­dom video (choice.) from gpi­ozero im­port But­ton from sig­nal im­port pause from ran­dom im­port choice

>> The fi­nal three im­ports are a li­brary that we shall use to list the con­tents of a direc­tory (glob), one to run ter­mi­nal com­mands (sub­pro­cess), and the last is a li­brary to em­u­late a key­board (key­board). im­port glob im­port sub­pro­cess im­port key­board

>> Mov­ing on, we cre­ate three func­tions—blocks of code that we can later re­use by call­ing their name. The first func­tion is called “play_video,” and it first cre­ates a list (a data stor­age ob­ject) called “videos.” def play_video():

videos = []

>> To fill the list with all the videos we can play, we use a “for” loop. This will it­er­ate over ev­ery file in a direc­tory, as long as it’s an MP4 video file. For this we use glob to ac­cess the direc­tory “/me­dia/pi/ Videos,” which is re­ally a USB stick called “Videos” full of MP4 files. Change this to match the name of your cho­sen direc­tory full of videos. Each time an MP4 is found, it’s ap­pended to the “videos” list that we’ve just cre­ated. or file in glob.glob(“/me­dia/pi/ Videos/*.mp4”):

videos.ap­pend(file)

>> Check that the list has been pop­u­lated with file names by print­ing the list’s con­tents to the Python shell:

print(videos) Then choose a ran­dom video from the list and store it in a vari­able called “cho­sen.” Print this to the Python shell: cho­sen = choice(videos)

print(cho­sen)

>> The last line in this func­tion uses the Popen class from the sub­pro­cess li­brary to run a com­mand as if we were sit­ting at the ter­mi­nal, and this com­mand is to open the omx­player me­dia player, then play the cho­sen video.

sub­pro­cess.Popen([‘omx­player’,(cho­sen)])

>> The next func­tion is called “stop_video,” and as you can guess, it stops the cur­rently play­ing video. For this we use the key­board li­brary, specif­i­cally the “press_ and_re­lease” func­tion, to sim­u­late press­ing Q. def stop_video():

key­board.press_and_re­lease(‘q’)

>> The last func­tion is called “pause_video,” and it em­u­lates press­ing the space bar on the key­board, which is how omx­player pauses video. def pause_video():

key­board.press_and_re­lease(‘space’)

>> With the func­tions cre­ated, we next need to tell our code where our but­tons are con­nected. We have three but­tons con­nected: ran­domiser (play), stop, and pause_but­ton. These but­tons are con­nected to the GPIO at pins two, three, and four, re­spec­tively [ Im­age B]. ran­domiser = But­ton(2) stop = But­ton(3) pause_but­ton = But­ton(4)

2 PUSH THE BUT­TON

On to the last part of the code, which looks for a video when a but­ton is pressed, and then re­acts ac­cord­ingly. First, how­ever, we need to wrap this sec­tion in an ex­cep­tion han­dler. It tries to run the code, but if the user presses Ctrl-C, the code ex­its. So, for the try sec­tion, our code first prints three lines to the shell—these are the in­struc­tions to the user. The

\n be­tween each color is Python short­hand to in­sert a new line be­tween each in­struc­tion: try:

print(“Press the GREEN but­ton to start\nYELLOW to pause\nRED to stop”)

>> Our three but­tons are wait­ing to be pressed, and us­ing the GPIO Zero li­brary, we call the when_­pressed class to de­tect when each of the but­tons are pressed. When this hap­pens, the cor­re­spond­ing func­tion is run. So, when we press the green ran­dom­izer (play) but­ton, it ran­domly chooses a video from the USB stick, and plays it for us. You’ll no­tice that the func­tions don’t have a () at the end of the func­tion name. If they did, the code would run au­to­mat­i­cally. Right now, they’re ready to run on de­mand. ran­domiser.when_­pressed = play_video stop.when_­pressed = stop_video pause_but­ton.when_­pressed = pause_video

>> The last line in this sec­tion is a sim­ple pause() , and this is used to keep the code run­ning, and not just ex­it­ing af­ter run­ning once.

pause()

>> But what hap­pens if the user presses Ctrl-C? Well, this is a “Key­board In­ter­rupt,” and if that oc­curs, we shall sim­ply print an empty line, then “Exit” on the screen. ex­cept Key­board­In­ter­rupt:

print(“\ nEXIT”)

>> Now save your code, and open a ter­mi­nal to the direc­tory where you have saved the code. In or­der to use the code, we need to run it with sudo (root pow­ers), be­cause the key­board li­brary used in the project can only be used as root. To run the code, type:

$ sudo python3 VideoPlayer.py

When you’re all sit­ting on the couch, press the green but­ton to play the ran­domly cho­sen video, to pause press yel­low, and press red to stop. En­joy your evening, and be care­ful not to spill the pop­corn!

A

B

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.