Maker.io main logo

Another Ambassador Moment: Portable Video Player with Raspberry Pi

2021-06-14 | By Robby Huang

License: None Raspberry Pi

What’s a Raspberry Pi?

“The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing and to learn how to program in languages like Scratch and Python. It’s capable of doing everything you’d expect a desktop computer to do, from browsing the internet and playing high-definition video, to making spreadsheets, word-processing, and playing games.” 

                                    ------ www.raspberrypi.org

In other words, it is a computer with the size and price of a microcontroller. 

 

Raspberry Pi vs Arduino

Many people like to compare Raspberry Pi and Arduino. They are the most popular controller board for projects in recent years, but they are actually quite different. Raspberry is actually more similar to a computer than an Arduino. Arduino has no interpreter, no firmware, and no operating system. The code we write and upload from the Arduino IDE. The code is based on C code with a nit default library and it will be converted into machine code to run on the Atmega 328 chip. Meanwhile, an RPi has a microprocessor and has all kinds of external ports that can support audio, video, HDMI, Ethernet, etc., it all depends on the model. The scripts we write in RPi are programs that will be compiled in the Linux OS. Below is a simple comparison of the two to give you some basic ideas. They have a lot of pros and cons and we pick them depending on the project. For example, Raspberry Pi is a more preferable foundation for the portable video player we are building.

 Raspberry Pi Portable Video Player

Bill of materials

Hardware Setup

First, follow the Raspberry Pi official guide to set it up. The next step is to plug the TFT into the RPi 40 pin GPIO connector. We use the expansion cable in between them. The white stripe on the cable should be on the left side (the side close to the buttons) of the PiTFT. We need to be really careful because of the delicacy of the PiTFT. Before we plug the module into the RPi, we should double-check that the white stripe on the cable is located next to the white ‘1’ on the PiTFT.

Software Setup

This section can vary depending on the version of the Raspberry Pi OS we are using. I am using the Buster release and the instruction below should work for this release. 

More information can be found here: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi.pdf

Install software to support the piTFT by running:

sudo apt-get install -y bc fbi git python-pip python-smbus python-spidev evtest libts-bin

The python-pip application is used to install Python packages on the Pi. The evtest and libts-bin applications are used for testing the piTFT.

Run the command:

sudo pip install evdev

Please see the adafruit forum for more information.

Implementation

Step 1

MPlayer is an open-source media player software application in the OS of RPi. By typing “mplayer” into the command window, we receive a list of video controls. These are the possible controls we can interface with mPlayer. I copied it down for later reference. The left column corresponds to the keys on a keyboard.

Step 2

First, we created a FIFO file and named it “video_fifo”. We can make sure that it was created using the command “ls -l” and see that the orange-colored video_fifo is there. Then we use the command “sudo SDL_VIDEORIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -input file=/home/pi/test_fifo -vo sdl -framedrop /home/pi/bigbuckbunny320p.mp4” to play a video. We include the input flag in this command to make sure that the video can listen to input commands while playing in the foreground. We can send commands to the video_fifo such as ‘echo “pause”> /home/pi/video_fifo’. A complete list of commands can be found when calling “mplayer –input cmdlist” or at this “http://www.mplayerhq.hu/DOCS/tech/slave.txt” online text file. To test this command we need to either run startx and open up two console windows on the desktop or SSH in two terminals on my PC. In one of the window, run the command to play the video.

 1

Step 3

We use “nano fifo_test.py” in the /home/pi directory to create the fifo_test.py file, which is a Python routine. We use it to take an input command from the user, recognize it, make it control the video, and quit to return to the command line. Inside the script, I first imported the subprocess library, then used the prompt_text to get user input and use the raw_input function to store the input in a string variable. Raw_input is a Python 2 function equivalent to input in Python 3. Then I used an if statement to compare the input string with some command. If they were equal, I used print to output the subprocess command line for the respective input command. The screenshot above shows the video frame was paused when the user input pause. I did this on the desktop so that it is easier to demo for the lab report, it works the same with PiTFT.

2

 Step 4

There are four buttons on the PiTFT board. Each of them corresponds to a GPIO pin on RPi. We created a Python routine and named it one_button.py. First and foremost, we imported theRPi.GPIO module and the time module. Then, we used the GPIO.setmode(GPIO.BCM) to set the mode for Broadcom number instead of board numbers. After initializing a GPIO, weset it to internal pullup. We used a looping algorithm: a delay that enables the messages to be printed and an if statement that checks if it is pulldown and print respectively. Since the output will be similar to step 5, I will not include a screenshot until then.

Raspberry Pi Portable Video Player

Step 5

Step 5 is an extension of step 4, we just need to initialize more GPIO pins and add more if statements in the while loop. For one of the buttons, we need to quit the Python program after printing. There are many ways to do this. At first, I simply put an exit() function there, but I later realized that using break and clear all GPIO outside the loop is a better approach. I added that feature to my button connected with GPIO 17. The screenshot on the left is the output as I pressed them one by one.

 3

Step 6

Step 6 is a combination of the previous steps. I added the subprocess module into the GPIO script in step 5 so that each button corresponds to a certain command. I used “seek 10” and “seek -10” to fast forward 10 seconds or move backward for 10 seconds. I used the stop command to stop the mPlayer, and also the “pause” command was used. I named this script ‘video_control.py’.

 4Raspberry Pi Portable Video Player

These two screenshots show you the output on the console running the Python script (left) and the console playing video on Mplayer (right).

Reference

 Raspberry Pi Portable Video Player

Video and Blog Post inspired from Embedded OS Course at Cornell.

制造商零件编号 110061125
RASPBERRY PI 4B - STARTER KIT -
Seeed Technology Co., Ltd
¥859.59
Details
制造商零件编号 2423
DISPLAY PITFT PLUS 320X240 2.8
Adafruit Industries LLC
¥365.90
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum