ubuntu16.04安装nginx教程 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器笔记 用ffmpeg命令推流现有文件

简介

RTMP推流器(Streamer)的在流媒体系统中的作用可以用下图表示。首先将视频数据以RTMP的形式发送到流媒体服务器端(Server,比如FMS,Red5,Wowza等),然后客户端(一般为Flash Player)通过访问流媒体服务器就可以收看实时流了。
在这里插入图片描述

1、首先:运行本程序之前需要先运行RTMP流媒体服务器,并在流媒体服务器上建立相应的Application。
2、推流的同时,客户端(例如 Flash Player, FFplay等等)拉流收看推送的直播流(服务器不能长时间保存视频数据)
例如:
  1、推流:ffmpeg -re -i cuc_ieschool.flv -f flv rtmp://192.168.126.128 /live/camera
      ffmpeg RTMP推送命令举例
  2、拉流:VLC打开网络串流:rtmp://192.168.126.128/live/camera
  在这里插入图片描述

封装格式

RTMP采用的封装格式是FLV。因此在指定输出流媒体的时候需要指定其封装格式为“flv”。
比如在推流的时候添加传参命令: -f flv
  同理,其他流媒体协议也需要指定其封装格式。例如采用UDP推流时指定封装格式为“mpegts”。

c代码实现推流—最简单的基于FFmpeg的推流器(以推送RTMP为例)—做了延迟→→按照视频实际的帧率发送
c代码实现收流—最简单的基于FFMPEG的推流器附件:收流器
↓下面则是使用ffmpeg指令来实现推流 用vlc收流

1. 安装nginx的依赖包

# 查看zlib是否安装
dpkg -l | grep zlib
# 解决依赖包openssl安装
sudo apt-get install openssl libssl-dev
# 解决依赖包pcre安装
sudo apt-get install libpcre3 libpcre3-dev
# 解决依赖包zlib安装
sudo apt-get install zlib1g-dev

1.5为nginx添加rtmp支持

为了增加对rtmp的支持,下载nginx-rtmp-module,地址:https://github.com/arut/nginx-rtmp-module#example-nginxconf,这个是个开源项目。下载后解压放到一个目录(如/home/user/nginx-rtmp-module),后面配置nginx要用到这个位置

2. 官网下载nginx

wget命令详解

//下载nginx
wget http://nginx.org/download/nginx-1.13.1.tar.gz

//解压nginx
tar -xzvf nginx-1.13.1.tar.gz   →得到文件夹nginx-1.13.1

3. 安装nginx

//配置nginx
cd nginx解压文件夹
./configure --prefix=/usr/local/nginx  --add-module=/home/user/nginx-rtmp-module(所添加的tmrp解压地址)  --with-http_ssl_module

//编译nginx
make
//安装nginx
make install

3.5修改nginx配置文件nginx.conf进行配置(重启nginx后配置才生效)

gedit /usr/local/nginx/conf/nginx.conf
//弹出文本编辑界面进行修改,类似txt编辑方式

下面是修改后的nginx.conf :
1在里面加入rtmp的配置
2.使nginx能具有直播状态监听的功能

#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;
}

#使nginx增加rtmp功能。
rtmp {  							#协议名称
    server {  						#说明内部中是服务器的相关配置
        listen 1935;  				#rtmp服务监听端口
  
        application live {  		#直播服务器
            live on;  				#live on表示启用rtmp直播
            #allow play all;		#这4行是用于录制的
            #record all;
            #record_path /opt/video/record;	#录制视频的存放路径(记得开启写权限)。
            #r添加后重新启动nginx后重新推流,查看/opt/video/record路径下的文件可以看到多了一个test+时间戳命名的flv文件,这就是录制的视频了。
            #record_unique on;
            						#record off 不记录数据
        }  
        application hls {  			#HLS(HTTP Live Streaming)
            live on;  				#live on 启用rtmp直播
            hls on;  				#hls on 启用hls直播
            hls_path /tmp/hls;  	#hls_path 切片保存位置   #视频流文件目录(自己创建)
            #hls_fragment 3s 		#每个切片的长度
        }  
        application vod{			#点播服务器
            play /opt/video/vod; 	# 被点播的视频文件存放地址
        }
        #只需要吧被点播的视频文件name.mp4放到/opt/video/vod目录下,打开VLC,选择媒体-》打开网络串流,输入如下链接:rtmp://xx.xx.xx.xx/vod/name.mp4
    }  
}  

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;
	
	#添加的新内容--使nginx能具有直播状态监听的功能
	server {
	    listen       8080;		#监听端口->决定了在浏览器中输入http://192.168.1.11.8080/stat 此处的端口号是8080
	    
	    location /stat{							#配置查看服务器状态路由
	            rtmp_stat all;
	            rtmp_stat_stylesheet stat.xsl;
	    }
	    location /stat.xsl{						 #配置状态信息来源
	    #location /stat 和 /stat.xsl只是用来监控服务器和客户端情况的。
	           root /home/user/nginx-rtmp-module;#其中/home/user/nginx-rtmp-module是nginx-rtmp-module文件夹的绝对路径

	    }
	}
	
    server {
        listen       80;			#默认80端口  应该和前面的80802种用途的端口
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        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;
        }

		location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                #或 application/x-mpegURL
                video/mp2t ts;
            }
           alias /usr/local/nginx/html/hls; #视频流文件目录(自己创建)
            expires -1;
            add_header Cache-Control no-cache;
       }

        # 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;
    #    }
    #}

}

4. 检查nginx是否安装成功

cd /usr/local/nginx/sbin
./nginx -t 

如果结果结果显示:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5. 配置用户(不设置也能用)

# 添加www组
groupadd www
# 创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
useradd -g  www www -s /bin/false

6. 配置防火墙 (ubuntu默认不开防火墙,不需要)

7. 启动nginx

# 方法1
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 方法2
cd /usr/local/nginx/sbin
./nginx


关闭./nginx -s stop或者./nginx -s quit

常用命令:
# cp /usr/local/nginx/sbin/nginx /usr/local/bin/    #把nginx加入到环境变量里,不用输入全路径了
# nginx            #第一次启动
# nginx -t        #检查配置文件是否正确
# nginx -s reload  #平滑重启,修改配置文件后,不断服务重启
# nginx -s stop    #停止服务

8可能遇到端口被占用的情况

nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

原因:	1可能是被别的进程占用了80端口 
			查询所有端口netstat -tlnp
			查询特定端口如1935  netstat -tlnp|grep 1935
			然后关掉这个进程,把端口释放出来
			如tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      20211/nginx.conf
			  kill -9 20211//1935端口被进程nginx.conf(其线程号20211)占用
		2也可能是nginx服务已经在运行了,所以再启动时193580端口会占用
			关闭nginx服务自身再打开pkill -9 nginx

9、然后再启动服务,当服务启动成功的话,在本机浏览器输入:http://localhost/ 即http://127.0.0.1

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

如果出现上述内容,则服务成功启动

11、可以通过netstat -ltn 命令可以看到端口的监听情况.

激活Internet连接 (仅服务器)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     

80是nginx默认的http监听端口。

nginx配合ffmpeg做流媒体服务器的原理是: nginx通过rtmp模块提供rtmp服务, ffmpeg推送一个rtmp流到nginx, 然后客户端通过访问nginx来收看实时视频流. HLS也是差不多的原理,只是最终客户端是通过HTTP协议来访问的,但是ffmpeg推送流仍然是rtmp的.

发布了81 篇原创文章 · 获赞 1 · 访问量 2921

猜你喜欢

转载自blog.csdn.net/qq_42024067/article/details/103693800