rtmp直播代码示例
#docker-compose.yml
services:
openresty:
image: openresty:test
container_name: openresty-rtmp
restart: always
volumes:
- ./openresty/html:/var/www/html
- ./openresty/conf.d:/etc/nginx/conf.d
environment:
- TZ=Asia/Shanghai
ports:
- "8080:80"
- "1935:1935"
networks:
- myweb
networks:
myweb:
name: openresty-rtmp-web
driver: bridge
#index.conf
lua_shared_dict geoip_db 10m; # 为了缓存数据库,可以设定缓存共享字典
init_by_lua_block {
local geoip = require "geoip.mmdb"
mmdb = assert(geoip.load_database("/var/www/html/GeoLite2-Country.mmdb"))
}
server {
listen 80;
location /test {
default_type text/html;
content_by_lua_block {
local ip = ngx.var.remote_addr
local result = mmdb:lookup(ip)
if (result.country.iso_code ~= "CN")
then
ngx.status = ngx.HTTP_NOT_FOUND
ngx.say("IP is not allow")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
ngx.say("<p>hello, world</p>")
}
}
location /mp4/ {
alias /var/www/html/mp4/;
expires max;
add_header Cache-Control public;
autoindex off;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/html/xsl/;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /var/www/html/hls;
expires -1;
add_header Cache-Control no-cache;
}
}
#rtmp.main
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /var/www/html/mp4;
}
application live {
live on;
#exec_push ffmpeg -y -i rtmp://127.0.0.1:1935/live/$name -vf fps=1/60 -update 1 /var/www/html/images/$name.jpg;
on_publish http://api.runtucaipiao.cn/api/live-publish;
on_play http://api.runtucaipiao.cn/api/live-play;
on_done http://api.runtucaipiao.cn/api/live-done;
#record keyframes;
#record_path /var/www/html/live;
#record_max_frames 10;
#record_interval 2m;
#on_record_done http://api.runtucaipiao.cn/api/live-record-done;
#record all;
#record_path /var/www/html/live;
#record_max_size 1024k;
#record_unique on;
#hls on;
#wait_key on;
#hls_path /var/www/html/hls;
#hls_fragment 10s;
#hls_playlist_length 60s;
#hls_continuous on;
#hls_cleanup on;
#hls_nested on;
}
}
}