# nginx-rtmp-http-flv **Repository Path**: LateWinter/nginx-rtmp-http-flv ## Basic Information - **Project Name**: nginx-rtmp-http-flv - **Description**: 结合使用ffmpeg+rtmp插件进行rtsp流转换,使用nginx+http-flv插件,可以用http协议播放flv格式实时流视频 - **Primary Language**: Unknown - **License**: BSD-2-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 2 - **Created**: 2021-04-01 - **Last Updated**: 2025-03-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # nginx-rtmp-http-flv #### 介绍 结合使用ffmpeg+rtmp插件进行rtsp流转换,使用nginx+http-flv插件,可以用http协议播放flv格式实时流视频 #### 安装教程 1. 开启nginx的rtmp功能 ``` rtmp { server { listen 1935; application live { live on; gop_cache on ; #打开GOP缓存,减少首屏等待时间 ### 保存视频/start record video; #记录直播视频 } application hls { live on; hls on; hls_path temp/hls; hls_fragment 8s; } } } ``` 2. 使用ffmpeg命令,推流到nginx的rtmp的流服务器,本例是把rtsp流转换成rtmp流的命令 * 推流到nginx的rtmp的流服务器 ` ffmpeg -re -rtsp_transport tcp -i rtsp://admin:abc12345@ip -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://rtmpip:1935/live/video ` 3. 配置nginx http-flv插件,是实时视频能http协议播放 * 开启http-flv功能 ``` 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; server { listen 9092; location / { root html; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { 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头 expires -1; } location /hls { # 指定允许跨域的方法,*代表所有 add_header Access-Control-Allow-Methods *; # 预检命令的缓存,如果不缓存每次会发送两次请求 add_header Access-Control-Max-Age 3600; # 带cookie请求需要加上这个字段,并设置为true add_header Access-Control-Allow-Credentials true; # 表示允许这个域跨域调用(客户端发送请求的域名和端口) # $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号 add_header Access-Control-Allow-Origin $http_origin; # 表示请求头的字段 动态获取 add_header Access-Control-Allow-Headers $http_access_control_request_headers; # OPTIONS预检命令,预检命令通过时才发送请求 # 检查请求的类型是不是预检命令 if ($request_method = OPTIONS){ return 200; } #server hls fragments types{ application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias temp/hls; expires -1; } } } ``` * 使用flv.js插件播放,播放地址为 `http://localip:9092/live?port=1935&app=live&stream=video` port为rtmp的端口 app为rtmp配置的live stream为rtmp地址流路径, rtmp://rtmpip:1935/live/**video** #### nginx-http-flv,windows编译教程 1. [链接]( https://github.com/aushy/nginx-http-flv-win64) https://github.com/aushy/nginx-http-flv-win64 #### nginx-http-flv,linux编译教程 1. [链接]( https://github.com/winshining/nginx-http-flv-module.git) https://github.com/winshining/nginx-http-flv-module.git 2. https://www.freesion.com/article/51881008653/