Remote Recording Solution (Part 3) – PiCam!

A.K.A. How do I use this now???


Holy moly! You’ve made it to part three (the final stage) of the remote recording solution, PiCam! So you’ve followed Part 1 and bought all the parts you need and gotten them assembled. You’ve followed Part 2 and got all the software bits running that you need… So now you just need to put it all together and make the hardware and software do what you want!

If you recall in the final steps of the part 2, you ran the gst-launch to test that the software ran properly. The final step is similar, only we’re going to add a bunch of add-on commands to the gst-launch to make it do what we want. This will cause it to run things depending on how we want to do them.

Here’s the general command I use to launch my streamer/recorder depending on which I’m doing

Saving to file:

gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! filesink location=/home/pi/Videos/file.flv

 

 

Streaming to RTMP Server (Chew.tv, Youtube, etc):

gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location=rtmp://rtmpserver/streamkey

 

Now… You’ll notice there are some settings you can change here. The above is an example for a 1080p 30fps stream. But what if you wanted to do 720p? Or 60fps? Yes, the PiCam CAN do 60fps 720p streaming/recording, which I think is pretty freaking awesome for such a compact little device. So… how would you do that? Well you the width&height should be obvious, then to change the FPS you change framerate from 30/1 to 60/1. So it would look something like video/x-h264,width=1280,height=720,framerate=60/1,profile=high That tells it to do 1280×720 video at 60 frames per second.

You can change the bitrate if you want, but know that larger bitrates increase filesize, while smaller bitrates decrease quality. It’s really a balancing act based on what you’re trying to do. Larger filesizes/bandwidths are required for higher bitrate and resolutions/FPS streams. With the above settings, it saved a test file of about 16 seconds and used 4mb of space. Of course the grows with the length of stream when you’re saving a file, and if you’re streaming, you simply need to make sure you have enough bandwidth available for it.

The other difference you can see between the two options is the method for the sink (output). In the first example, we’re using filesink with a location specified to a directory and file name on the Pi. In the second example, you see we’re using an rtmpsink with location to the rtmpurl that you would use for Chew/Youtube/Twitch/Whatever.

For those trying rtmp streaming for the first time, most stream hosts will give you your rtmp url broken down in to two parts, commonly called the Stream URL and the Stream Key For instance, Chew.tv may give you Stream URL: rtmp://stream-na.chew.tv/live and Stream Key: chew-someusernamehash  You then put that information into most of the stream software separately. But really you’re setting the rtmp stream to go to the designated URL rtmp://stream=na.chew.tv/live/chew-someusernamehash and that is what’s called when a viewer pulls up the stream. So what we’re doing in the second example is simply combining it into one RTMP url and the server knows what to do with it. It’s the same with youtube, twitch, etc. You’ll get a stream url, and a “stream key” which is usually just a unique hash to your channel, which you can then reset. Chew.tv and some others however I believe give a unique key each stream, so you’ll need to pay attention depending on which streaming service you’re using the camera with.


Advanced PiCam Steps

So you may be wondering, “Is there a way I can automate some of these startup steps?” Well… when there’s a will, there’s always a way. For instance, I wanted to create a simple bash script to automatically call up the proper gstreamer launch configuration dependent upon what I wanted to do. Now I warn you: this is just a test script that does what I want it to do. There are probably better ways to do this, but this is simply the method I chose to accomplish what I wanted to do.

As I’ve said in the earlier guide steps, I enjoy command line workings. I use SSH to connect to my pi, and go about what I need to do that way. I wanted a method that I could automate my steps and simply run a simple script thru an ssh client from my phone to start the pi-cam. Voila!

#!/bin/bash

#Set date variable for file saving by datetime
dt=$(date '+%Y%m%d-%H%M%S');

PS3='Please enter your choice: '
options=("1080p30 Stream" "1080p30 File" "720p60 Stream" "720p60 File" "720p30 Stream" "720p30 File" "Quit")
select opt in "${options[@]}"
do
case $opt in
"1080p30 Stream")
echo "Please enter RTMP to stream to:"
read url
echo "Streaming: 1080p30 to $url"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location=$url
break
;;
"1080p30 File")
echo "Saving to file: 1080p30"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! filesink location=/home/pi/Videos/$dt-1080p30.flv
break
;;
"720p60 Stream")
echo "Please enter RTMP to stream to:"
read url
echo "Streaming: 720p60 to $url"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location=$url
break
;;
"720p60 File")
echo "Saving to file: 720p60"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1280,height=720,framerate=60/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! filesink location=/home/pi/Videos/$dt-720p60.flv
break
;;
"720p30 Stream")
echo "Please enter RTMP to stream to:"
read url
echo "Streaming: 720p30 to $url"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1920,height=1080,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location=$url
break
;;
"720p30 File")
echo "Saving to file: 720p30"
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1280,height=720,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! filesink location=/home/pi/Videos/$dt-720p30.flv
break
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done

You’re probably looking at that and going, “HOLY SHIT WHAT IS THAT??” And to be honest, any time you look at a script on the web, you should think that and not use it unless you understand it and know what it’s doing. I’m merely providing this for an example. This is a simple bash script (the common command like scripting for many linux installs/distros) that offers 6 different streaming options and an exit option. The first the the script does is set a date/time variable since I couldn’t figure an easy way to include it in the filesink.

All of this is saved to a filename.sh which I provided execute privileges to, and I simply run it and choose what I want to do.

Then it provides the options for the choice between the three resolutions/fps combos I wanted to allow (1080p 30fps, 720p 60fps, 720p 30fps). The user then selects whichever option they want and the script goes through a case statement to match the option they chose.

If the user chose a streaming option, the script then prompts for the RTMP url, and sets whatever the user inputs as a variable, then launches gstreamer with the proper resolution/fps setting and the rtmpsink with the rtmp url variable.

If a file option is chosen, it launches gstreamer with the resolution/fps and sets the filesink to save to the /home/pi/Videos/ directory with a datetime-resolutionfps.flv filename. The other things are just some fluff echos that I’ve added to display information of choice, etc.

As you can see, with some intuition and basic research you can get this little inexpensive device to do a whole heck of a lot! Hopefully this tutorial/guide has inspired you to get involved in the Raspberry Pi project, and maybe I’ll see you streaming on Chew.tv soon!