In the previous article on the Mac to build a live server nginx+rtmp, we have built a nginx+rtmp live server. The following need to add support to the Nginx server for HLS. In Nginx
addition to the support of the HLS
more simple, just simple modification under the configuration file nginx.conf
.
Installing Nginx and RTMP modules
For the installation of Nginx is relatively simple, please refer to the previous article on the Mac to build a live server nginx+rtmp. Here are some tips:
(1) After installing Nginx, open the configuration file:
/usr/local/nginx/conf/nginx.conf
(2) command to start Nginx:
$ sudo/usr/local/nginx/sbin/nginx-/usr/local/nginx/sbin/nginx
HLS Live stream Configurationfind Http-->server, add in curly braces
Server {Listen8080; server_name localhost; #charset Koi8-R; #access_log logs/Host.access.log Main; Location/{root HTML; Index index.html index.htm; } #HLS配置开始, this configuration is for the ' client ' to be able to get HLS's pull location with the HTTP protocol/HLS {# Serve HLS fragments types {application/Vnd.apple.mpegurl m3u8; Video/mp2t ts; } root HTML; Add_header Cache-control no-Cache; } #HLS配置结束 #error_page404/404. html; # REDIRECT Server error pages to theStaticPage/50x.html # Error_page - 502 503 504/50x.html; Location= /50x.html {root html; } }
Found it
rtmp
Under the
server
Add in curly Braces
#在http节点下面 (i.e. the end of the file) plus rtmp configuration: rtmp { server { 1935; Application Zbcs { live on; Record off; } #增加对HLS支持开始 application HLs { live on; HLS on; /usr/local/var/www/HLS; Hls_fragment 5s; } #增加对HLS支持结束 }}
Description
- Live on; Turn on real-time
- HLS on; Open HLS
- Hls_path; TS File Storage Path
- Hls_fragment 5s; Each TS file contains 5 Seconds of video content
HLS Live delay
We know that the HLS protocol is to divide the live stream into a segment of the video to download playback, so assume that the list contains 5 TS files, each TS file contains 5 Seconds of video content, then the overall delay is 25 seconds. Because when you see these videos, the host has recorded the video and uploaded it, so the delay occurs. Of course, you can shorten the length of the list and the size of a single TS file to reduce latency, the extreme can be reduced to a list length of 1, and ts length of 1s, but this will increase the number of requests, increase the server pressure, when the slow speed back to cause more buffering, so the official recommended TS length of 10s, So it's going to be a big 30s delay. Resources:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/ Frequentlyaskedquestions/frequentlyaskedquestions.html
Save the configuration file, reload the Nginx configuration
Nginx-s Reload
to push a stream
FFmpeg the same as in the previous article, however, we need to push into the new configuration of HLS, the movie keyword can be any replacement
Ffmpeg-re-i/users/jiangys/documents/document/demo.mp4-vcodec copy-f flv rtmp://localhost:1935/hls/ Movie
Then, we can be in this directory (this is also nginx under the HTML default configuration file)
/usr/local/var/www/hls
When you see the generated ts
files, you also generate a " 你的m3u8的文件名称.m3u8“
file
Test Pull Stream
With the above configuration, we can see the streamed stream in both rtmp and HLS two modes. Note that if you use HTTP, you are listening on port 8080, which is written in the config file.
(1) Using rtmp: (use VLC to verify playback)
RTMP://192.168.1.100/hls/movie
(2) Play with HLS
http://192.168.1.100:8080/hls/movie.m3u8
For HLS pull stream, we can also enter the above address to play directly in the Safari browser , in addition to the use of VLC playback. You can also use the ipad or Safari on your iphone to access (the IP address of the computer where you need to change localhost to nginx)
Add
1. In HLS, we want to store the TS file generated by the push stream in the specified directory, such as "/tmp/hls"
application HLs { live on; HLS on; /tmp/HLS; }
Then, we also need to change the root path in Http-->server to:/tmp. Otherwise, the stream will not be pulled.
Root HTML refers to the location of the current Nginx server root directory, pointing to the /usr/local/var/www directory
Reference article:
Use Nginx and rtmp plugins to build video live and on-demand servers
Mac Live Server Nginx configuration support for HLS