Nginx的编译
- nginx 源码包下载:http://nginx.org/en/download.html
- PCRE 下载:http://www.pcre.org (rewrite 模块依赖)
- zlib 库下载:http://zlib.net (gzip 模块依赖,不过Linux如果已经安装会使用系统安装的gzip)
- openssl 库:https://github.com/openssl/openssl (需要https支持的话需要编译openssl)
- Nginx-rtmp-module:https://github.com/arut/nginx-rtmp-module.git (支持hls 的模块)
prebuild
这里添加了https的支持,需要实现编译安装openssl。
git clone https://github.com/openssl/openssl.git
cd path/openssl
./config
make
sudo make install
build nginx
编译选项官方参考:http://nginx.org/en/docs/configure.html
./configure \
--prefix=/usr/local/nginx/ \
--with-debug \
--with-file-aio \
--with-threads \
--with-cc-opt="-O3" \
--with-http_v2_module \
--with-http_realip_module \--with-http_gzip_static_module \
--with-http_ssl_module \
--with-openssl=../openssl/ \
--with-http_stub_status_module \
--with-http_xslt_module \
--with-pcre=../pcre-8.43 \
--add-module=../nginx-rtmp-module
说明:
openssl的目录是编译的目录,而且是已经编译完了的目录,编译Nginx的时候会去找需要的库文件。另外pcre 不要下载pcre2 的包,Nginx目前不支持,但是未来可能会支持,具体视Nginx的更新而定。
配置Nginx
修改文件位置:/usr/local/nginx/conf/nginx.conf
官方参考: https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls
在文件中http server配置之前或者之后添加 rtmp 服务:
rtmp {
server {
listen 1946;
chunk_size 4000;
#HLS
# For HLS to work please create a directory in tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1946/hls/movie
#
# If you need to transcode live stream use 'exec' feature.
#
application hls {
live on;
hls on;
hls_path /home/test/Videos/hls; #具体存放hls 相关文件的路径
hls_fragment 6s;
hls_cleanup off; #不要每次播放就清除原先的文件
}
}
}
然后需要在http的server中添加hls的服务接口:
http {
....
server {
listen 8081;
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home/test/Videos; #比上述rtmp服务中的路径少了hls,因为播放的url中会跟有hls,文件寻找路径会追加hls
expires -1;
add_header Access-Control-Allow-Origin *;
add_header Cache-Control no-cache;
}
}
....
}
端口号如果已经被占用,可以先查看Nginx是否已经启动,并且占用了对应的端口,如果没有可以自行修改端口号。
端口查询:
端口查询:
netstat -nlp
另外可以参见 nginx 服务启动停止的参考命令
播放hls link
- 如果有已经准备好的 playlist 文件(即m3u8文件) ,就放到上述配置的路径中:/home/test/Videos/hls
放置对应的ts 文件,或者其他音频文件,字幕文件。例如如下的目录结构:
-/home/test/Videos/hls/
-master.m3u8
-a1
---prog_index.m3u8
---fileSequence0.aac
---fileSequence1.aac
-v3
---prog_index.m3u8
---fileSequence0.ts
---fileSequence1.ts
-v7
---prog_index.m3u8
---fileSequence0.ts
---fileSequence1.ts
具体文件没有放全,可以看下具体的master playlist(master.m3u8):
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=4775338,BANDWIDTH=5054232,CODECS="avc1.64002a,mp4a.40.2",RESOLUTION=1920x1080,FRAME-RATE=60.000,AUDIO="aud1"
v7/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=914722,BANDWIDTH=922242,CODECS="avc1.64001e,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=30.000,AUDIO="aud1"
v3/prog_index.m3u8
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud1",LANGUAGE="en",NAME="English",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="a1/prog_index.m3u8"
每个子目录下会放置对应的media playlist(prog_index.m3u8)。
播放地址:http://IP_ADDR:8081/hls/master.m3u8
- 如果没有已经准备好的hls 文件,可以利用FFmpeg 将mp4 文件切成对应的hls 文件:
ffmpeg -re -i /Users/richyleo/Downloads/warcraft.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1946/hls/test
播放地址: http://IP_ADDR:8081/hls/test.m3u8
参考
https://www.jianshu.com/p/31c195fd50a4
https://blog.csdn.net/tao_627/article/details/22271559