Skip to content

Instantly share code, notes, and snippets.

@PatchworkBoy
Last active September 14, 2021 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatchworkBoy/b064b2499d7f9c1ef5fa624daad04205 to your computer and use it in GitHub Desktop.
Save PatchworkBoy/b064b2499d7f9c1ef5fa624daad04205 to your computer and use it in GitHub Desktop.
Raspberry Pi Looped Video / Audio Player

Flash Raspbian Buster Lite (https://www.raspberrypi.org/downloads/raspbian/) to an SD. Whack in Pi (plugged into local network, screen, speakers & with keyboard) - power on. Log in (defaults: pi / raspberry).

Force audio thru headphone jack instead of HDMI via:

sudo raspi-config

Interfacing Options > SSH > Would you like to enable? YES > OK
Advanced > Audio > Force 3.5mm ('headphone') jack > Finish

Force 1080p video output:

sudo raspi-config
Advanced > Resolution > CEA Mode 31 > OK > OK > Finish

Bring source trees up to date with:

sudo apt-get update

Install omxplayer:

sudo apt-get install omxplayer

(download & install process takes place - 5 mins-ish)

Then from a PC/Mac on same network, use an SFTP client (eg: WSFTP, Filezilla, CutesFTP) to connect to raspberrypi.local on port 22, with username pi, password raspberry. Upload video into /home/pi

Back on the Pi itself (or, connecting over network from an SSH client eg: putty on Windows, or Terminal on MacOS, using same details as for SFTP above - if macOS in terminal: ssh pi@raspberrypi.local accept host key, password is raspberry)

Create /etc/systemd/system/omxplayer.service:

sudo nano /etc/systemd/system/omxplayer.service

Enter contents (or copy and paste if doing via SSH):

[Unit]
Description=Ambient scenery display
Before=systemd-user-sessions.service

[Service]
TimeoutStartSec=0

ExecStart=/usr/bin/omxplayer -r --loop -o both "/home/pi/video.mp4"
Type=simple
User=pi
ExecStop=/usr/bin/killall omxplayer

Restart=yes

[Install]
WantedBy=multi-user.target

Ctrl-O to save, Ctrl-X to exit.

NB: If only wanting to loop an audio file, replace MP4 file above with MP3 file, don't plug in a screen. Full commandline flags for omxplayer (ExecStart= line above) are at: https://github.com/popcornmix/omxplayer

Then run:

sudo systemctl daemon-reload
sudo systemctl enable omxplayer
sudo systemctl start omxplayer

Video should appear on screen.

Turn off, unplug from network, disconnect keyboard, turn on, done. Pi should boot, play file in loop with visual to HDMI and audio to headphone jack, until power is cut to the Pi.

To replace the video file... plug it back onto your network and turn on. From PC/Mac on same network, use an SFTP client (eg: WSFTP, Filezilla, CutesFTP) to connect to raspberrypi.local on port 22, with username pi, password raspberry. Upload video into /home/pi overwriting the existing one (give it same filename).

Disconnect, cycle power on the Pi.

SDCard also contains a test file with audio - /home/pi/Test_withaudio.mp4 (Source Credit: https://www.beeple-crap.com/vjloops)

Connect via SSH from MacOS Terminal, issue following:

mv Searchlights.mp4 hold.mp4 && mv Test_withaudio.mp4 Searchlights.mp4
sudo systemctl restart omxplayer.service

Player should restart with new file, playing audio to all outputs (HDMI and Headphone Jack), video to HDMI.

To undo:

mv Searchlights.mp4 Test_withaudio.mp4 && mv hold.mp4 Searchlights.mp4
sudo systemctl restart omxplayer.service

Player should now be playing supplied Searchlights.mp4 file again, which doesn't have any audio.

If you make changes to /etc/systemd/system/omxplayer.service, after saving the changes you MUST issue...

sudo systemctl daemon-reload

...to apply those changes.

See https://www.raspberrypi.org/magpi/ssh-remote-control-raspberry-pi/ for details on SSH / Copying files over network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment