【入门】ffmpeg、nginx、nginx-http-flv-module转发rtsp流、VLC查看
参考:https://blog.csdn.net/string_kai/article/details/100598268
-
使用ffmpeg将rtsp转成rtmp
-
使用nginx中的nginx-http-flv-module 模块:
- 转发到rtmp的1935端口上
- 转发到http的80端口上
-
使用VLC查看
nginx安装 nginx-http-flv-module 模块并添加到nginx
1.clone nginx-http-flv-module :
cd /usr/local/src/
git clone https://github.com/winshining/nginx-http-flv-module.git
**2.将模块编译进 NGINX:**注意/usr/local/src/nginx-1.6.2
是下载解压nginx的位置,/usr/local/webserver/nginx
是nginx安装的位置,用来运行nginx
cd /usr/local/src/nginx-1.6.2 #nginx安装位置
./configure --prefix=/usr/local/webserver/nginx --add-module=/usr/local/src/nginx-http-flv-module
make
make install
3./usr/local/webserver/nginx/conf/nginx.conf
添加rtmp
和location /live
http {
server {
# rtsp -> flv
location /live {
flv_live on;
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
}
}
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
log_size 1m; #buffer size used by log module to log in access.log
server {
listen 1935; #Nginx监听的RTMP推流/拉流端口
server_name localhost; #for suffix wildcard matching of virtual host name
application live {
live on;
gop_cache off; #open GOP cache for reducing the wating time for the first picture of video
}
}
}
4.重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
注意重新载入配置文件可能不成功。我在重新载入的时候1935端口并没有开启,实际可能需要关闭nginx,再开启:
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
/usr/local/webserver/nginx/sbin/nginx # 运行
hint:可以通过linux查看端口占用情况
lsof -i:1935
如果1935端口开启,会有类似如下的信息:
[root@k8s-node1 nginx]# lsof -i:1935
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3387 root 7u IPv4 247733434 0t0 TCP *:macromedia-fcs (LISTEN)
nginx 3388 www 7u IPv4 247733434 0t0 TCP *:macromedia-fcs (LISTEN)
/usr/local/webserver/nginx/conf/nginx.conf
全部如下:
user www www;
worker_processes 1;
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
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"';
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#gzip on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/webserver/nginx/dist;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
# rtsp -> flv
location /live {
flv_live on;
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
access_log off;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
log_size 1m; #buffer size used by log module to log in access.log
server {
listen 1935; #Nginx监听的RTMP推流/拉流端口
server_name localhost; #for suffix wildcard matching of virtual host name
application live {
live on;
gop_cache off; #open GOP cache for reducing the wating time for the first picture of video
}
}
}
# rtmp {
# server {
# listen 1935; #Nginx监听的RTMP推流/拉流端口
# application live {
# live on; #当推流时,RTMP路径中的APP(RTMP中一个概念)匹配myapp时,开启直播
# record off; #不记录视频
# gop_cache off;
# }
# }
# }
ffmpeg转发
安装ffmpeg,请参考其他文章。
ffmpeg使用下面的命令转发,需要一直运行:
ffmpeg -rtsp_transport tcp -i rtsp://username:password@example.com[:port] -vcodec h264 -f flv -an rtmp://localhost:1935/live/room
VLC查看
rtmp视频的url根据nginx配置文件的不同,而改变:
rtmp://example.com[:port]/live/room
flv视频的url由nginx配置文件决定,格式
http://example.com:80/live?port=1935&app=live&stream=room
可使用VLC查看对应的网址。
使用python运行ffmpeg,效果同ffmpeg转发
使用python,我们可以自己控制输出的参数了。
# https://www.cxyzjd.com/article/xiao__run/114662994
# https://zhuanlan.zhihu.com/p/74260950
import subprocess as sp
class Live(object):
def __init__(self, cameraPath, rtmpUrl):
# 自行设置
self.rtmpUrl = rtmpUrl
self.cameraPath = cameraPath # rtsp://username:password@example.com[:port]
def run(self):
print('开启推流')
# fps = 30 #60
# width = 640 #1920
# height = 480 #1080
# ffmpeg -rtsp_transport tcp -i rtsp://username:password@example.com[:port] -vcodec h264 -f flv -an rtmp://localhost:1935/live/room
command = ['ffmpeg',
'-rtsp_transport',
'tcp',
'-i', self.cameraPath,
'-vcodec', 'h264',
'-f', 'flv',
# '-r', str(fps),
# '-s', '{}x{}'.format(width, height),
# '-preset', 'ultrafast',
'-an',
self.rtmpUrl]
p = sp.Popen(command)
live = Live(cameraPath='rtsp://username:password@example.com[:port]', rtmpUrl='rtmp://localhost:1935/live/room')
live.run()
print('rtmp://localhost:1935/live/room')
踩过的坑/错误
make的时候遇到错误:[-Werror=unused-but-set-variable]
修改 objs/Makefile 删除里面的 -Werror
往/usr/local/webserver/nginx/conf/nginx.conf
添加rtmp{}出错:
nginx: [emerg] unknown directive "flv_live" in /usr/local/webserver/nginx/conf/nginx.conf:85
nginx: [emerg] unknown directive "rtmp" in /usr/local/webserver/nginx/conf/nginx.conf:13
解决方法,./configure的时候增加–prefix,为nginx的安装路径:
./configure --prefix=/usr/local/webserver/nginx --add-module=/usr/local/src/nginx-http-flv-module
make
make install
错误:Connection to tcp://localhost:1935 failed (Connection refused)
[tcp @ 0x1c4fc60] Connection to tcp://localhost:1935 failed (Connection refused), trying next address
[tcp @ 0x1c4fc60] Connection to tcp://localhost:1935 failed: Connection refused
[rtmp @ 0x1a4e740] Cannot open connection tcp://localhost:1935
rtmp://localhost:1935/live/room: Connection refused
尝试重启nginx:
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
/usr/local/webserver/nginx/sbin/nginx # 运行
linux查看端口占用情况
lsof -i:1935
如果1935端口开启,会有类似如下的信息:
[root@k8s-node1 nginx]# lsof -i:1935
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3387 root 7u IPv4 247733434 0t0 TCP *:macromedia-fcs (LISTEN)
nginx 3388 www 7u IPv4 247733434 0t0 TCP *:macromedia-fcs (LISTEN)
ffserver在这边并不需要【下面的内容略过】
ffserver -d -f /usr/share/doc/ffmpeg-3.4.8/ffserver.conf
修改/usr/share/doc/ffmpeg-3.4.8/ffserver.conf
<Feed feed1.ffm>
ACL allow 127.0.0.1
ACL allow localhost #增加
</Feed>
ffserver -d -f /usr/share/doc/ffmpeg-3.4.8/ffserver.conf