├── .gitattributes ├── CN-README.md ├── LICENSE.txt ├── README.md ├── assets └── images │ └── jetbrains.svg └── nginx.conf /.gitattributes: -------------------------------------------------------------------------------- 1 | *.* linguist-language=Shell 2 | -------------------------------------------------------------------------------- /CN-README.md: -------------------------------------------------------------------------------- 1 | ![ar414-nginx-rtmp-ffmpeg](https://cdn.ar414.com/ar414-nginx-rtmp-ffmpeg.png) 2 | ### :two_men_holding_hands:前言 3 | > 最近帮朋友的公司部署了一套分流+水印的直播系统 4 | > 5 | > 顺手打包成docker镜像,方便大家需要用到的时候开箱即用,不需要百度一些零碎的文章 6 | > 也可做简单的直播服务,只需调整配置文件便可达到你的需求. 7 | > 8 | > 需求:将直播流分流到两个云厂商的直播云,一个有水印,一个无水印。使用hls播放 9 | > 10 | > 朋友需求的拓扑示意图: 11 | > 12 | > ![ar414-nginx-rtmp](https://cdn.ar414.com/ar414-nginx-rtmp.png) 13 | > 14 | > 当前拓扑示意图(阿里云和腾讯云不方便放出推流和拉流地址,有兴趣的同学可以去申请玩一下) 15 | > 16 | > ![ar414-nginx-service](https://cdn.ar414.com/nginx-live-service.png?a=1) 17 | 18 | # :whale:[docker-nginx-rtmp-ffmpeg](https://hub.docker.com/repository/docker/ar414/nginx-rtmp-ffmpeg) 19 | 基于[docker-nginx-rtmp](https://github.com/alfg/docker-nginx-rtmp)进行配置部署,这篇文章的意义是实现直播分流及直播画面水印. 20 | * Nginx 1.16.1(从源代码编译的稳定版本) 21 | * nginx-rtmp-module 1.2.1(从源代码编译) 22 | * ffmpeg 4.2.1(从源代码编译) 23 | * 已配置好的[nginx.conf](https://github.com/ar414-com/nginx-rtmp-ffmpeg-conf/blob/master/nginx.conf) 24 | * 只支持1920*1080(如需支持其他分辨率可参考[nginx.conf](https://github.com/alfg/docker-nginx-rtmp/blob/master/nginx.conf)) 25 | * 实现两路分流 26 | * 本机 27 | * 直播云(例:阿里云、腾讯云、ucloud) 28 | * 实现直播水印效果 29 | * 水印图片存放位置(容器内):/opt/images/logo.png 30 | 31 | ## :computer:部署运行 32 | ### 服务器 33 | * 安装docker(Centos7,其他系统请发挥你的搜索功能) 34 | ```bash 35 | $ yum -y install docker #安装docker 36 | $ systemctl enable docker #配置开机启动 37 | $ systemctl start docker #启动docker服务 38 | ``` 39 | * 拉取docker镜像并运行 40 | ```bash 41 | #如果速度慢可使用阿里云:docker pull registry.cn-shenzhen.aliyuncs.com/ar414/nginx-rtmp-ffmpeg:v1 42 | $ docker pull ar414/nginx-rtmp-ffmpeg 43 | $ docker run -it -d -p 1935:1935 -p 8080:80 --rm ar414/nginx-rtmp-ffmpeg 44 | ``` 45 | * 推流地址(Stream live content to): 46 | ``` 47 | rtmp://:1935/stream/$STREAM_NAME 48 | ``` 49 | * SSL证书 50 | 51 | 将证书复制到容器内,并在容器内修改nginx.conf配置文件,然后重新commit(操作容器内的文件都需要重新commit才会生效) 52 | ``` 53 | #/etc/nginx/nginx.conf 54 | listen 443 ssl; 55 | ssl_certificate /opt/certs/example.com.crt; 56 | ssl_certificate_key /opt/certs/example.com.key; 57 | ``` 58 | 59 | ### OBS配置 60 | * Stream Type: `Custom Streaming Server` 61 | * URL: `rtmp://:1935/stream` 62 | * Stream Key:ar414 63 | ![obs-config](https://cdn.ar414.com/obs-config.png) 64 | 65 | ### 观看测试 66 | > HLS播放测试工具:http://player.alicdn.com/aliplayer/setting/setting.html (如果配置了证书则使用https) 67 | 68 | * HLS播放地址 69 | * 有水印:http://\:8080/hls/ar414_wm.m3u8 70 | ![ar414-hls-wm](https://cdn.ar414.com/ar414-hls-wm.png) 71 | * 无水印:http://\:8080/hls/ar414.m3u8 72 | ![ar414-hls](https://cdn.ar414.com/ar414-hls.png) 73 | 74 | > RTMP测试工具:[PotPlayer](https://daumpotplayer.com/download/) 75 | 76 | * RTMP播放地址 77 | * 无水印:rtmp://\:1935/stream/ar414 78 | ![ar414-rtmp](https://cdn.ar414.com/ar414-rtmp.png) 79 | * 有水印:需要分流到其他服务器上 80 | 81 | ## :page_facing_up:配置文件简解(分流、水印及水印位置) 82 | > [完整配置文件](https://github.com/ar414-com/nginx-rtmp-ffmpeg-conf/blob/master/nginx.conf) 83 | 84 | * RTMP配置 85 | ```bash 86 | rtmp { 87 | server { 88 | listen 1935; #端口 89 | chunk_size 4000; 90 | #RTMP 直播流配置 91 | application stream { 92 | live on; 93 | #添加水印及分流,这次方便测试直接分流到当前服务器hls 94 | #实际生产一般都分流到直播云(腾讯云、阿里云、ucloud) 95 | #只需把需要分流的地址替换即可 96 | #有水印:rtmp://localhost:1935/hls/$name_wm 97 | #无水印:rtmp://localhost:1935/hls/$name 98 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 99 | -filter_complex "overlay=10:10,split=1[ar414]" 100 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_wm 101 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name; 102 | } 103 | 104 | application hls { 105 | live on; 106 | hls on; 107 | hls_fragment 5; 108 | hls_path /opt/data/hls; 109 | } 110 | } 111 | } 112 | ``` 113 | * 如果需要推多个直播云则复制多个 exec ffmpeg即可 如下: 114 | ```bash 115 | application stream { 116 | live on; 117 | #分流至本机hls 118 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 119 | -filter_complex "overlay=10:10,split=1[ar414]" 120 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_wm 121 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name; 122 | 123 | #分流至腾讯云 124 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 125 | -filter_complex "overlay=10:10,split=1[ar414]" 126 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.tencent.com/stream/$name_wm 127 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.tencent.com/stream/$name; 128 | 129 | #分流至阿里云 130 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 131 | -filter_complex "overlay=10:10,split=1[ar414]" 132 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.aliyun.com/stream/$name_wm 133 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.aliyun.com/stream/$name; 134 | } 135 | ``` 136 | 137 | * 水印位置 138 | * 水印位置 139 | 140 | | 水印图片位置 | overlay值 | 141 | | ---- | ---- | 142 | | 左上角 | 10:10 | 143 | | 右上角 | main_w-overlay_w-10:10 | 144 | | 左下角 | 10:main_h-overlay_h-10 | 145 | | 右下角 | main_w-overlay_w-10 : main_h-overlay_h-10 | 146 | * overlay参数 147 | 148 | | 参数 | 说明 | 149 | | ---- | ---- | 150 | | main_w | 视频单帧图像宽度(当前配置文件1920) | 151 | | main_h | 视频单帧图像高度(当前配置文件1080) | 152 | | overlay_w | 水印图片的宽度 | 153 | | overlay_h | 水印图片的高度 | 154 | 155 | 156 | ## :octocat:结语 157 | * 如果觉得对你有帮助请给我一个[start](https://github.com/ar414-com/nginx-rtmp-ffmpeg-conf) 158 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 ar414 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## :whale:[docker-nginx-rtmp-ffmpeg](https://hub.docker.com/repository/docker/ar414/nginx-rtmp-ffmpeg) 2 | [简体中文](./CN-README.md) 3 | 4 | ### Support 5 | [![jetbrains](./assets/images/jetbrains.svg)](https://www.jetbrains.com/?from=nginx-rtmp-ffmpeg) 6 | 7 | Based on the configuration and deployment of [docker-nginx-rtmp](https://github.com/alfg/docker-nginx-rtmp)the meaning of this article is to realize live streaming and live screen watermarking. 8 | * Nginx 1.16.1(Stable version compiled from source code) 9 | * nginx-rtmp-module 1.2.1(Compile from source) 10 | * ffmpeg 4.2.1(Compile from source) 11 | * Configured [nginx.conf](https://github.com/ar414-com/nginx-rtmp-ffmpeg-conf/blob/master/nginx.conf) 12 | * Only support 1920*1080 (if you need to support other resolutions, please refer to [nginx.conf](https://github.com/alfg/docker-nginx-rtmp/blob/master/nginx.conf)) 13 | * Realize two-way shunt 14 | * localhost 15 | * Live cloud provider(例:AWS) 16 | * Realize live watermark effect 17 | * Watermark image storage location (in the container):/opt/images/logo.png 18 | 19 | ## :computer:Deploy and run 20 | ### Server 21 | * Install docker (Centos7, other systems please use your search function) 22 | ```bash 23 | $ yum -y install docker #install docker 24 | $ systemctl enable docker #Configure startup 25 | $ systemctl start docker #Start the docker service 26 | ``` 27 | * Pull the docker image and run 28 | ```bash 29 | $ docker pull ar414/nginx-rtmp-ffmpeg 30 | $ docker run -it -d -p 1935:1935 -p 8080:80 --rm ar414/nginx-rtmp-ffmpeg 31 | ``` 32 | * Push stream address(Stream live content to): 33 | ``` 34 | rtmp://:1935/stream/$STREAM_NAME 35 | ``` 36 | * SSL certificate 37 | 38 | Copy the certificate to the container, and modify the nginx.conf configuration file in the container, and then re-commit (all the files in the container need to be re-committed to take effect) 39 | ``` 40 | #/etc/nginx/nginx.conf 41 | listen 443 ssl; 42 | ssl_certificate /opt/certs/example.com.crt; 43 | ssl_certificate_key /opt/certs/example.com.key; 44 | ``` 45 | 46 | ### OBS configuration 47 | * Stream Type: `Custom Streaming Server` 48 | * URL: `rtmp://:1935/stream` 49 | * Stream Key:ar414 50 | ![obs-config](https://cdn.ar414.com/obs-config.png) 51 | 52 | ### Watch test 53 | > HLS playback test tool:http://player.alicdn.com/aliplayer/setting/setting.html (If a certificate is configured, use https) 54 | 55 | * HLS playback address 56 | * With watermark:http://\:8080/hls/ar414_wm.m3u8 57 | ![ar414-hls-wm](https://cdn.ar414.com/ar414-hls-wm.png) 58 | * No watermark:http://\:8080/hls/ar414.m3u8 59 | ![ar414-hls](https://cdn.ar414.com/ar414-hls.png) 60 | 61 | > RTMP test tool:[PotPlayer](https://daumpotplayer.com/download/) 62 | 63 | * RTMP playback address 64 | * No watermark:rtmp://\:1935/stream/ar414 65 | ![ar414-rtmp](https://cdn.ar414.com/ar414-rtmp.png) 66 | * With watermark:Need to be diverted to other servers 67 | 68 | ## :page_facing_up:Brief explanation of configuration file (diversion, watermark and watermark position) 69 | > [Full configuration file](https://github.com/ar414-com/nginx-rtmp-ffmpeg-conf/blob/master/nginx.conf) 70 | 71 | * RTMP configuration 72 | ```bash 73 | rtmp { 74 | server { 75 | listen 1935; #port 76 | chunk_size 4000; 77 | #RTMP Live streaming configuration 78 | application stream { 79 | live on; 80 | # Add watermark and diversion, this time it is convenient to test diversion directly to the current server hls 81 | # Actual production is generally diverted to the live cloud(AWS) 82 | # Just replace the address that needs to be diverted 83 | # With watermark:rtmp://localhost:1935/hls/$name_wm 84 | # No watermark:rtmp://localhost:1935/hls/$name 85 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 86 | -filter_complex "overlay=10:10,split=1[ar414]" 87 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_wm 88 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name; 89 | } 90 | 91 | application hls { 92 | live on; 93 | hls on; 94 | hls_fragment 5; 95 | hls_path /opt/data/hls; 96 | } 97 | } 98 | } 99 | ``` 100 | * If you need to push multiple live clouds, copy multiple exec ffmpeg as follows: 101 | ```bash 102 | application stream { 103 | live on; 104 | #Shunt to local hls 105 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 106 | -filter_complex "overlay=10:10,split=1[ar414]" 107 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_wm 108 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name; 109 | 110 | #Diverted to Tencent Cloud 111 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 112 | -filter_complex "overlay=10:10,split=1[ar414]" 113 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.tencent.com/stream/$name_wm 114 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.tencent.com/stream/$name; 115 | 116 | #Diverted to Alibaba Cloud 117 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 118 | -filter_complex "overlay=10:10,split=1[ar414]" 119 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.aliyun.com/stream/$name_wm 120 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.aliyun.com/stream/$name; 121 | } 122 | ``` 123 | 124 | * Watermark position 125 | * Watermark position 126 | 127 | | Watermark image location | overlay value | 128 | | ---- | ---- | 129 | | Upper left corner | 10:10 | 130 | | Upper right corner | main_w-overlay_w-10:10 | 131 | | Bottom left | 10:main_h-overlay_h-10 | 132 | | Bottom right corner | main_w-overlay_w-10 : main_h-overlay_h-10 | 133 | * overlay param 134 | 135 | | param | description | 136 | | ---- | ---- | 137 | | main_w | Video single frame image width (current profile 1920) | 138 | | main_h | Video single frame image height (current profile 1080) | 139 | | overlay_w | The width of the watermark image | 140 | | overlay_h | The height of the watermark image | 141 | 142 | 143 | ### Support Author 144 | [![coffee](http://cdn.ar414.com/coffee.png)](https://www.buymeacoffee.com/ar414) [![wx](https://cdn.ar414.com/wecaht-logo.png)](https://cdn.ar414.com/wxpay_coffee.jpg)[![alipay](https://cdn.ar414.com/alipay-logo.png)](https://cdn.ar414.com/alipay_coffee.jpg) 145 | -------------------------------------------------------------------------------- /assets/images/jetbrains.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | 34 | 35 | 36 | 39 | 40 | 41 | 42 | 43 | 45 | 47 | 48 | 51 | 54 | 56 | 57 | 59 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | daemon off; 2 | 3 | error_log /dev/stdout info; 4 | 5 | events { 6 | worker_connections 1024; 7 | } 8 | 9 | rtmp { 10 | server { 11 | listen 1935; 12 | chunk_size 4000; 13 | 14 | application stream { 15 | live on; 16 | 17 | exec ffmpeg -i rtmp://localhost:1935/stream/$name -i /opt/images/ar414.png 18 | -filter_complex "overlay=10:10,split=1[ar414]" 19 | -map '[ar414]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_wm 20 | -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name; 21 | 22 | } 23 | 24 | application hls { 25 | live on; 26 | hls on; 27 | hls_fragment 5; 28 | hls_path /opt/data/hls; 29 | 30 | } 31 | } 32 | } 33 | 34 | http { 35 | access_log /dev/stdout combined; 36 | 37 | ssl_ciphers HIGH:!aNULL:!MD5; 38 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 39 | ssl_session_cache shared:SSL:10m; 40 | ssl_session_timeout 10m; 41 | 42 | server { 43 | listen 80; 44 | 45 | # Uncomment these lines to enable SSL. 46 | # Update the ssl paths with your own certificate and private key. 47 | # listen 443 ssl; 48 | # ssl_certificate /opt/certs/example.com.crt; 49 | # ssl_certificate_key /opt/certs/example.com.key; 50 | 51 | location /hls { 52 | types { 53 | application/vnd.apple.mpegurl m3u8; 54 | video/mp2t ts; 55 | } 56 | root /opt/data; 57 | add_header Cache-Control no-cache; 58 | add_header Access-Control-Allow-Origin *; 59 | } 60 | 61 | location /live { 62 | alias /opt/data/hls; 63 | types { 64 | application/vnd.apple.mpegurl m3u8; 65 | video/mp2t ts; 66 | } 67 | add_header Cache-Control no-cache; 68 | add_header Access-Control-Allow-Origin *; 69 | } 70 | 71 | location /stat { 72 | rtmp_stat all; 73 | rtmp_stat_stylesheet static/stat.xsl; 74 | } 75 | 76 | location /static { 77 | alias /www/static; 78 | } 79 | 80 | location = /crossdomain.xml { 81 | root /www/static; 82 | default_type text/xml; 83 | expires 24h; 84 | } 85 | } 86 | } --------------------------------------------------------------------------------