Warm tip: This article is reproduced from serverfault.com, please click

ffmpeg-设置自定义RTMP流服务器时无法打开连接tcp:// localhost:1935

(ffmpeg - Cannot open connection tcp://localhost:1935 when to set up custom RTMP stream server)

发布于 2020-11-29 15:57:30

我的简单网络如下:

192.168.31.52 is my local pc 
192.168.31.251 is an ip camera.

我可以rtsp://192.168.31.251/cam/realmonitor?channel=1&subtype=0使用SMPlayer打开流
用这种方式构建用于自定义RTMP流的nginx。

sudo apt update
sudo apt install build-essential git
sudo apt install libpcre3-dev libssl-dev zlib1g-dev 
git clone https://github.com/arut/nginx-rtmp-module.git
git clone https://github.com/nginx/nginx.git
cd nginx
./auto/configure --add-module=../nginx-rtmp-module
make
sudo make install

设置nginx的配置文件:

sudo vim /usr/local/nginx/conf/nginx.conf
rtmp { 
    server { 
        listen 1935; 
        application live { 
            live on; 
            interleave on;
 
            hls on; 
            hls_path /tmp/hls; 
            hls_fragment 15s; 
        } 
    } 
} 

然后设置nginx的权限:

mkdir /tmp/hls
sudo chmod -R 755  /tmp/hls
sudo chown -R www-data:www-data  /tmp/hls

在中编辑index.html /tmp/hls

<p>test for nginx</p>

双方127.0.0.1/index.html192.168.31.52/index.html可以打开/tmp/hls/index.html

现在在我的网络上打开端口1935:

sudo firewall-cmd --zone=public --add-port=1935/tcp --permanent
sudo firewall-cmd --reload 
sudo firewall-cmd --list-ports | grep 1935
1935/tcp

启动nginx:

sudo systemctl start nginx

从IP摄像机192.168.31.251到本地PC 192.168.31.52的rtsp流的上游。

input="rtsp://192.168.31.251/cam/realmonitor?channel=1&subtype=0"
output="rtmp://192.168.31.52:1935/live/sample"
ffmpeg -i $input -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv $output

它遇到以下错误:

[tcp @ 0x59fb700] Connection to tcp://192.168.31.52:1935 failed: Connection refused
[rtmp @ 0x59fc5c0] Cannot open connection tcp://192.168.31.52:1935
rtmp://192.168.31.52:1935/live/sample: Connection refused

为了使问题更简单,我用$input本地PC中的mp4文件替换了相同的错误信息。
如何解决?
ping我的机器:

ping 192.168.31.52
PING 192.168.31.52 (192.168.31.52): 56 data bytes
64 bytes from 192.168.31.52: icmp_seq=0 ttl=64 time=0.108 ms
64 bytes from 192.168.31.52: icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from 192.168.31.52: icmp_seq=2 ttl=64 time=0.111 ms

为什么端口1935未打开,设置后我重新启动了Nginx?

sudo lsof -i:1935
#nothing in the output
netstat -ltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:51413           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:1080          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8123          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8384          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:9091            0.0.0.0:*               LISTEN     
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22000                :::*                    LISTEN     
tcp6       0      0 :::51413                :::*                    LISTEN     
tcp6       0      0 :::21                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::2681                 :::*                    LISTEN  

防火墙命令不起作用:

sudo firewall-cmd --zone=public --add-port=1935/tcp --permanent
sudo firewall-cmd --reload 

我的nginx版本:

sudo nginx -v
nginx version: nginx/1.10.3
Questioner
showkey
Viewed
11
aergistal 2021-01-08 19:54:33

如果你自己尚未配置nginx为系统服务,则很可能正在运行预安装的版本,而不是所编译的包含RTMP模块的版本。

sudo nginx -v
nginx version: nginx/1.10.3

这是2017年以来的稳定版本。

使用最新版本https://github.com/nginx/nginx.git

./objs/nginx -v
nginx version: nginx/1.19.6

当你首次提出问题时,2020年11月29日的版本应为主线1.19.5。

检查系统服务的定义和nginx可执行路径。