Nginx直播推流

1、环境配置

  • 1.系统:Linux虚拟机
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.2 LTS
Release:	18.04
Codename:	bionic

2、编译工具

  • 1、FFmpeg的编译,最好加上x264
. /configure  --enable-libx264 --enable-gpl
make -j4 & make install
  • 2、nginx的编译
./configure --add-module=/home/hou/nginx/nginx-rtmp-module
make
make install
  • 3、配置nginx.conf文件

user  root;
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;
	chunk_size 4096;   # 数据传输块的大小
	application vod {
	    # 视频文件存放位置。
            play /home/hou/nginx/video/vod;
        }
        application live {
            live on;#开启直播
			# 配置HLS m3u8 文件的存放地址,最好和nginx的sbin文件保持一致
			# 地址就是根据ffmpeg推流 http://xxx/live_hls/home.m3u8
			hls on;#开启HLS直播
			hls_path /home/hou/nginx/m3u8File;
			hls_fragment 2s;
			hls_playlist_length 16s;
			# 录制模块 开启URL http://xxx/control/record/start?app=live&name=home&rec=myRecord
			recorder myRecord {
					# 录制all(音频和视频)audio video keyframes manual(手动开启和关闭,默认自动录制)
					record all;
					# 用于将当前时间戳添加到己被记录的文件中
               		record_unique on; 
					# 设置录制文件输出的文件名
					record_suffix -%Y-%m-%d-%H_%M_%S.flv;
					# 先手动创建一个目录mkdir /myRecord/flv;
					record_path /home/hou/nginx/video/record/flv;
					# 是指最大录制文件的大小
					# record max size 5000GB
			}
			#  drop 命令有选择地踢出推流用户或拉流用户。
			#  踢出推流 http://xxx/control/drop/publisher?app=mylive&name=777
			#  踢出拉流流 http://xxx/control/drop/subscriber?app=mylive&name=777
			#  redirect 命令有选择地重定向推流用户或拉流用户
			#  重定向推流地址到newname http://xxx/control/redirect/publisher?app=mylive&name=77&newname=l
        }
    }
}

# HTTP can be used for accessing RTMP stats

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       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
		#支持HLS模块,也是实时生成m3u8,实时删除切片的方式直到直播结束
		location /live_hls {
		             types{
				#m3u8 type的设置
				application/vnd.apple.mpegurl m3u8;
				#ts 分片文件的设置
				video/mp2t ts;
				}  
				#指定访问m3u8文件目录
				alias /home/hou/nginx/m3u8File;
				#禁止缓存
				add_header Cache-Control no-cache;
		}
		# 控制器模块,可以通过http协议来控制rtmp 可以使用record、drop、redirect命令
		location /control {
			rtmp_control all;
		}
		# rtmp stat 是流数据统计模块
		location /liveStat {
			rtmp_stat all;
			rtmp_stat_stylesheet stat.xsl;
		}
		location /stat.xsl {
			rtmp_stat all;
			root /home/hou/nginx/down/NRM;
		}
        #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;
    #    }
    #}

}


3、开始推流拉流

3.1 启动nginx

找到安装路径sbin文件夹下的nginx
直接运行:nginx
修改conf之后刷新运行:nginx -t 

3.2 开始推流

1、没有装x264 和 aac 编码的推流
ffmpeg -re -i orange.mp4 -c copy -f flv rtmp://xxx:1935/live/home
2、有装x264 和 aac 编码的推流
ffmpeg -re -i orange.mp4 -vcodec libx264 -acodec aac -f flv rtmp://xxx:1935/live/home

3.3 VLS工具拉流验证

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

记住我的名字啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值