Last active
May 1, 2019 20:26
-
-
Save kennyk/18038dce3177a64654d2dc2b1ad52169 to your computer and use it in GitHub Desktop.
Notes on live streaming from raspberry pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thanks https://johnvoysey.wordpress.com/2014/05/07/raspberry-pi-camera-live-streaming/ | |
libx264 | |
Compiling the libx264 library from source is relatively straightforward. You will need git installed to check out the latest version. Use the following commands to check out, compile & install libx264: | |
git clone git://git.videolan.org/x264.git | |
cd x264/ | |
./configure --enable-static --enable-shared | |
make | |
make install | |
ldconfig | |
FFmpeg | |
Compiling FFmpeg can take a long time – it took about 5 hours to complete on my Raspberry – and will use a significant amount of memory. While compiling this on a Raspberry Pi Model A (with 256Mb of RAM), the compile eventually failed due to a lack of memory. | |
To get around this, you can increase the size of the swap file in Raspbian by editing the contents of /etc/dphys-swapfile using vi (or your favourite text editor). | |
By default, it will look like this: | |
CONF_SWAPSIZE=100 | |
In my case, increasing the swap size to 1024 solved the problem, but you can increase or decrease this based on your environment. You will need to reboot for this to take effect. | |
Another change that may help is to decrease the amount of memory the Raspberry allocates to the GPU. By default 128Mb of RAM is allocated to the GPU – for a headless server, this seems a bit excessive. | |
To change the memory split, start raspi-config from a terminal window: | |
sudo raspi-config | |
Then navigate to Advanced Options then Memory Split. Reduce this to 16, hit Ok, then close the tool. You will need to reboot for this to take effect as well. | |
Once you’ve made these changes, you can check out, compile and install FFmpeg using the following commands: | |
git clone git://source.ffmpeg.org/ffmpeg.git | |
cd ffmpeg | |
./configure --enable-gpl --enable-libx264 --enable-nonfree | |
make | |
make install | |
ldconfig | |
As mentioned earlier, this will take about 5 hours to complete. | |
nginx | |
I’m using the nginx web server to host my live streaming. As with FFmpeg, the version of nginx in the Raspbian repositories does not currently have all of the required features, so it needs to be build from source. I’m also using the nginx-rtmp-module plugin for nginx to enable support for the live stream. | |
The first step is to check out the current version of both nginx and nginx-rtmp-module, then compile and install them. This can be achieved using the following commands: | |
Need some additional dependencies for nginx (Thanks https://jobinbasani.com/2014/08/23/nginx-based-streaming-server-raspberry-pi/): | |
apt-get install -y libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev | |
git clone https://github.com/nginx/nginx.git /usr/src/nginx | |
git clone https://github.com/arut/nginx-rtmp-module.git /usr/src/nginx-rtmp-module | |
cd /usr/src/nginx | |
./configure --sbin-path=/usr/sbin/nginx \ | |
--conf-path=/etc/nginx/nginx.conf \ | |
--pid-path=/var/run/nginx.pid \ | |
--error-log-path=/var/log/nginx/error.log \ | |
--http-log-path=/var/log/nginx/access.log \ | |
--with-http_ssl_module \ | |
--without-http_proxy_module \ | |
--add-module=/usr/src/nginx-rtmp-module | |
make | |
make install | |
Once the installation is complete, you’ll need to setup the nginx configuration file. Open /etc/nginx/nginx.conf in a text editor. My nginx.conf file is as follows: | |
worker_processes 1; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
server { | |
listen 80; | |
server_name localhost; | |
location / { | |
root /var/www; | |
index index.html index.htm; | |
} | |
location /hls { | |
types { | |
application/vnd.apple.mpegurl m3u8; | |
video/mp2t ts; | |
} | |
root /var/www; | |
index index.html; | |
add_header Cache-Control no-cache; | |
} | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html { | |
root html; | |
} | |
} | |
} | |
rtmp { | |
server { | |
listen 1935; | |
chunk_size 4000; | |
application hls { | |
live on; | |
hls on; | |
hls_path /var/www/hls; | |
} | |
} | |
} | |
If it doesn’t already exist, create the /var/www and /var/www/hls directories. | |
sudo mkdir /var/www | |
sudo mkdir /var/www/hls | |
Once this has completed, you should be able to start nginx: | |
sudo /etc/init.d/nginx start | |
The final step here is to create a HTML file to access the video stream. Create a file, e.g. /var/www/index.html and paste the following; | |
<html> | |
<body> | |
<video controls="controls" width="1280" height="720" autoplay="autoplay" > | |
<source src="hls/live.m3u8" type="application/x-mpegURL" /> | |
</video> | |
</body> | |
</html> | |
Starting the stream | |
With nginx running, and your Raspberry Pi Camera attached, start the stream by running the following: | |
cd /var/www | |
raspivid -w 1280 -h 720 -fps 25 -t 0 -b 1800000 -o - | ffmpeg -y -f h264 -i - -c:v copy -map 0:0 -f flv -rtmp_buffer 100 -rtmp_live live rtmp://localhost:1935/hls/live | |
By default, this will start a 720p stream. | |
You can adjust the width (-w), height (-h), fps (-fps) and bitrate (-b) to suit your network speed. You can also add -vf and/or -hf if you need to rotate the image. | |
Watching the stream | |
Using Safari (either on OS X or using an iOS device), navigate to http://your.raspberry.pi.address/index.html | |
--------------- | |
Youtube Directions (some repetitiveness here, will maybe cleanup later. The ffmpeg command in the end is what matters): | |
Thanks https://www.youtube.com/watch?v=S3zNBGfPFxI | |
sudo apt-get install libmp3lame-dev; sudo apt-get install autoconf; sudo apt-get install libtool; sudo apt-get install checkinstall; sudo apt-get install libssl-dev | |
mkdir /home/pi/src | |
cd /home/pi/src | |
git clone git://git.videolan.org/x264 | |
cd x264 | |
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl | |
make | |
sudo make install | |
cd | |
cd /home/pi/src | |
sudo git clone git://source.ffmpeg.org/ffmpeg.git | |
cd ffmpeg | |
sudo ./configure --enable-gpl --enable-nonfree --enable-libx264 --enable-libmp3lame | |
sudo make -j$(nproc) && sudo make install | |
raspivid -o - -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/YOURCODEHERE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment