Nginx+rtmp实现rtmp,hls直播流

一、nginx+rtmp服务搭建

    下载nginx源码包:

wget https://mirrors.huaweicloud.com/nginx/nginx-1.14.0.tar.gz
  解压缩:
tar -xvzf ./nginx-1.14.0.tar.gz

    下载nginx-rtmp源码包:

wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz

解压缩: 

tar -xvzf ./v1.2.1.tar.gz

 安装nginx+rtmp组件

     nginx (1.3.14 - 1.5.0)版本运行如下命令:

 

./configure --add-module=/path/to/nginx-rtmp-module --with-http_ssl_module
  make
  make install

二、服务配置

1、rtmp点播:

nginx.conf配置文件中,添加rtmp,与http平行:

rtmp{
	server {
       listen 1996;  #//服务端口
       chunk_size 4096;   #//数据传输块的大小
       application vod {
           play /disk1/guoyiman/rtmp/test; #//视频文件存放位置。
       }
  	 }
}


//播放url:rtmp://host:port/vod/videoname

2、rtmp直播:

events {
    use epoll;# 选择epoll模型可以达到最佳的IO性能
    worker_connections  1024;
}

rtmp {                #RTMP服务
    server {
       listen 1996;  #//服务端口
       chunk_size 4096;   #//数据传输块的大小
       application live{ #直播
           live on;
       }
   }
}


rtmp直播推流,拉流播放操作:

使用ffmpeg推流:
ffmpeg -re -i "jin_all.mp4" -f flv rtmp://10.200.20.56:1996/hls/flv
播放url:
rtmp://10.200.20.56:1996/hls/flv

3、hls

RTMP推流请求配置

rtmp {
    server {
        # 监听端口
        listen 1996;
        chunk_size 2048; 
		application hls {
            	live on;		
            	hls on;
            	hls_path /data/hls;		//hls 文件存放地址
        				}
    		}
	}

HTTP播放配置

http{
	...

	server {
        listen       18880;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

         # http播放地址
        location /hls {
                types {
                 application/vnd.apple.mpegurl m3u8;
                 video/mp2t ts;
                }
                root /data;        //hls文件路径
                add_header Cache-Control no-cache;
        }

        # 统计
        location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
                root /disk1/guoyiman/rtmp/nginx-rtmp-module/;
        }
    }


}

直播推流,hls播放

ffmpeg推流:
    ffmpeg -re -i "jin_all.mp4" -vcodec libx264 -vprofile baseline  -f flv rtmp://10.200.20.56:1996/hls/test1
	推流地址:rtmp://10.200.20.56:1996/hls,test1为名称可更改,hls为rtmp 中application 配置名称
rtmp播放:rtmp://10.200.20.56:1996/hls/test1
hls播放:http://10.200.20.56:18880/hls/test1.m3u8
监控统计:访问url:http://10.200.20.56:18880/stat

 

遇到的坑点:

1、配置hls_path路径,使用ffmpeg推流,rtmp直播流可访问,hls直播404,检查原因,hls_path路径下没有ts,m3u8文件;

2、视频流切片存放失败原因:使用nginx配置中没有给nginx指定用户名只能走默认的nobody用户和nogroup用户组,其实就是没有组,需要对写入的目录增大权限的修改

3、修改目录权限仍没有出现ts,m3u8文件,原因:目录权限指全路径而非某一文件夹,所有hls_path中全路径都需要增大权限,避免权限增大影响,在主目录下单独开出data目录增大权限,测试后删除文件夹;亦可对nginx指定高权限用户组,暂未尝试

附录:nginx.conf参考文件



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

	 # http播放地址
    	location /hls {
        	types {
           	 application/vnd.apple.mpegurl m3u8;
            	 video/mp2t ts;
        	}
       	 	root /data; 
		add_header Cache-Control no-cache;  
    	}

	# 统计
   	location /stat {
        	rtmp_stat all;
        	rtmp_stat_stylesheet stat.xsl;
    	}

	location /stat.xsl {
        	root /disk1/guoyiman/rtmp/nginx-rtmp-module/;
    	}
    }



}
rtmp {
    server {
        # 监听端口
        listen 1996;
        chunk_size 2048;

        # HLS
        # For HLS to work please create a directory in tmpfs (/tmp/hls here)
        # for the fragments. The directory contents is served via HTTP (see
        # http{} section in config)
        #
        # Incoming stream must be in H264/AAC. For iPhones use baseline H264
        # profile (see ffmpeg example).
        # This example creates RTMP stream from movie ready for HLS:
        #
        # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
        #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
        #    -f flv rtmp://localhost:1935/hls/movie
        #
        # If you need to transcode live stream use 'exec' feature.
        #

        # rtmp推流请求路径

	application hls {
            live on;
            hls on;
            hls_path /data/hls;
	}
	application live{
	   live on;
	}
	application vod {
           play /disk1/guoyiman/rtmp/test; #//视频文件存放位置。
       }
    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx是一个高性能的开源Web服务器,同时也可以用作反向代理服务器、负载均衡器和媒体服务器。NginxRtmp-Module是一个第三方模块,它为Nginx服务器添加了RTMP(Real-Time Messaging Protocol)支持,使其可以充当一个媒体服务器。FFmpeg是一个跨平台的音视频处理工具,它可以用来进行多媒体文件的转码、编解码、录制和媒体传输等操作。 引用中提到了使用Nginx Nginx-Rtmp-Module Ffmpeg搭建媒体服务器的方法。具体配置可以查看conf/nginx.conf文件。首先需要解压文件,然后运行nginx_start.bat文件来启动Nginx服务器。这样就可以使用Nginx服务器提供的RTMP服务,通过FFmpeg推送一个RTMPNginx,然后客户端可以通过访问Nginx来收看实时视频。还可以使用HLS(HTTP Live Streaming)协议,通过HTTP协议来访问Nginx服务器并收看视频,但是FFmpeg推送仍然是RTMP的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [nginx+nginx-rtmp-module+ffmpeg搭建媒体服务器服务器](https://blog.csdn.net/sinat_16643223/article/details/115108327)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ffmpeg+nginx+nginx-rtmp-module搭建个人直播平台](https://download.csdn.net/download/qq_29114677/13012815)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值