nginx+flv

  • 新建一个源码目录
1
2
$ mkdir nginx-src
$ cd nginx-src
  • 下载相关源码
1
2
3
4
5
6
7
8
9
10
11
12
13
#nginx源码
$ git clone https://github.com/nginx/nginx.git
#nginx的rtmp模块源码
$ git clone https://github.com/arut/nginx-rtmp-module.git
$ wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
$ tar -zxvf openssl-1.0.2g.tar.gz
#nginx的依赖pcre源码
$ wget ftp://ftp.pcre.org/pub/pcre/pcre-8.39.tar.gz
$ tar -xzvf pcre-8.39.tar.gz
$ ./configure
$ cd pcre-8.39
$ sudo make && sudo make install
$ cd nginx$ git checkout release-1.9.9

这里还需要openssl的源码,因为wget下载失败,因此我用http下载再上传到nginx-src目录

  • 编译脚本
1
vi cfg.sh

写入:

1
2
3
4
5
6
7
8
9
#  cfg.sh文件的内容
auto/configure --prefix=/usr/local/nginx \
--with-pcre=../pcre-8.39 \
--with-openssl=../openssl-1.0.2g \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_flv_module \ # 同时安装flv和rtmp会冲突,可先安装rtmp,再安装flv,通过add-module
--with-http_mp4_module \
--add-module=../nginx-rtmp-module/
  • 配置并安装
1
2
3
4
$ chmod a+x cfg.sh
$ sudo ./cfg.sh
$ sudo make
$ sudo make install

如果配置出问题可能是缺少包,可尝试

1
sudo apt-get install openssl openssl-dev
  • 配置rtmp服务
1
vi /usr/local/nginx/conf/nginx.conf

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
worker_processes  1;

events {
worker_connections 1024;
}

rtmp {
server {
listen 1933;
chunk_size 4096;

application vod {
play /opt/video/vod;
}

application live{ #第一处添加的直播字段
live on;
allow publish all;
allow play all;
}
}

}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 7766;
server_name localhost;

location /stat { #第二处添加的location字段。
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl { #第二处添加的location字段。
root /etc/rtmpServer/nginx-rtmp-module/;
}

location / {
root html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
  • 启动nginx
1
/usr/local/nginx/sbin/nginx
  • 推流
1
ffmpeg -re -i ./video.mp4 -c copy -f flv rtmp://118.24.8.33:1933/rtmplive/demo

-A RH-Firewall-1-INPUT -m state —state NEW -m tcp -p tcp —dport 8088-j ACCEPT

1
2
iptables -A INPUT -p tcp --dport 8088 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

flv.js 版

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
worker_processes  4;
events {
worker_connections 1024;
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;


rtmp{
out_queue 4096;
out_cork 8;
max_streams 12;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s;
log_size 1m;
server{
listen 1933;
# server_name 192.168.42.34;
application myapp{
live on;
gop_cache on;
# 测试流
# pull rtmp://live.hkstv.hk.lxdns.com/live/hks;
}
application hls{
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
}
application dash{
live on;
dash on;
dash_path /usr/local/nginx/html/dash;
}
}
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 7766;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /live{
flv_live on;
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
location /hls{
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/nginx/html/hls;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /usr/local/nginx/html/dash;
add_header 'Cache-Control' 'no-cache';
}

location /stat {
#configuration of push & pull status
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/nginx-http-flv-module;
}

location /control {
rtmp_control all; #configuration of control module of rtmp
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}

推流 myapp 是rtmp中的应用, test是自己取的stream名字

1
./ffmpeg.exe -re -stream_loop -1 -i ./video2.mp4 videoCodecType=H.264 -vcodec copy -an -acodec copy -f flv rtmp://118.24.8.33:1933/myapp/test

拉流

1
2
3
4
5
6
7
8
FLV: 
http://118.24.8.33:7766/live?port=1933&app=myapp&stream=test

RTMP:
rtmp://118.24.8.33:1933/myapp/test

HLS:
http://118.24.8.33:7766/myapp/test.m3u8

Reference:

  • nginx-flv

https://blog.csdn.net/hjing123/article/details/103983309

https://segmentfault.com/a/1190000016043297

https://blog.csdn.net/string_kai/article/details/100598268