相关资料:
【Docker初探】第一节:Docker+Alpine+Openssh+Supervisor部署Linux基础环境
【Docker初探】第二节:安装alpine-pkg-glibc,部署java环境
【Docker初探】第四节:使用mount挂载共享文件夹(cifs)
【Docker初探】第五节:安装PostgreSQL+Nginx
【Docker初探】第六节:Docker Swarm集群,及Windows下部署
安装编译环境:
apk add build-base
apk add pcre pcre-dev
apk add openssl openssl-dev
apk add zlib zlib-dev
apk add git
apk add wget
官网下载源码并编译:
http://nginx.org/en/download.html
https://github.com/winshining/nginx-http-flv-module
# 下载nginx源码
wget http://nginx.org/download/nginx-1.17.3.tar.gz
# 下载nginx-http-flv-module
git clone https://github.com/winshining/nginx-http-flv-module.git
# 解压
tar -zxvf nginx-1.17.3.tar.gz
# 进入nginx目录
cd nginx-1.17.3
# 编译配置
./configure --add-module=/root/nginx-http-flv-module
make
make install
nginx会安装到 /usr/local/nginx 目录下。
加入全局变量,修改"/etc/profile"文件:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/nginx/sbin
执行如下命令,使全局变量生效:
source /etc/profile
修改nginx配置文件/usr/local/nginx/conf/nginx.conf,内容如下:
#user nobody;
user root;
worker_processes 1;
error_log /root/www/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
location / {
root /root/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /live {
flv_live on; #打开HTTP播放FLV直播流功能
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回复
add_header 'Access-Control-Allow-Origin' '*'; #添加额外的HTTP头
add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#push和pull状态的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /root/www/rtmp; #指定stat.xsl的位置
}
#如果需要JSON风格的stat, 不用指定stat.xsl
#但是需要指定一个新的配置项rtmp_stat_format
#location /stat {
# rtmp_stat all;
# rtmp_stat_format json;
#}
location /control {
rtmp_control all; #rtmp控制模块的配置
}
}
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #log模块在access.log中记录日志的间隔时间,对调试非常有用
log_size 1m; #log模块用来记录日志的缓冲区大小
server {
listen 1935;
application myapp {
live on;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
建立/root/www/logs目录,用于存放视频流文件:
mkdir /root/www
mkdir /root/www/logs
启动nginx服务:
# 停止服务
nginx -s stop
# 启动服务
nginx
使用ffmepg工具直播推流(这里只推视频流,无声音,具体参数请看ffmepg文档):
./ffmpeg -f dshow -i video="HD WebCam" -r 25 -vcodec libx264 -acodec copy -preset:v ultrafast -tune:v ze
rolatency -f flv "rtmp://192.168.1.100:1935/myapp/test"
使用OBS工具直播推流:
使用flv.js播放:
Demo地址:http://bilibili.github.io/flv.js/demo/
GitHub源码:https://github.com/bilibili/flv.js
视频直播服务到此搭建完成!