手把手教你從安裝CentOS7.4鏡像開始,搭建IoT視頻監控系統( 二 )

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

推薦閱讀