02-流媒体-RTMP服务器搭建

流媒体的是所有传输音视频的总称,本节我们讨论常见的RTMP协议nginx服务器搭建和RTMP推流

过程如下
1.下载对应的服务器包
链接:https://pan.baidu.com/s/1G_YfBXHhbbRByINstOthPw
提取码:7oq1
2.解压
sudo apt-get install unzip
tar -zxvf nginx-1.20.0.tar.gz
unzip master.zip
3.安装对应的需求库
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
4.编译和安装
cd nginx-1.20.0
sudo ./configure --with-http_ssl_module --add-module=…/nginx-rtmp-module-master
sudo make
sudo make install
5.配置
sudo gedit /etc/init.d/nginx
写入内容

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
 
 
 
#注意:这里的三个变量需要根据具体的环境而做修改。
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
 
 
 
RETVAL=0
prog="nginx"
 
# Check that networking is up.
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ]  
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
  echo -n $"Stopping $prog: "
  $nginxd -s stop
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
# reload nginx service functions.
reload() {
  echo -n $"Reloading $prog: "
  kill -HUP `cat ${nginx_pid}`
  RETVAL=$?
  echo
}
# See how we were called.
case "$1" in
  start)
          start
          ;;
  stop)
          stop
          ;;
  reload)
          reload
          ;;
  restart)
          stop
          start
          ;;
  status)
          status $prog
          RETVAL=$?
          ;;
  *)
          echo $"Usage: $prog {start|stop|restart|reload|status|help}"
          exit 1
esac
exit $RETVAL

sudo chmod a+x /etc/init.d/nginx

sudo /etc/init.d/nginx start

7.再次添加配置
sudo gedit /usr/local/nginx/conf/nginx.conf
在文件最后加如下内容

rtmp {
    server {
            listen 1935;
            chunk_size 4096;
            application live {
                    live on;
                    record off;
hls on; 
hls_path /usr/share/nginx/html/hls; 
hls_fragment 2s;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {              
                    live on;
                    record off;
        }
application hls360p {     
                live on; 
                hls on; 
                hls_path /usr/share/nginx/html/hls2; 
hls_fragment 2s;
            } 
    }
}

sudo mkdir -p /usr/share/nginx/html/hls

sudo /etc/init.d/nginx restart

10.推流
ffmpeg -i xx.mp4 -f flv rtmp://ip地址:1935/live/xx
11.拉流
ffplay rtmp://ip地址:1935/live/xxx
弹出对话框,正是xx.mp4的内容
推流和拉流成功

若Ubuntu重启的话,用下面两条命令将服务器重启一次后:
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart

上面步骤完成了推流的过程,这里推流使用的是ffmpeg工具,拉流采用的是ffplay工具,协议使用的是rtmp。本栏目后续章节主要讨论它们的使用方法,原理,和源代码分析。

你可能感兴趣的:(流媒体,linux)