Raspberry Pi 2 W Video player

By | January 13, 2024

Overview

I want to create a headless video player using a Raspberry Pi Zero 2W and an inexpensive HDMI monitor. The Zero does not have audio output so a i2s amp will be added to the mix.

Create a Raspbian image

Raspberry Pi run on a operating system based off of linux. The easiest way to create this image is using the Raspberry Imager. This tool will create an image on a microSD card which you will insert into your raspberry pi zero 2W.

There are plenty of tutorials on this so they won’t be repeated here. It is important to setup your wifi connection using your SSID and password for your network, reset the user to pi and use a password only known to you and enable ssh.

Power it up

Plug in your hdmi monitor in the mini hdmi port, insert the microSD card into the 2W and power it up. The led will light up and blink at various speeds indicating activity.

Let see if we can ping it on your network. Open your terminal and enter the following.

ping -c 3 raspberrypi.local

PING raspberrypi.local (192.168.1.25): 56 data bytes
64 bytes from 192.168.1.25: icmp_seq=0 ttl=64 time=174.343 ms
64 bytes from 192.168.1.25: icmp_seq=1 ttl=64 time=9.222 ms
64 bytes from 192.168.1.25: icmp_seq=2 ttl=64 time=31.818 ms

--- raspberrypi.local ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 9.222/71.794/174.343/73.097 ms

Next we will ssh into the pi using the following. Note: I think its good practice to generate a new ssh key before your ssh into your pi.

ssh-keygen -R raspberrypi.local
ssh pi@raspberrypi.local

Linux raspberrypi 6.1.21-v7+ #1642 SMP Mon Apr  3 17:20:52 BST 2023 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Dec  4 22:08:41 2023
pi@raspberrypi:~ $ 

Now that we are in lets start installing what we need. Start with some updates.

sudo apt-get update
sudo apt-get upgrade

Next setup the configuration for the pi. Enter the config dialog using the following command.

sudo raspi-config

Once the configure screen is presented set the following

  • Screen blanking off – we are making a headless vidoe player we don’t want it to turn off
  • I2C and SPI – these are turned off but we can always get them in the user interface
  • Turn on VNC we will be using this to remote into the GUI, to remote in download VNC Viewer

Run videos from Python

VLC is the currently supported video player for Raspberry Pi. Omxplayer is obsolete. In this project we want to run it from python. Download the python compatible version of VLC using the following command.

pip install python-vlc

if your get errors try using the package manager instead

sudo apt-get update
sudo apt-get install python3-vlc

The error message you’re seeing indicates that the system is set up to manage Python packages using the system package manager (apt) rather than pip. This is common in environments where stability and consistency are important.

Installing python-vlc Using apt

First, try installing python-vlc using the system package manager:

sudo apt-get update
sudo apt-get install python-vlc

This will install the python-vlc package in a way that is compatible with your system’s package management.

New we can test this out by making a 720p mp4 file and store it in the videos folder. Then run the code below and have fun.

import vlc
import time

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

# creating vlc media player object
media_player = vlc.MediaPlayer()
media_player.toggle_fullscreen()

for _ in range(3):
    # set media and play
    media = vlc.Media(movie1)
    media_player.set_media(media)
    media_player.play()
    time.sleep(200)
    media_player.pause()
media_player.stop()

Adding local audio i2s amp

If you have a monitor with an audio out you can use that to output sound. I haven’t tested this. Instead I want to use an i2s amp.

You can get a i2s amp from Adafruit and hook it up to your pi using the following pins.

  • Amp Vin to Raspbery Pi 5V Power
  • Amp GND to Raspbery Pi Ground
  • Amp DIN to Raspbery Pi GPIO 21
  • Amp BCLK to Raspbery Pi GPIO 18
  • Amp LRCLK to Raspbery Pi GPIO 19

Running this script will add most of the changes needed.

curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/main/i2samp.sh | bash

Unfortunately I did not get any audio. This was solved it by adding the following to my config file. Edit it with the following command.

sudo nano /boot/config.txt

Then add the following to the file and save it by pressing cntrl x.

dtoverlay=audremap,enable_jack=on

Autostart

So my project will be headless and I want the python program to start on startup. Do this by adding the program to your autostart file. Before we do that lets install xterm which will allow us to see error message for our code as well as stop the process. Install xterm using the following command.

sudo apt-get install xterm -y

Next we will make a directory used for autostart files using the following command.

