20 October 2018

Streaming MPEG-TS to your Raspberry Pi

Raspberry Penguin
CC by openclipart.org
Usually people focus on getting camera streams off their Pi, but I wanted to set up a test environment that would let me play with some encoding and distribution software.

For the client in this scenario I'm running a Raspberry Pi 3 Model B which I have plugged into an ethernet cable on my home network. I'm using my Ubuntu 18.04 laptop to serve up the multicast stream.

If you're new to multicast the first piece of reading you should do is to read about the reserved IP ranges on Wikipedia. You can't broadcast on just any old address, but you do have a very wide selection to choose from. I decided to broadcast on udp:// and port 5000, because why not?

I downloaded a video from https://www.stockio.com/free-videos/ and saved it into a folder. From there all you need to do is run ffmpeg on the server, thusly:

ffmpeg -re -i costa.mp4 -bsf:v h264_mp4toannexb -acodec copy -vcodec copy -f mpegts udp://
There are actually some parameters in there that ffmpeg gave me in its error output when I tried to play the file. If you use the same file as me then you need to include them, otherwise you might not need to.

Once ffmpeg had started streaming the file I quickly opened up VLC on the Pi and tuned to the network stream that my laptop was streaming on. Hurray! Everything is working, my UDP multicast is up and running, now I can play with some more advanced features.

Here is a screenshot from my Pi:

Captured from Raspberry Pi, video from https://www.stockio.com/free-videos/
In case you don't want to have to dash over to your other window to see the stream before the clip ends you should know that ffmpeg is able generate test patterns for you. If you're like me and don't want to spend too much time reading the ffmpeg manual, I found another site that has a good little snippet that you can just copy/paste.