I found lots of disparate information across the internet as many things are with Raspberry Pi software projects. I wanted to be able to live stream a webcam I’ll have affixed to my 3D printer so myself and others can check out how it’s
spectacularly failing making awesome new things. Here’s a concise how-to. As always, never blindly run commands from the interwebs on your local machine. Verify sources, and read up on man pages about any command you’re unfamiliar with. This not only bolsters your stance on security, but you learn a thing or two along the way!
Warning: This is not a “im new to linux and what are $THING” friendly guide. You’re either going to learn a little bit about how Linux manages devices, building from source, and other neat things, or you will blindly install things and potentially hose your meme box. Do your homework, or at least comment!
- Raspberry Pi 3
- Raspbian 8 (Debian Jessie)
- 100Mbit link
- Logitech C920 (1080p, ~$60)
Install x264 and ffmpeg
I had trouble getting avconf (ffmpeg’s successor) to work. Support for the hardware acceleration the Raspberry Pi 3 (and others maybe?) possesses I could not find in avconf. I didn’t try very hard because it was frustrating and this Just Worked (TM).
[ x264 ]
mkdir -p ~/ffmpeg-source && cd ~/ffmpeg-source
tar jxvf last_x264.tar.bz2 && cd x264-snapshot-*
./configure --enable-shared --prefix=/usr
sudo make install
[ ffmpeg ]
git clone https://github.com/FFmpeg/FFmpeg.git && cd FFmpeg
# below is super important, it enables usage of the magical hardware support
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-omx --enable-omx-rpi --enable-nonfree
# the below will take a while. probably a hell of a lot longer on anything but a Pi 3. i hear the zero can take 8 hours to build this. omit the -j4 on non-Pi 3's
sudo make -j4
sudo make install
ffmpeg -f mp3 -ac 2 -i ~/Music/vaporwave_mix.mp3 -f v4l2 -s 1920x1080 -r 30 -input_format h264 -i /dev/video0 -vcodec copy -r 15 -g 30 -b:v 3000k -ar 44100 -threads 6 -b:a 96k -bufsize 3000k -f flv rtmp://a.rtmp.youtube.com/live2/KEYIDHERE
Many of these arguments are positional. For the non-technical, this means that their position or order in the command has some level of importance. For example, -i is provided near both the “mp3” and for /dev/video0 this is because it’s meaning is universal. More elaboration below.
-f force the format. this means you can explicitly define what the format is of the content you are manipulating. in our first instance, we are declaring an mp3. in the second, “v4l2” which is the video4linux2 video driver that allows us to get video from our webcam. in the third example we are forcing flv as this is the best we can do that youtube live (via RTMP) supports.
-ac the number of audio channels present. for example, mono=1 and stereo=2
-i input/intake, what is the source of the content? in the first example, we are referencing a file on our local file system. youtube live does not allow you to not provide audio, so i’m putting on a nice lengthy vaporwave mix. you could provide the audio via a microphone as well if this were an interactive stream.
-s size, resolution. i’m using 1080p (1920×1080). for higher framerates and less CPU crunch you could kick it down to 720p or even 480p.
-r rate, framerate. 30 fps is a pretty good median, but may be too much for some low power platforms. i hear that youtube won’t stream well at anything less than 15 FPS.
-input_format this is the declaring the format in which your video is being received. h264 is desirable as it means we don’t need to do additional encoding which is very cpu intensive and would likely destroy this whole scenario.
-vcodec video codec, in our example we are advising to use whatever is provided in the source, to avoid encoding.
-g i honestly don’t remember what this means and couldn’t find it in my man page! easter egg!
-b:v bitrate:video, you’ll need to calculate this ceiling based on your connection speed.
-ar audio rate, 44100 is CD quality and is perfectly acceptable for most purposes.
-threads the amount of threads given to ffmpeg to perform the operation, this may need to be tuned depending on your platform.
-b:a bitrate:audio. how big the audio channel will be when you try to squeeze it down the pipe. too small and you end up losing quality. too high and you have diminishing returns. most users don’t need to care about this.
rtmp://a.rtmp.youtube.com/live2/KEYIDHERE this is the destination we’re going to through our output FLV encoded output against. add your youtube live key to the end when you plug it into the example