Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edwardtoday/8aebc420b9a9d8a70b31b1366074aa5e to your computer and use it in GitHub Desktop.
Save edwardtoday/8aebc420b9a9d8a70b31b1366074aa5e to your computer and use it in GitHub Desktop.
基于树莓派的直播平台系统搭建

#基于树莓派的直播平台系统搭建

##1. 安装FFMPEG并添加H.264编解码器支持

###1.1 更新源并安装git

	sudo apt-get update

	sudo apt-get install git

###1.2 x264配置脚本config_x264_rpi.sh,放进x264目录

	#!/bin/sh

	./configure \

	--disable-shared --enable-static \

	--enable-strip \

	--disable-cli

###1.3 下载x264源码并编译安装

	git clone git://git.videolan.org/x264.git

	cd x264

	mv ../config_x264_rpi.sh ./

	chmod +x config_x264_rpi.sh

	./config_x264_rpi.sh

	make -j4

	sudo make install

###1.4 ffmpeg配置脚本config_ffmpeg_rpi.sh,放进ffmpeg目录

#!/bin/sh
PREFIX=/usr/local
./configure \
--enable-gpl    --enable-version3 --enable-nonfree \
--enable-static --disable-shared \
\
--prefix=$PREFIX \
\
--disable-opencl \
--disable-thumb \
--disable-pic \
--disable-stripping \
\
--enable-small \
\
--enable-ffmpeg \
--enable-ffplay \
--enable-ffserver \
--enable-ffprobe \
\
--disable-doc \
--disable-htmlpages \
--disable-podpages \
--disable-txtpages \
--disable-manpages \
\
--disable-everything \
\
--enable-libx264 \
--enable-encoder=libx264 \
--enable-decoder=h264 \
--enable-encoder=aac \
--enable-decoder=aac \
--enable-encoder=ac3 \
--enable-decoder=ac3 \
--enable-encoder=rawvideo \
--enable-decoder=rawvideo \
--enable-encoder=mjpeg \
--enable-decoder=mjpeg \
\
--enable-demuxer=concat \
--enable-muxer=flv \
--enable-demuxer=flv \
--enable-demuxer=live_flv \
--enable-muxer=hls \
--enable-muxer=segment \
--enable-muxer=stream_segment \
--enable-muxer=mov \
--enable-demuxer=mov \
--enable-muxer=mp4 \
--enable-muxer=mpegts \
--enable-demuxer=mpegts \
--enable-demuxer=mpegvideo \
--enable-muxer=matroska \
--enable-demuxer=matroska \
--enable-muxer=wav \
--enable-demuxer=wav \
--enable-muxer=pcm* \
--enable-demuxer=pcm* \
--enable-muxer=rawvideo \
--enable-demuxer=rawvideo \
--enable-muxer=rtsp \
--enable-demuxer=rtsp \
--enable-muxer=rtsp \
--enable-demuxer=sdp \
--enable-muxer=fifo \
--enable-muxer=tee \
\
--enable-parser=h264 \
--enable-parser=aac \
\
--enable-protocol=file \
--enable-protocol=tcp \
--enable-protocol=rtmp \
--enable-protocol=cache \
--enable-protocol=pipe \
\
--enable-filter=aresample \
--enable-filter=allyuv \
--enable-filter=scale \
--enable-libfreetype \
\
--enable-indev=v4l2 \
--enable-indev=alsa \
\
--enable-omx \
--enable-omx-rpi \
--enable-encoder=h264_omx \
\
--enable-mmal \
--enable-hwaccel=h264_mmal \
--enable-decoder=h264_mmal \
\
	

###1.5 在FFmpeg官网获取源码 http://ffmpeg.org/download.html 当前版本为 ffmpeg-3.3.2.tar.bz2 ,配置完成后编译并安装

	wget http://ffmpeg.org/releases/ffmpeg-3.3.2.tar.bz2

	tar jxvf ffmpeg-3.3.2.tar.bz2

	cd ffmpeg-3.3.2

	mv ../config_ffmpeg_rpi.sh ./

	chmod +x config_ffmpeg_rpi.sh

	./config_ffmpeg_rpi.sh

	make -j4

	sudo make install

输入ffmpeg并回车,其中带有有h264_omx和h264_mmal字样,说明ffmpeg已支持树莓派的H.264硬件编解码器。

##2. 安装Nginx

###2.1 安装nginx web服务器 (约6MB)

	sudo apt-get install nginx -y

###2.2 修改nginx的配置文件(

	sudo nano /etc/nginx/sites-available/default

配置文件内容,务必注意以下是两个不同文件

RTMP下

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
    rtmp {
        server {
            listen 1935;    #监听1935端口,接受推流
            #chunk_size 40000;
            application live {    #rtmp地址
                live on;
                 #exec_static  /usr/local/ffmpeg/bin/ffmpeg -i tcp://127.0.0.1:8181?listen
                #                   -c:v copy
                 #                   -f flv rtmp://localhost:1935/live/mystream;
            }
            application hls {    #hls地址
                live on;
                hls on;
                hls_path /tmp/hls;    #切片ts文件及m3u8文件存放路径
                hls_fragment 10s;
                hls_playlist_length 30s;
            }
        }
    }
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location /hls {
                types {
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
                }
                root /tmp;
                add_header Cache-Control no-cache;
}
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

