HLS协议编码格式要求:
视频的编码格式:H264音频的编码格式:AAC、MP3、AC-3视频的封装格式:ts保存 ts 索引的 m3u8 文件配置/usr/local/nginx/conf/nginx.conf将RTMP流转为HLS流 。
在http模块的server配置里增加新的配置:
location /live_hls{types {#m3u8 type设置application/vnd.apple.mpegurl m3u8;#ts分片文件设置video/mp2t ts;}#指向访问m3u8文件目录alias ./m3u8File; #和rtmp模块里的hls_path设置路径一样add_header Cache-Control no-cache; #禁止缓存}在rtmp模块的server配置里增加新的配置:
hls on;#开启hlshls_path ./m3u8File;#hls的ts切片存放路径 (这是个目录,会自动创建的)hls_fragment 2s; #本地切片长度hls_playlist_length 6s;#HLS播放列表长度/usr/local/nginx/conf/nginx.conf文件的完整的配置如下:
worker_processes1;#Nginx进程数,建议设置为等于CPU总核数events { worker_connections1024;#工作模式与连接数上限}rtmp_auto_push on;#RTMP服务rtmp {server {listen 8888;application live {live on;#开启实时record all; record_unique on; record_path "./video";#视频缓存的路径 record_suffix -%Y-%m-%d-%H_%M_%S.flv; hls on;#开启hls hls_path ./m3u8File;#hls的ts切片存放路径 hls_fragment 2s; #本地切片长度 hls_playlist_length 6s;#HLS播放列表长度 }}}#HTTP服务http {includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65;server {listen8099; server_namelocalhost;location / {roothtml; indexindex.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_page500 502 503 504/50x.html;location = /50x.html {roothtml;}}}配置之后重启服务器即可 。
按照前面的配置 , RTMP推流地址和HTTP访问地址如下:
RTMP推流和拉流地址: rtmp://127.0.0.1:8888/live/video01那么对应的HTTP的访问地址:http://127.0.0.1:8099/live_hls/video01.m3u8说明: 转为HLS流之后 , 如果浏览器支持HLS流就可以直接输入地址播放 。一般手机浏览器都支持的 。比如:苹果手机的自带浏览器 , QQ浏览器等浏览器都支持直接播放HLS流 。PC机的谷歌浏览器默认是不支持的 。
2.11 NGINX配置HTTP文件服务器在5.8小节里介绍了如何配置NGINX保留RTMP推流的视频文件 , 如果想做一个直播回放 , 历史记录查看的播放器,那么就可以将rtmp视频缓存的目录作为HTTP文件服务器访问的根目录,通过访问这个根目录获取目录下文件的索引,得到视频文件的访问地址就可以直接进行播放,就能做一个视频回放播放器 。
在http模块里新增加一个server配置,并填入新的配置,详细内容如下:
server { listen8090; server_namelocalhost; location / {root ./video;#指定哪个目录作为Http文件服务器的根目录,如果你这里写了file就是你的根目录,那么访问的时候file就不会出现在目录中autoindex on;#设置允许列出整个目录autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes 。改为off后,显示出文件的大概大小 , 单位是kB或者MB或者GBautoindex_localtime on; #默认为off,显示的文件时间为GMT时间 。改为on后,显示的文件时间为文件的服务器时间charset utf-8; #防止文件乱码显示, 如果用utf-8还是乱码,就改成gbk试试}}特别说明: nginx是支持配置多个server配置,监听不同的端口 , 可以给文件服务器单独设置一个监听端口,专门作为文件遍历使用 。
/usr/local/nginx/conf/nginx.conf文件的完整的配置如下:
worker_processes1;#Nginx进程数,建议设置为等于CPU总核数events { worker_connections1024;#工作模式与连接数上限}rtmp_auto_push on;#RTMP服务rtmp {server {listen 8888;application live {live on;#开启实时record all; record_unique on; record_path "./video";#视频缓存的路径 record_suffix -%Y-%m-%d-%H_%M_%S.flv; hls on;#开启hls hls_path ./m3u8File;#hls的ts切片存放路径 hls_fragment 2s; #本地切片长度 hls_playlist_length 6s;#HLS播放列表长度 }}}#HTTP服务http {includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65;server { listen8090; server_namelocalhost; location / {root ./video;#指定哪个目录作为Http文件服务器的根目录,如果你这里写了file就是你的根目录,那么访问的时候file就不会出现在目录中autoindex on;#设置允许列出整个目录autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes 。改为off后,显示出文件的大概大小,单位是kB或者MB或者GBautoindex_localtime on; #默认为off , 显示的文件时间为GMT时间 。改为on后,显示的文件时间为文件的服务器时间charset utf-8; #防止文件乱码显示, 如果用utf-8还是乱码,就改成gbk试试}}server {listen8099; server_namelocalhost;location / {roothtml; indexindex.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_page500 502 503 504/50x.html;location = /50x.html {roothtml;}}}
推荐阅读
- 从源码入手探究一个因useImperativeHandle引起的Bug
- 如何把螃蟹杀掉(如何将螃蟹从洞里赶出来)
- C# RulesEngine 规则引擎:从入门到看懵
- C++算法之旅、02 从木棒切割问题领悟二分法精髓
- 教你如何解决T+0的问题
- 3 Python全栈工程师之从网页搭建入门到Flask全栈项目实战 - 入门Flask微框架
- 机器学习实战-AdaBoost
- 从0搭建vue3组件库:自动化发布、管理版本号、生成 changelog、tag
- 从0搭建vue3组件库: 如何完整搭建一个前端脚手架?
- WiFi万能钥匙怎么用(教你一招wifi永不断网)