Synchronized Video Playback with Raspberry Pi 2
The Raspberry Pi makes a decent and inexpensive media player for exhibitions. Because it is also a fully functional computer, you can also synchronize playback across multiple machines. There are various methods of doing this, each with their own advantages and disadvantages. This method follows the KISS principle as much as possible, and it applies to a very common scenario:
- You have two screens or projectors
- You have two different videos
- You want the videos to start at the same time
- You want the videos to loop forever
- The videos are the same length
If you want, you can add more screens playing more videos very easily. However, each screen will need its own Raspberry Pi. I will assume you are using a Mac, but the same method should work for Windows or Linux computers (Linux is probably even easier in this case). You are also going to need some other stuff as well:
- HDMI cables to connect your Pi to your screen or projector
- A USB stick
- Ethernet cables for each Pi
- an Etherenet Hub
- a laptop with an Ethernet port
If you have a newer macbook, you can get a dongle which adds an ethernet port. It is only for starting playback, so you don't have to worry about leaving your laptop around unattended.
Getting everything ready can take some time. Fortunately none of the steps are that difficult, and most of them only have to be done once. Here is the overall approach:
- Setup PSSH on your laptop
- Setup the individual Raspberry Pis
- Hook up all the machines with Ethernet
- Use the laptop to start video playback
- Detach your laptop from the network.
Before we begin, you should make sure that your video files are the same length, and encoded as H.264 mp4s. If they aren't, you can run them through something like Adobe Media Encoder to produce the correct files. Put these on your USB key.
Now we can go through each of these parts and look at the details.
Set up PSSH on your laptop
SSH is a program that lets you log into another computer over a network and run programs. If you are on a Mac or Linux computer you have it already installed. We could use SSH to start playback of the videos on a Raspberry Pi from the laptop. However, because we want to start playback on several Raspberry Pi simultaneously, we need a special version of SSH: PSSH ("Parallel SSH"). When its set up correctly, PSSH will send the exact same command to multiple computers, which is exactly what we need.
The easy way to get PSSH on to your macbook is with Homebrew. If you don't have Homebrew already, you will need to open up a terminal ( Applications> Utilities > Terminal) and install it using the command on the Homebrew website. When Homebrew is finished installing itself, you can type this to install homebrew:
brew install pssh
Homebrew will do some magical things, and PSSH should be installed. In order to use it with our Raspberry Pi's we need to do a few other things first.
To use PSSH conveniently, we need to do two things. First, we need to generate some keys that will let us log into the Raspberry Pi's automatically from our laptop without having to put in our password. Second, we need to give PSSH a list of the Raspberry Pi's for it to connect to.
Create some SSH Keys
If you have an SSH key already, copy it to the USB stick. Otherwise, we can generate one and copy that. More detailed explanations of what and why we are doing this are found here. To generate your key from the terminal, type:
ssh-keygen -t rsa -C "email@example.com"
The ssh-keygen program will ask you some questions, but unless you have a good reason to do otherwise just hit Enter and skip through them. Now we need to copy the key to our thumb drive. You can do this easily by typing: cd .ssh open .
This will open the directory with the keys in the Finder for you. Just drag the one called "id_rsa.pub" to your thumb drive. Later we will copy those onto the Raspberry Pi so that we can log in easily.
Create the list of Pis for PSSH
The list of Raspberry Pis is kept in a text file. In terminal, type: cd ~ nano hosts_file
This will open up an editor so you can list the Pis, one on each line, like this: firstname.lastname@example.org email@example.com
If you are setting up more displays, just add another line for each one in the same format. Quit nano (Ctrl+X) and be sure to save the file when it prompts you.
We now have almost everything setup on the laptop for using PSSH to connect to the Raspberry Pis. There are a couple things we need to return to after we setup the Raspberry Pis.
Setup the Individual Raspberry Pis
You will need to do the same things for each Raspberry Pi, the only differences are the numbers you assign to each one, and the video file you copy over.
If your Raspberry Pis are brand new, follow the instructions on this page, starting where it says Download and Image Raspbian Directly.
Once that is finished, or if you already have Raspbian installed, you should check that it is configured to have enough memory for graphics. From the Raspbian terminal window type: raspi-config
Navigate the menu to the option for memory-split and enter 256. This should be enough for playing most videos on a Raspberry Pi 2, if your movies have a really high bit-rate, you might try 512.
From within the config program, you will also find a boot option. Since the machine is only going to be playing movies, you can set this to "Command Line" and that will help save some memory too.
Save and restart the Raspberry Pi.
One thing that you might want to do to free up some space on your Raspberry Pi for your video is to remove the Wolfram Engine which can take up a lot of room. From the Raspberry Pi command line, type: sudo apt-get purge wolfram-engine
Now, lets start the desktop to make copying the videos over easier. Type: startx
This will start the desktop. Insert your USB key. It should be auto-detected, and you should be given an option to view the files. Copy one of the movie file to your main (home) directory. When it has copied over, right click and rename the file "movie.mp4".
Let's check that everything is going alright so far. Open the Raspberry Pi terminal, and type:
This should play your movie. Check that the sound works and that the playback looks alright. If not, you may need to revisit your encoding settings to find something that works better. If you want to check that your movies are all the same length, you can get some useful information from omxplayer by typing:
omxplayer -i movie.mp4
Configure Network Settings on the Raspberry Pis
Each Raspberry Pi needs its own, fixed ip address. To set these, you need to edit the interfaces configuration on the Raspberry pi. From the Raspberry Pi Terminal, type: sudo nano /etc/network/interfaces You will have to enter your Raspberry Pi password, the default is 'raspberry'.
The important part of the interfaces file is the section that reads
it is about 6 lines down. the whole section should read:
auto eth0 iface eth0 inet static address 192.168.0.1 netmask 255.255.255.0 up route add -net 188.8.131.52 netmask 240.0.0.0 eth0
Note the 'address' line. On each Raspberry Pi, this line should correspond to one of the Pis listed in the hosts_file we created on the laptop earlier. So if you had two Raspberry Pis, one should have the address 192.168.0.1, and the other should have the address 192.168.0.2.
To save the file, press Ctrl+X and then y when it prompts tyou to save. Now, when you restart the Raspberry Pi, it should have the address you assigned. You can confirm this from the terminal by typing:
This will display information about the networking setup of the Raspberry Pi. In the section labeled eth0, it should list the IP address you assigned, that is - inet addr: 192.168.0.1).
Set up the SSH keys on the Raspberry Pi
You will need to copy the id_rsa.pub file from your USB stick to the .ssh folder on your Raspberry Pi, and rename it "authorized_keys". If you can't see the .ssh folder, that is because it is hidden. In the Raspberry Pi file manager, make sure "View> Show Hidden" is checked. The shortcut is typing Ctrl+H. We will also need to change the permissions of the authorized_keys file. Type:
sudo chmod 600 ~/.ssh/authorized_keys
Use a script to start Movie Playback
Eventually, we are going to use a script to start movie playback. This because in order to synchronize the videos correctly, we may need to delay the start of one of the films slightly. Make sure you are in your home directory by typing:
Now let's create our script by typing:
This will start a new file for us. The script should look like this:
#!/bin/bash sleep 0 omxplayer -b -loop -no-osd movie.mp4
Save the file by typing Ctrl+X and then y when asked if you want to save. This script has two commands, one tells the computer to wait or 'sleep' for 0 seconds. Later we will change this from 0 to help synchronize the movies. The second command just tells omxplayer to start playing our movie. There are some other options there as well. '-b' tells omx player to black out the computer background, '-loop' tells the movie to play in a loop, and '-no-osd' stops the timeline from appearing when the movie skips back to the beginning. You can find out more about bash scripting here.
Now, we need to change the permissions on the script we just created. Type:
chmod 775 playmovie.sh
This command determines who is allowed to run the script. Now, you can test that the script works by typing:
Your movie should start playing.
Hook up all the machines with Ethernet
Next we need to physically connect all of the machines to the Ethernet hub using our Ethernet cables. We are also going to check that the laptop can communicate with all the Raspberry Pis.
Check Your Laptop Networking
In order for your laptop to communicate with the Raspberry Pis, you will need need to give it a static IP address. If you are on a Mac, look under System Preferences > Networking and find your USB dongle from the list, click on "Advanced" and TCP/IP. Where it says "Configure IPv4", choose "Manually" from the list. Give your laptop the IP address 192.168.0.100, then "Apply" the settings.
Open the terminal on your mac. We will connect to each of the Raspberry Pis individually. To connect to the first Raspberry Pi, Type:
It will ask you if you want to add the Pi to the list of known hosts. Say 'Yes'. When you are connected, test that you can play your movie by typing:
This should start movie playback on the first Pi. Then type:
Now, repeat these steps to verify that you can connect to all of the Raspberry Pis.
Use the Laptop to Start Video Playback
Everything should now be setup to start synchronized movie playback. On your laptop, type:
pssh -h hosts_file ./playmovie.sh
The movies should all start playing. To stop playback press Ctrl+X, then type:
pssh -h hosts_file pkill omxplayer
The movies should all stop.
Tweaking the video start times
When you start playback, you may notice that one of the videos starts to play slightly before the other. In order to fix this, we need to modify our script, playmovie.sh to add a small delay to the movie that starts first. For convenience, you can do this from your laptop by using ssh to connect to the laptop. For example, if the movie that starts first is on the computer with the IP address 192.168.0.1, type:
Next, open the playmovie.sh script by typing
Edit the 'sleep' line to add a few seconds. For example:
will add a delay of two and a half seconds. Save the file by typing Ctrl+X and typing yes when asked if you want to save. Be sure to log out of the Pi, by typing:
Then you can start playback again using pssh:
pssh -h hosts_file ./playmovie.sh
Getting the files perfectly synchronized is now simply a matter of tweaking the 'sleep' line in the script.
Once you have started playback on all the Raspberry Pis, you can disconnect your laptop from the hub. You can also disconnect the Raspberry Pi's from the hub if it is difficult to hide all of the cables in your installation.
I hope this guide was helpful, if you find any mistakes please let me know.