nginx+nginx-http-flv-module(推荐主流)
FLV文件 就是由“ FLV Header + RTMP数据 ”构成的。
FLV与 RTMP之间的关 系秘密,即 FLV 是在 RTMP 数据之上加了一层“⻢甲”
测试网页http://bilibili.github.io/flv.js/demo/
模块地址
通过服务端将其 RTMP 流实时转为 http-flv 流,从而浏览器可直接使用该流进行直播(使用bilibili提供的 flv.js )。
所需资料集(以及下载地址):
linux实现
linux实现步骤
- 下载需要的压缩包并解压
- 在nginx的文件夹下配置环境并编译
- 在编译后的nginx目录下修改conf/nginx.conf的配置
- 在编译后的nginx目录下执行sbin/nginx启动服务器
- 推流
- 测试
提前准备依赖
yum -y install unzip yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl-devel
不准备就是等报错了根据提示去安装所需的
nginx+flv编译
这里是nginx+flv
nginx和flv的版本号要贯穿整个配置,如果修改了版本,记得文件夹和其他配置的地方也要同步
cd /opt mkdir nginx cd nginx git clone https://github.com/winshining/nginx-http-flv-module.git wget http://nginx.org/download/nginx-1.24.0.tar.gz [root@iZbp1c400avts4bhhmibppZ nginx]# ls nginx-1.24.0.tar.gz nginx-http-flv-module
解压
tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0 # 其他环境根据自己的需要去添加 ./configure --prefix=/usr/local/nginx --add-module=../nginx-http-flv-module
下面的configuration就是配置路径
编译
make && make install
第一次启动
cd /usr/local/nginx sbin/nginx
访问ip:80端口来判断是否成功,因为nginx,service可能会有未卸载干净的,出现此界面表示nginx成功启动
修改nginx.conf文件
修改编译后的nginx文件夹的conf下的nginx.conf文件
cd /usr/local/nginx vim conf/nginx.conf
新增两部分
rtmp { server { listen 1935; application myapp { live on; } } }
location /live { flv_live on; } location /flv { add_header 'Access-Control-Allow-Origin' '*'; flv_live on; chunked_transfer_encoding on; }
为了解决该跨域问题
添加add_header 'Access-Control-Allow-Origin' '*';
location / { add_header 'Access-Control-Allow-Origin' '*'; root html; index index.html index.htm; }
完整配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } rtmp { server { listen 1935; application myapp { live on; } } } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { add_header 'Access-Control-Allow-Origin' '*'; root html; index index.html index.htm; } location /live { flv_live on; } location /flv { add_header 'Access-Control-Allow-Origin' '*'; flv_live on; chunked_transfer_encoding on; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
重启服务器
sbin/nginx -s reload
推流
播放本地视频
ffmpeg -re -i ".\test.mp4" -c copy -f flv "rtmp://ip:1935/myapp/test"
播放测试flv
http://ip/flv?port=1935&app=myapp&stream=test
这里的参数是和nginx.conf相对应,test是推流时选择的steam流标识
播放测试rtmp
rtmp://ip:1935/myapp/test