Windows下面搭建基于rtmp的服务器硬件环境通过使用nginx搭建一个简单的服务器,使用ffmpeg推送rtmp实时流,利用video标签实现可以在web端和手机端的浏览器中进行直播。
将解压后的目录名:nginx 1.7.11.3 Gryphon改成:nginx-1.7.11.3-Gryphon
保证stat.xls的目录为:nginx-1.7.11.3-Gryphon\nginx-rtmp-module\stat.xsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | rtmp { server { listen 1935; chunk_size 4000; application hls{ live on ; #启用rtmp直播 #地址为rtmp://[server]:[rtmp_port]/[app]/[stream] hls on ; #启用hls直播 #地址为地址为http://[server]:[http_port]/[app]/[stream].m3u8 hls_path html/hls; #此处hls需手动在html文件夹下面创建,否则程序会报错 hls_fragment 5s; recorder rec { #启用录制 record all manual; #手动控制录制启停 record_suffix _rec.flv; record_path nginx-rtmp-module/tmp/rec/; #录制保存地址 record_unique on; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server { listen 80; # server_name localhost; location /stat { # rtmp_stat all; # rtmp_stat_stylesheet stat.xls; #} #location /stat.xls { # root nginx-rtmp-module/; #} #location /control { # rtmp_control all; #} location /hls { # Serve HLS fragments types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root html; expires -1; } |
1 | nginx.exe -c conf\nginx-win-.conf |
提供rtmp直播源
1 | ffmpeg -re -analyzeduration 8000 -probesize 200000 -i "rtmp://live.hkstv.hk.lxdns.com/live/hks" -strict -2 -bsf:a aac_adtstoasc -c copy -flvflags aac_seq_header_detect -f flv rtmp://localhost/hls/mystream |
在网页中展示视屏[size=1em]注意1、这里提供rtmp源的机器不一定和nginx在同一台物理主机上,可以是网络上的另一台机器,只要保证它能与nginx所在的主机建立tcp链接即可。(也就是nginx主机需要开启rtmp服务的监听端口,这里是1935,当然你也可以修改为其他的端口。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <link rel="stylesheet" href="http://vjs.zencdn.net/5.10/video-js.css"> </head> <video id=example-video width=960 height=540 class="video-js vjs-default-skin" controls autoplay=true> <source src="hls/mystream.m3u8" type="application/x-mpegURL"> </video> <script src="http://vjs.zencdn.net/5.10/video.js"></script> <script src="https://cdn.bootcss.com/videojs-contrib-hls/5.14.1/videojs-contrib-hls.min.js"></script> </script> <script> var player = videojs('example-video'); player.play(); </script> </html> |
如果只是用video标签是无法播放.m3u8的视屏文件的,需要引入videojs-contrib-hls.js。videojs-contrib-hls支持一堆HLS功能,详情参考[
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |