How to make an auto starting and looping video player with video files on USB drive
1. Format SD card
From Mac or PC download software: “SD Formatter” and format the SD card.
2. Download image
From Mac or PC download: Raspbian lite
3. Flash SD card with "Raspbian lite" image
On Mac or PC use software: “Balena Etcher” to flash SD card with Raspbian lite image.
4. Boot up Raspberry Pi
Then open Raspberry Pi config. "sudo" gives you root (administrator) rights:
$ sudo raspi-config
Only if you have norwegian keyboard:
Localisation options -> Change Keyboard Layout -> Generic 105-key PC -> Other -> Norwegian -> Norwegian (top) -> The default for the keyboard layout -> No compose key
Boot Options -> Desktop / CLI -> Console Autologin
Interfacing Options -> SSH -> enable -> YES
Advanced Options -> Memory Split -> Replace with: 256 -> OK
Network Options -> Wi-fi -> Enter your wi-fi SSID (name of wi-fi network) -> Enter your wi-fi password (if you don't have wi-fi, use an internet connected ethernet cable)
5. Auto mount USB drive
You may now copy a videofile to the USB drive (see point 10 for info about codec) and then insert USB drive and type:
$ sudo blkid
Find name of drive e.g. “/dev/sda1". I think the first drive connected is always “/dev/sda1” on Raspberry Pi. Then open "fstab" with the text editor "Nano":
$ sudo nano /etc/fstab
Type this at the bottom. Make sure you type it exactly as here, or else the pi might not boot up and you will have to start over at point nr. 3. The USB drive will be read-only to minimize the risk of a dirty unmount.
/dev/sda1 /home/pi/usbdrv vfat uid=pi,gid=pi,umask=0022,sync,auto,nosuid,ro,nouser 0 0
"Ctrl-X" -> type big "Y" and press ENTER to confirm exit and save. Then reboot:
$ sudo reboot
After reboot type "ls" to list current directory:
You will now see usbdrv. Type this to change directory to usbdrv:
$ cd usbdrv
Then type "ls" again to list files in usbdrv directory that is now hopefully the contents of your USB drive:
6. Install omxplayer
Make sure your pi is connected to the internet. Just connect an internet connected ethernet cable if you don't have wi-fi and reboot ("sudo reboot"). First update apt-get (Advanced Packaging Tool - software management):
$ sudo apt-get update
Then install "Omxplayer":
$ sudo apt-get install omxplayer
This will install Omxplayer and all libraries Omxplayer is dependent on.
"Do you want to continue?"
Type big "Y" and press ENTER
7. Looping script
First go to the home directory on the pi:
$ cd /home/pi
Then create the looping script:
$ sudo nano loopingVideo.sh
Then type this exactly like here:
#!/bin/sh VIDEOPATH="/home/pi/usbdrv" SERVICE="omxplayer" while true; do if ps ax | grep -v grep | grep $SERVICE > /dev/null then sleep 1; else for entry in $VIDEOPATH/* do clear omxplayer -o both $entry > /dev/null done fi done
Omxplayer overruns hdmi/audio jack options in raspi-config (amixer). "-o both" in script directs audio to both.
"Ctrl-X" to save and exit
Type big "Y" and press ENTER to confirm
Type "ls" to see the file:
Then make the file executable (runable):
$ sudo chmod +x loopingVideo.sh
Type "ls" again and see that the file has changed color to green:
8. Disable dhcp (internet)
This is not mandatory, but the boot will go faster as the pi will not spend time looking for internet connection that is not there:
$ sudo update-rc.d dhcpcd disable
If you want to enable internet again, type:
$ sudo update-rc.d dhcpcd enable
9. Hide console and autorun script on startup boot
Using "/.bashrc" makes it possible to use the keyboard to exit script loop with "ctrl-c". ".bashrc" runs after the user is logged in. It also means that the script will start if you connect to the pi with ssh.
$ sudo nano /home/pi/.bashrc
Type this at the bottom of the text. The first line here makes sure the screen behind the video is black and is not showing console text. "0" means it turns the screen black after 0 minutes. "setterm -cursor off" turns off the blinking type cursor. And the last line runst the script.
setterm -blank 0 setterm -cursor off /home/pi/loopingVideo.sh
10. Use correct video codec
- Copy your videofile to USB drive. Make sure the filename does not contain special characters or spaces.
- h264 codec works best. Apple Prores or Intermediate codec does not work.
- You can place multiple files on USB drive and they will play after each other
- You can also place audio wav files on the USB drive
11. Boot up pi
- Insert USB drive before you give power to the pi. If you boot up the pi without USB drive connected, it will stop booting when trying to find USB drive. (/dev/sda1). Insert USB drive or the boot will fail.
- Connect pi to screen and power on screen
- The last thing to connect to pi is power
- The movie should automatically start playing after boot sequence with no console text showing.
- You can now use the pi with just hdmi, USB drive and power connected. If you want to abort the video and do something on the pi, just connect a keyboard and press "ctrl-c". If you want the type cursor back type "setterm -cursor on".
Lightning icon on screen means it is getting too little power -> use power supply with higher ampere
Issues when formatting on Windows
We had trouble getting this to work if the USB drive was formatted on Windows. It worked if we used macOS or Raspberry Pi to format.
Here is how to do it on the pi: To show the USB drive among all storage partitions and volumes on your pi use:
You can also use:
Suppose it may be /dev/sda1. Unmount it with:
$ sudo umount /dev/sda1
To format drive with the FAT32 file system format:
$ sudo mkfs.vfat /dev/sda1
To set a label name for your drive:
$ sudo mkfs.vfat -n 'name_for_your_pendrive' /dev/sda1