Live Stream Your Pets with Linux and YouTube!

Next, click on the "Output" tab (Figure 5). My settings are visible, and I recommend keeping them close to mine, except for the bitrate of the video and audio. If you want higher quality video (and you can afford the bandwidth), this is where you set the average upload speed. You also can change the audio quality if you want higher quality. Keep in mind that the resolution you chose in the last step will work with the bandwidth you selected here to give you the video quality users will see. A video rate of 1500 (measured in kbps) works well with my 10FPS and 720p resolution. But if you try to stream 1080p, 30FPS video with 1500kbps, it's going to be really poor quality video. You'll have to experiment to find the sweet spot.

Figure 5. 1500 is the maximum my current internet connection can handle.

The "Stream" tab is where you configure the streaming service you want OBS to use (Figure 6). You should be able to select YouTube and then paste that stream key you got from YouTube earlier. (This is not the channel ID; it's that hidden key from back in Figure 2.) Once entered, you shouldn't need to make any changes in settings. OBS will keep all the settings, including streaming information.

Figure 6. It's truly amazing how well OBS does with YouTube streaming.

All that's left is to add the camera(s) to your preview screen. This is the nicest feature of OBS, well apart from actually being able to stream to YouTube. The setup is drag and drop, and you can resize cameras, overlap cameras and arrange them however you want. Since OBS supports so many types of inputs, you can get crazy with text overlays and so on. To add a network camera, click the + at the bottom middle of the main window, and select "media source" (Figure 7). Then uncheck "local file" and enter the camera URL in the "input" field (Figure 8). Once you click OK, your camera should appear on the preview window, and you can resize and move it. The interface also allows you to crop the section of the video you want to use. It's very powerful and incredibly user-friendly. Plus, as I mentioned earlier, OBS stores all your tweaks automatically, so the next time you start it, you'll get the same arrangement.

Figure 7. Media source isn't obvious as the choice for network cameras.

Figure 8. Be sure to uncheck the "local file", or you won't have an input field.

Once you have your camera(s) set up, you can decide whether you want to include audio if your camera supports it. The audio levels should appear in the column next to the list of cameras. Then just click "Start Streaming" to send your stream live to YouTube. It takes 30 seconds or so to show up in the YouTube dashboard, but now is the time to make sure streaming works.

Automation

I could just leave OBS running 24/7 and have it stream my bird feeders all night. Honestly, I'm not sure how YouTube would handle a 24/7 stream, but I don't want to do that anyway. I not only want to automate the starting and stopping of OBS, but I also want to make sure that if something crashes, it starts back up the next day without me needing to fix it. Cron was the obvious way to manage that, but since OBS is a GUI program, cron proved to be challenging. In the end, I was able to include environment variables in my crontab, and things worked smoothly. Here's what my OBS part of crontab looks like. Check it out, and I'll explain it afterward:


DISPLAY=:0
@reboot sleep 10; obs --startstreaming
0 5 * * * /usr/local/bin/sunwait civ up 45.3733N 84.9553W;
 ↪obs --startstreaming
0 16 * * * /usr/local/bin/sunwait civ down 45.3733N
 ↪84.9553W; pkill obs

First off, setting the DISPLAY environment variable to :0 means that crontab can launch a GUI application on the current desktop. I was embarrassed when I realized how easy it was to get cron to launch GUI apps. It is important to note that the user must be logged in, however.

The @reboot line starts OBS when the system boots. The simple --startstreaming flag tells OBS to launch and immediately start streaming. It's awesome. Really, if I had to figure out a way to automate actually clicking a button, we probably wouldn't be doing this project together.

The next two lines are a little confusing. First off, I have the program "sunwait" installed. It's an old program, but it's so incredible, I can't believe it's not in every distribution by default. I've mentioned it before in BirdCam articles, but basically, it's a C program that determines sunrise and sunset based on your longitude and latitude. The last version was released in 2004 (seriously), but it still compiles. You can get the source here.

Anyway, those two cron lines tell the server to start and stop OBS at sunrise and sunset. At 5AM, I tell sunwait to "wait" until the sun rises. It literally just waits until sunrise and then ends. Once it ends, OBS is started up. Then at 4PM, I tell sunwait to wait until sunset, and after the sunwait program ends, kill stops OBS. Why 5AM and 4PM? Well, in my part of the world, the sun never rises before 5AM and never sets before 4PM. There is the potential problem that if I reboot my server after 4PM, it will stream all night. But that potential problem doesn't concern me enough to make the logic more complicated.

Since my server doesn't have a monitor or keyboard connected, a random GUI application starting and stopping in the middle of the screen doesn't affect anything. Since I connect to my server's desktop only when I want to make a change to OBS, it's actually convenient that it's always running front and center on my desktop! I couldn't be happier with the current live stream setup.

______________________

Shawn Powers is a Linux Journal Associate Editor. You might find him on IRC, Twitter, or training IT pros at CBT Nuggets.