mkdir /home/pi/.config/autostart
nano /home/pi/.config/autostart/videoplayer.desktop

Then Edit the file as using the following.

nano /home/pi/.config/autostart/videoplayer.desktop

Put the following code in the file and save pressing cntrl X.

[Desktop Entry]
Type=Application
Name=Clock
Exec=xterm -hold -e '/usr/bin/python3 /home/pi/videoplayer.py'

Now reboot and your video play will load how cool is that?

Stopping python script

The auto boot program can be stopped by opening a terminal and finding the pid for the running program using the following command.

sudo ps -ax | grep python

Then use the following command to stop that process.

sudo kill <PID>

Adding buttons

So this is supposed to be a player and I want to start videos using buttons. The raspberry pi has lot of io pins and we want to access them. I want to install the circuit python blinka library to access some of the cool libraries they have so lets do that with the following commands.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pip

Next upgrade the tools using the following command.

sudo apt install --upgrade python3-setuptools

Next we will install the blinka library for circuit python.

cd ~
pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo -E env PATH=$PATH python3 raspi-blinka.py

An finally we can test the library using the following code.

import board
import digitalio
import busio

print("Hello blinka!")

# Try to great a Digital input
pin = digitalio.DigitalInOut(board.D4)
print("Digital IO ok!")

# Try to create an I2C device
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C ok!")

# Try to create an SPI device
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
print("SPI ok!")

print("done!")

If everything is Ok you will get an output like the follows.

Hello blinka!
Digital IO ok!
I2C ok!
SPI ok!

Hooking up to the raspberry pi pins with hardware is out of scope for this article. A good start is to see what pins you have on your pi using the following command in the terminal.

pinout
,--------------------------------.
| oooooooooooooooooooo J8     +====
| 1ooooooooooooooooooo        | USB
|                             +====
|      Pi Model ??? V1.0         |
|      +----+                 +====
| |D|  |SoC |                 | USB
| |S|  |    |                 +====
| |I|  +----+                    |
|                   |C|     +======
|                   |S|     |   Net
| pwr        |HDMI| |I||A|  +======
`-| |--------|    |----|V|-------'

Revision           : 902120
SoC                : BCM2837
RAM                : 512MB
Storage            : MicroSD
USB ports          : 4 (of which 0 USB3)
Ethernet ports     : 1 (0Mbps max. speed)
Wi-fi              : False
Bluetooth          : False
Camera ports (CSI) : 1
Display ports (DSI): 1

J8:
   3V3  (1) (2)  5V    
 GPIO2  (3) (4)  5V    
 GPIO3  (5) (6)  GND   
 GPIO4  (7) (8)  GPIO14
   GND  (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND   
GPIO22 (15) (16) GPIO23
   3V3 (17) (18) GPIO24
GPIO10 (19) (20) GND   
 GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8 
   GND (25) (26) GPIO7 
 GPIO0 (27) (28) GPIO1 
 GPIO5 (29) (30) GND   
 GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND   
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
   GND (39) (40) GPIO21

For further information, please refer to https://pinout.xyz/
pi@raspberrypi:~ $ 

Adding lights

My other project with use the raspberry pi pico drive neopixels. Basically these io pins require special communications to the neopixel. On the raspberry pi zero 2W only the GP10, 12, 18 and 21 can drive neopixels. I am currently using pins as follows.

  • 10 not used yet designated as SPI_MOSI on the data sheet.
  • 12 not used
  • 18 i2s amp BCLK
  • 21 i2s amp DIN

So I started with pin 12 that I am not using for anything yet. We I run the code is says I need to run in sudo mode which I don’t want to do. Apparently neopixels need to operate at a very low level in the hardware which is a risk. Then I found this advice online on using pin 10. I don’t plan to use spi so I think this is a good choice.

“I was able to control a neopixel ring without sudo. Here are the steps I took. I didn’t exhaustively try with and without all of them so I’m not sure if they’re all necessary:”

  1. physically move LED connection to pin 10 BCM10/MOSI/physical pin 19
  2. change pin in code accordingly
  3. add pi user to spi and gpio groups: sudo usermod -aG gpio,spi pi
  4. create /etc/udev/rules.d/99-com.rules containing the single line SUBSYSTEM=="spidev", GROUP="spi". this line is already there
  5. enable SPI interface through raspi-config
  6. run the program as pi user without sudo