PiCam & OBS Integration (Low-Latency Streaming)

Overview

So if you’ve read any of my Chew/PiCam guides (part 1, part 2 and part 3), you’ll know I’m pretty happy with the Chew/PiCam setup. Overall, it’s my goto option when attempting to stream (or record) when at a gig or out in the field. Powered by a nice steady power bank (Anker 20000mah) I can run the PiCam nonstop for any endeavors I’ve tried.

Though when I’m not in the field or at a gig, I yearned for a way to incorporate the PiCam into my normal OBS setup. Unfortunately, you cannot simply plug in the Pi to your computer via USB and have it act as a webcam… At least not that I’ve found a way to do 😉

So, how can you incorporate it? I looked at quite a few options. I currently run two webcams, and was looking to add the Pi to the suare of cameras. So whatever method I chose, it had to be low-latency so it looked in time with the other webcams that were connected directly to the computer. My first attempt involved nginx-rtmp and pulling the feed from that. While it worked, the latency was insane at times… well not insane, but not nearly as low as I’d liked it to be. What would happen on the webcams would sometimes show up on the PiCam feed 10 seconds later. For a live streaming gig, definitely not ideal.

My second attempt was to setup an IP Webcam on it. This worked well setting up thanks to the Pi community and people who’ve honed the setup down to a science. Unfortunately, it too suffered from latency as well as low frame-rate. However, if you’re looking for a home-security style setup then I highly suggest this setup. (Raspberry Pi IP Camera)

I had about given up when I stumbled across some posts on the Pi forums discussing using netcat and piping it thru mplayer. I did not hold my breath, however after testing… I can definitively say it was successful for my needs! So, I decided I’ll share it for those who have a PiCam and want to incorporate it into their home/studio streaming setup.

 

Step 1 – Information

Gather some information from both of your computers. From your windows (or mac) machine, get the IP address. Also grab the Pi IP address if you’re going to remote into it.

 

Step 2 – Pi Setup

So luckily the Pi actually has everything installed that you need on it now. So what we’re going to do is create a quick file for easy startup. We’ve just got a couple of things to do.

First – Create a fifo file on the pi. I do this in the home directory for simplicity. We’ll name the fifo video for ease remembering:

cd ~
mkfifo video

 

Second – Open your favorite editor, mine is nano, and input the following, and save it as a shell file. Mine is named picam.sh:

cat video | nc.traditional 192.168.1.3 5000 & raspivid -o video -t 0 -w 1280 -h 720

 

The IP address/port you input is going to be the ip addressof your Windows or Mac machine that will be running mplayer on it. The -w and -h refer to the height and width you are planning on streaming… I set it to 1280×720 because that’s my preferred resolution. You can set it to 1080p or higher/smaller depending on your preference. Just know that the larger the resolution the more data needs to be sent and could possibly introduce more latency. If you’ve saved this shell file in a directory that is not the same as the video fifo file you created, you’ll need to change the ‘video’ portion to match the location of the fifo file.

Third – After you’ve saved the file, you’ll need one more step for ease. The following tells the Pi it’s an executable shell file that you can run:

chmod +x picam.sh

 

 

Step 3 – Windows/Mac Setup

So I’ll provide the Windows setup, as that’s what I use. However mplayer and netcat are both available for Mac as well, so you can figure that out if you’re a Mac person.

First – Download and install netcat and mplayer for windows. netcat doesn’t appear to have a native build on the sourceforge, however a kind individual has been maintaining a build personally for others to use.

Go here and download/unzip netcat: https://eternallybored.org/misc/netcat/ Once you’ve downloaded it, make note of where you unzip it. For ease, I put it in the root of C, but you can put it anywhere you want, just make a note of it.

Second – Go download and unzip mplayer for windows here: https://sourceforge.net/projects/mplayerwin/ Again, make note of where you unzip. Again for my purposes I unpacked it in root of C.

Third – In your favorite text editor, create a simple text file with the following:

c:\netcat\nc.exe -l -p 5000 | c:\mplayer\mplayer.exe -fps 60 -cache 4096 -

 

Once you’ve saved it, rename it to whatever you want but save it with a .bat extension. This tells window it’s a batch file and that it runs the commands inside of it. The cache you can set to lower if you’d like. I tried 1024 but occasionally emptied the buffer so I increased it a bit just for safety. You’ll also notice I specified 60fps. That’s because the v2 camera for Pi I’m using can do 60fps at 720p. If you’re running larger resolution or simply don’t want 60fps, you can of course change that to 30. In fact, you might want to as your Chew archive will be capped at 30fps anyways. That will lower latency even a bit more as you’re sending half as much data.

 

Step 4 – Low-Latency Magic

That’s all there is to setup, so let’s get this in action.

On the streaming computer, run the batch (or mac variant) file you created. You should see something similar to the following:

Now on the Pi, remote in or whatever you’re doing and switch to where you saved your shell file and run it.

./picam.sh

 

Here you can see I’ve VNC’d into my Pi and run the command in a terminal window in my home directory:

The output on the Pi should start running thru the pipe and pushing to the streaming computer. If you look at the output on the streaming computer, you should see something like the following:

Also you’ll see an mplayer window open after a few moments with the stream from the PiCam! Go ahead, test it out. Look how low-latency that is! Awesome right? Below you can see the stopwatch app in Windows on top, and the picam capture of it displayed in mplayer. Mind you, this is a 720p 60fps stream over a wifi connection:

 

Step 4 – OBS Capture

So, now that the mplayer window is running, you simply need to start up OBS (or whatever software you’re using) and do a game capture and select it.

 

Step 5 – ENJOY!

Ok, so you’ve got it setup, you’re rocking a new PiCam incorporated into your home/studio streaming option. So what now? Enjoy it! If you enjoyed it and want to support other guides like this, feel free to donate to motivate for other solutions etc!