RTSP视频流播放几种解决方式
RTSP视频流播放几种解决方式
一、使用FFMPEG把RTSP转RTMP 拉流转码到Nginx +Nginx-rtmp-moudle
首先要先下载ffmpeg
第三方下载:https://linux.softpedia.com/get/Multimedia/Video/FFmpeg-3054.shtml ,
官网:http://ffmpeg.org/download.html,github官网也有。
安装参考https://www.jianshu.com/p/1be929404528
中间可能因环境不同坑比较多,网上的解决方案也比较成熟可以多搜索。
安装Nginx和Nginx-rtmp-moudle
主要步骤 解压完Nginx包后 ./configruation –add-module=/你的路径/nginx-rtmp-module-master 加上这个模块 然后make makeinstall
之后 修改Nginx.conf 加上rtmp相应的配置 安装和配置参考:https://www.cnblogs.com/bluesky-yuan/p/10148700.html 这个坑相对较少 。
完成上述两部之后可以开始推流了
推流命令示例:ffmpeg -re -rtsp_transport tcp -i rtsp://admin:njxyj2019@11.15.159.26:554/h264 -tune zerolatency -vcodec libx264 -b:v 400k -s 720x576 -r 25 -f flv rtmp://你的ip:你在Nginx.conf下修改的rtmp端口/conf中的application/自定义流名(随便)
关于ffmpeg的各种参数指令 含义 可以通过百度获取 太多了 就不写了
上面完成后基本就可以通过 rtmp://你的ip:你在Nginx.conf下修改的rtmp端口/conf中的application/自定义流名(随便) 访问nginx来获取视频流播放了
二、使用FFMPEG+PingOS
同上第一步
安装 PingOS 全中文官网 安装配置一条龙 都有(如果你的服务器不能连接外网,你需要下载安装后把release.sh 脚本中的代码 一点点自己执行 ,所有需要下载的操作都提前下载好安装好 放到服务器上再执行之后的操作。)
PingOS主要还是基于Nginx及他的一些moudle,可以说主要基于Nginx-rtmp-mouldle
所以说如果觉得对流的流畅度及一些别的不满意主要还是通过修改内置的nginx.conf来调整。
拉流推送到PingOS, obs或者FFmpeg 都可以实现 ffmpeg还是同上的第三步 obs可以自行百度
三、经验分享
推荐使用FFMPEG+PingOS
测试表明使用PingOS的首帧播放和延迟很低,rtmp流稳定流畅很少卡顿
PingOS 支持多种播放模式 hls hls+ rtmp ts flv , 支持多种协议 rtmp http 和https(听说rtsp也在开发中) 同一个推流 可以有多种播放方式
测试之后 rtmp实时性最高 延迟最低 但是有些前端播放器会获取首帧之后就卡住 (由于没有前端配合没有测试很多播放器) rtmp的劣势主要还是依赖于flash
hls 的延迟和流畅度也很不错,但是实时性差一些,hls主要通过储存切片的方式实现播放,可以通过配置每个切片的大小为多少,把存储的切片播完时 每次切换切片会微微卡顿一下 , 优势 可以通过http协议请求 不依赖于flash
别的方式测试了之后延迟和首帧播放时间很长 也没有过多研究 如有需要可以对应官网多多研究配置。
贴一个我们项目的nginx配置作为参考
user root;
daemon on;
master_process on;
worker_processes 4;
#worker_rlimit 4g;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
worker_rlimit_nofile 102400;
worker_rlimit_core 2G;
working_directory /tmp;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 4096;
multi_listen unix:/tmp/http 8082;
multi_listen unix:/tmp/rtmp 1935;
dynamic_refresh_interval 5s;
dynamic_domain_buckets 1001;
resolver 114.114.114.114 valid=1m;
resolver_timeout 30s;
}
#stream_zone buckets=1024 streams=4096;
#dynamic_conf conf/nginx_dynamic.conf 10;
#dynamic_log logs/dynamic.log info;
rtmp {
# log_format log_bandwidth '{"app":"$app","name":"$name","bitrate":$bitrate,"args":"$args","timestamp":$ntp,"ts":"$time_local","type":"$command","remote_addr":"$remote_addr","domain":"$domain"}';
# access_log logs/bandwidth.log log_bandwidth trunc=60s;
server {
listen 1935;
serverid 000;
out_queue 2048;
server_name 11.15.238.245;
rtmp_auto_pull on;
rtmp_auto_pull_port unix:/tmp/rtmp;
application push {
live on;
push rtmp://127.0.0.1/live app=live;
}
application live {
live_record off;
live_record_path /srv/program/ffmpeg;
recorder r1{
record all;
record_path /srv/program/ffmpeg;
}
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 10s;
hls_max_fragment 20s;
hls_playlist_length 2m;
hls_type live;
hls_continuous on;
hls_fragment_slicing aligned;
hls2 on;
mpegts_cache_time 20s;
hls2_fragment 2000ms;
hls2_max_fragment 3000ms;
hls2_playlist_length 6000ms;
wait_key on;
wait_video on;
cache_time 1s;
send_all on;
low_latency off;
fix_timestamp 2s;
# h265 codecid, default 12
hevc_codecid 12;
}
}
}
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" "$http_X-Real-IP" "$host"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#reset_server_name www.test1.com www.test2.com;
#gzip on;
upstream hlsm {
# hash $remote_addr consistent;
hash $arg_session consistent;
# 这里需要注意,你要开几个进程,就要按这个规则写几条记录
server unix:/tmp/http.0;
server unix:/tmp/http.1;
server unix:/tmp/http.2;
server unix:/tmp/http.3;
}
server {
listen 8082;
# listen 443 ssl;
# ssl_certificate /usr/local/pingos/cert/full_chain.pem;
# ssl_certificate_key /usr/local/pingos/cert/privkey.pem;
location /rtmp_stat {
rtmp_stat all;
rtmp_stat_stylesheet /stat.xsl;
}
location /xstat {
rtmp_stat all;
}
location /sys_stat {
sys_stat;
}
location ~ .mp4$ {
root html;
#mp4;
}
location /control {
rtmp_control all;
}
location /flv {
flv_live 1935 app=live;
add_header 'Access-Control-Allow-Origin' '*';
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "*";
add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
add_header "Access-Control-Expose-Headers" "*";
}
location /ts {
ts_live 1935 app=live;
expires -1;
add_header 'Access-Control-Allow-Origin' '*';
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "*";
add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
add_header "Access-Control-Expose-Headers" "*";
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
expires -1;
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*';
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "*";
add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
add_header "Access-Control-Expose-Headers" "*";
}
location /hlsm {
hls2_live 1935 app=live;
add_header 'Access-Control-Allow-Origin' '*';
add_header Cache-Control no-cache;
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "*";
add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
add_header "Access-Control-Expose-Headers" "*";
}
location /hls2 {
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 1M;
proxy_busy_buffers_size 2M;
proxy_max_temp_file_size 0;
set $hls_args location=/hls2&scheme=$scheme;
# if ($args) {
# set $hls_args $args&location=/hls2&scheme=$scheme;
# }
proxy_set_header Host $host:$server_port;
rewrite ^/(.*)/(.*)\.(.*)$ /hlsm/$2.$3?$hls_args break;
proxy_pass http://hlsm;
}
location / {
chunked_transfer_encoding on;
root html/;
}
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。邮件至 wcfinyourheart@163.com
文章标题:RTSP视频流播放几种解决方式
本文作者:wcf
发布时间:2020-07-16, 10:29:53
最后更新:2020-07-16, 10:41:33
原始链接:http://1007638786.github.io/2020/07/16/rtsp-shi-pin-liu-bo-fang-ji-chong-jie-jue-fang-shi/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。