This year I decided to take an impromptu trip to New York to visit the World Maker Faire with my two daughters. Of all the attractions there, the one my youngest daughter (2 years old) can’t stop talking about is The Hand of Man, a 26-foot long hydraulically-actuated hand that picks up cars and drops them. While there, we captured our own video and now all she wants to do is watch it over and over again.
Lately I’ve been doing some testing on the Raspberry Pi Zero with @fhunleth when he mentioned that he put together a simple video player using some demo code from Raspberry Pi. This simple video player would play back video with no sound and boasts 1080p buttery smooth playback. So I decided to create a device that would play back the video on repeat.
Creating a new project
Lets start by generating a new project:
mix nerves.new video_pi --init-gadget
cd video_pi
export MIX_TARGET=rpi0
Next, we will edit the mix.exs
file and add a dependency to our target deps
defp deps(target) do
[
{:nerves_runtime, "~> 0.6"},
{:nerves_init_gadget, "~> 0.4"},
{:rpi_video, github: "fhunleth/rpi_video"}
] ++ system(target)
end
Then, fetch out dependencies
mix deps.get
Finally, lets create some firmware and boot it up. Insert an SD card and run:
mix do firmware, firmware.burn
When fwup
is finished writing to the SD card, remove it from your computer (fwup
automatically ejects the disk) and insert it into your Raspberry Pi Zero. Then, connect a micro USB cable to the USB port labeled: ‘USB’ and not the one labeled: PWR IN
. This will allow your computer to interface with the Raspberry Pi. Plug the other end of the USB cable into your computer and wait a bit for it to power up. After a few seconds you should be able to ssh
to the device by running:
ssh nerves.local
If successful, you will receive an iex>
prompt. To exit the ssh connection and return back to your host, type ~.
. See the nerves_init_gadget
documentation for more information.
Preparing the video
My video was shot on an iPhone and needed to be transcoded in order to be played back by the video player code. On Mac, you will need ffmpeg
which you can install through homebrew.
brew install ffmpeg
Then transcode the video
ffmpeg -i input.mov output.h264
Copying the video to the Pi
With the Raspberry Pi Zero still connected to our machine we need to copy the movie to the device for playback. We can do this using sftp
. The reason we are copying the movie using sftp
instead of including it in the rootfs_overlay
is because of its size. The application data partition has a much larger partition size than the rootfs partitions. Also, if we want to change the video we are playing back, it would be nice to not have to recompile and deploy. Okay, lets put the file on the device. In the following example, you are going to replace /path/to/output_video
with the actual path to where your transcoded video is.
cd /path/to/output_video
sftp nerves.local
You should now be connected to the Raspberry Pi with an sftp>
prompt. To upload the file:
put output.h264 /root/
bye
Playing the video
To play the video you simply need to call RpiVideo.play("/root/output.h264")
. In my case, I simply added a loop to the application start.
spawn(fn -> play_video("/root/output.h264") end)
# later in the file
def play_video(video) do
RpiVideo.play(video)
play_video(video)
end
There is a slight blip of the console session that you can see when the video goes to replay. It would be nice to have an option to look the video which would require additional work. If anyone is interested, you can find more about the code at the rpi_video
source.
This project is a great way to get up and running quickly with Nerves. With Halloween right around the corner, it is also a neat way to add video to costumes and decorations. Please share any enhancements and ways that you’ve used this in the comments.