环境:
windows
后面出linux的
一、nginx的安装和配置
首先我们下载nginx。在nginx官网上下载的nginx是不带rtmp模块的,所以我们在http://nginx-win.ecsds.eu/download/中下载nginx 1.7.11.3 Gryphon.zip,如下图所示。该版本的nginx包含rtmp组件,通过rtmp组件,才能提供流媒体服务,使nginx成为rtmp流媒体服务器。
下载后我们解压nginx 1.7.11.3 Gryphon.zip,得到nginx 1.7.11.3 Gryphon文件夹。使用命令提示符进入解压后的路径下,输入命令
nginx.exe -v智能体编程bash
上述命令的作用是查看nginx版本,输入上述命令后出现下图所示信息表示nginx安装成功
我们在nginx 1.7.11.3 Gryphon目录下新建三个目录:m3u8File、rec、vod,如下图所示:
然后我们进入nginx 1.7.11.3 Gryphon文件夹的conf目录下,新建一个文件“nginx.conf”,如下图所示:
打开nginx.conf,输入如下信息后,保存。
worker_processes1;#Nginx进程数,建议设置为等于CPU总核数events{worker_connections1024;#工作模式与连接数上限}rtmp_auto_push on;#RTMP服务rtmp{server{listen1935;#服务端口chunk_size4096;#数据传输块的大小application vod{play ./vod;#视频文件存放位置}application live{live on;#开启直播hls on;#开启hls直播。这个参数把直播服务器改造成实时回放服务器#wait_key on; #对视频切片进行保护,这样就不会产生马赛克了hls_path ./m3u8File;#切片视频文件存放位置(HLS,m3u8文件存放位置)hls_fragment 2s;#每个视频切片的时长hls_playlist_length 16s;recorder myRecord{record all manual;record_suffix _.flv;record_path ./rec;}#hls_continuous on; #连续模式#hls_cleanup on; #对多余的切片进行删除#hls_nested on; #嵌套模式}}}#HTTP服务http{include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout65;server{listen80;server_name localhost;location /{root html;index index.html index.htm;}location /live_hls{types{#m3u8 type设置application/vnd.apple.mpegurl m3u8;#ts分片文件设置video/mp2t ts;}#指向访问m3u8文件目录alias./m3u8File;add_header Cache-Control no-cache;#禁止缓存}location /control{rtmp_control all;}location /stat{rtmp_stat all;rtmp_stat_stylesheet stat.xsl;}location /stat.xsl{root ./nginx-rtmp-module-master;}# redirect server error pages to the static page /50x.html#error_page500502503504/50x.html;location=/50x.html{root html;}}}当通过上面的步骤验证了nginx配置文件的正确性后,我们就可以启动nginx了。通过命令提示符在nginx.exe所在的目录下输入命令:
start nginx智能体编程bash
上述命令的作用是载入缺省./conf/nginx.conf配置文件,启动nginx。输入完成后,在浏览器中输入nginx所在电脑的ip(该ip地址可以通过ipconfig命令查看,或者直接输入127.0.0.1,localhost,也是可以的),得到下图所示页面,则表示nginx启动成功了。然后用户可以通过命令nginx.exe -s stop或者nginx.exe -s quit停止nginx。
推流测试:
1.本地视频文件推流
ffmpeg -i video3.mp4 -f flv rtmp://127.0.0.1/live/test1智能体编程bash
其中-i 表示输入流。这里的输入流是video3.mp4。-f 表示设定的输出格式。这里因为推流为rtmp流得使用flv格式,所以设成-f flv。因为是本机测试,所以推流的IP地址填127.0.0.1(也可以填用ipconfig命令查看到的nginx所在电脑的ip),推流地址为rtmp://127.0.0.1/live/test1。关于"live"目录,详情可见《 关于使用FFmpeg推流时,live目录的理解》。上述语句的意思是通过ffmpeg将媒体文件video3.mp4转换成rtmp流,推流到本机的nginx服务器中。
拉流测试
拉流指的是用户端从服务器拉取语音视频流到客户端播放。下面我们会通过vlc拉流nginx中的语音视频流,然后在vlc播放。执行完上述推流的步骤后,我们在windows中打开vlc,点击“打开网络串流”,如下图所示:
输入网络URL,如下图所示(因为是本机测试,所以IP地址填127.0.0.1,也可以填用ipconfig命令查看到的nginx所在电脑的ip))
可以看到在vlc中出现视频画面了,表示拉流成功了
2.实时推流
ffmpeg -f gdigrab -t 30 -framerate 15 -i desktop -f dshow -i audio="virtual-audio-capturer"-f flv rtmp://127.0.0.1/live/test智能体编程bash
录屏电脑桌面并实时推流