Nginx直播推流

1、环境配置

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

  • 2.FFmpeg 版本4.0.4,点击下载
  • 3.nginx 点击下载
  • 4.nginx-rtmp-module 点击下载

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工具拉流验证

Nginx直播推流_第1张图片

你可能感兴趣的:(直播)