HLS下

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /hls/ {
                root /run/shm;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

###2.3 启动nginx

	sudo /etc/init.d/nginx start

nginx的www根目录默认在 /usr/share/nginx/www中

##3. 安装Picam

###3.1 开启树莓派摄像头

	sudo raspi-config

###3.2 安装依赖包

	sudo apt-get update

	sudo apt-get install libharfbuzz0b libfontconfig1

###3.3 安装配置文件

cat > make_dirs.sh <<'EOF'
#!/bin/bash
DEST_DIR=~/picam
SHM_DIR=/run/shm

mkdir -p $SHM_DIR/rec
mkdir -p $SHM_DIR/hooks
mkdir -p $SHM_DIR/state
mkdir -p $DEST_DIR/archive

ln -sfn $DEST_DIR/archive $SHM_DIR/rec/archive
ln -sfn $SHM_DIR/rec $DEST_DIR/rec
ln -sfn $SHM_DIR/hooks $DEST_DIR/hooks
ln -sfn $SHM_DIR/state $DEST_DIR/state
EOF

###3.4 添加脚本可执行命令

	chmod +x make\_dirs.sh

	./make\_dirs.sh

###3.5 安装Picam

	wget https://github.com/iizukanao/picam/releases/download/v1.4.6/picam-1.4.6-binary.tar.xz

	tar xvf picam-1.4.6-binary.tar.xz

	cp picam-1.4.6-binary/picam ~/picam/

###3.6 运行Picam

	cd ~/picam

	./picam --alsadev hw:1,0

Github地址:https://github.com/iizukanao/picam

完成上述步骤后即可在局域网下访问树莓派 获得HLS/RTMP视频流

HLS:

	sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf.picam

	sudo /home/pi/picam/picam -v 768000 -o /run/shm/hls

播放地址:http://YOUR_IP:80/hls/index.m3u8

RTMP:

	sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

	/usr/local/ffmpeg/bin/ffmpeg -i tcp://127.0.0.1:8181?listen -c:v copy -ar 44100 -ab 40000  -f flv rtmp://localhost:1935/live/mystream

播放地址:rtmp://YOUR_IP:1935/live/mystream

##4. 安装Ngrok

以安装64位Centos的服务器为例安装Ngrok的Server端

需要将制定域名解析到该服务器上

所需软件下载:

GO的下载地址:http://www.golangtc.com/download

GIT下载地址:https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz

ngrok克隆地址:https://github.com/inconshreveable/ngrok.git

###4.1 安装git

####安装git所用到依赖包

	yum -y install zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker hg wget gcc gcc-c++

####下载git

	wget https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz

####解压git

	tar zxvf git-2.6.0.tar.gz

####编译git

	cd git-2.6.0

	./configure –prefix=/usr/local/git

	make

	make install

####创建git的软连接

	ln -s /usr/local/git/bin/\* /usr/bin/

###4.2 安装go环境

####下载go环境

	wget http://www.golangtc.com/static/go/1.4.2/go1.4.2.linux-amd64.tar.gz

####解压

	tar zxvf go1.4.2.linux-amd64.tar.gz

	mv go /usr/local

####创建软连接

	ln -s /usr/local/go/bin/\* /usr/bin

####编译ngrok

	cd /usr/local/

	git clone <https://github.com/inconshreveable/ngrok.git>

	export GOPATH=/usr/local/ngrok/

	export NGROK\_DOMAIN=”你得域名”

	cd ngrok

###4.3 生成域名证书并拷贝到指定位置

	openssl genrsa -out rootCA.key 2048

	openssl req -x509 -new -nodes -key rootCA.key -subj “/CN=$NGROK_DOMAIN” -days 5000 -out rootCA.pem

	openssl genrsa -out server.key 2048

	openssl req -new -key server.key -subj “/CN=$NGROK_DOMAIN” -out server.csr

	openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000

	cp rootCA.pem assets/client/tls/ngrokroot.crt

	cp server.crt assets/server/tls/snakeoil.crt

	cp server.key assets/server/tls/snakeoil.key

###4.4 编译服务端和客户端

指定编译环境变量,如何确认GOOS和GOARCH,可以通过go env来查看

####编译服务端

	cd /usr/local/go/src

	GOOS=linux GOARCH=amd64 ./make.bash

	cd /usr/local/ngrok/

	GOOS=linux GOARCH=amd64 make release-server

####由于是linux操作系统,所以使用下面的编译客户端

	cd /usr/local/go/src

	GOOS=linux GOARCH=amd64 ./make.bash

	cd /usr/local/ngrok/

	GOOS=linux GOARCH=amd64 make release-client

Windows的客户端编译(64位,32位的把GOARCH的值改为386)

	cd /usr/local/go/src

	GOOS=windows GOARCH=amd64 ./make.bash

	cd /usr/local/ngrok/

	GOOS=windows GOARCH=amd64 make release-client

####启动服务端

/usr/local/ngrok/bin/ngrokd -domain=”$NGROK_DOMAIN” -httpAddr=”:80”

###4.5 客户端的使用及其配制文件

客户端配置文件(ngrok.cfg)

	server_addr: “Your_Domain:4443”

	trust_host_root_certs: false

客户端使用

RTMP:

	sudo /home/pi/ngrok_client/ngrok_tx  -proto=tcp -config=/home/pi/ngrok.cfg 1935 

HLS:

	sudo /home/pi/ngrok -config=/home/pi/ngrok.cfg -subdomain=test  80

完成上述操作即可从公网访问

RTMP:

播放地址:http://YOUR_DOMAIN:PORT/hls/index.m3u8

HLS:

播放地址:rtmp://YOUR_DOMAIN:PORT/live/mystream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment