美文网首页
2018-03-06-nginx_rtmp.md

2018-03-06-nginx_rtmp.md

作者: 水泥中的鱼 | 来源:发表于2018-03-06 17:20 被阅读0次

    提示,这不是一个手把手的教程,更多是说了一个思路,需要对docker,系统,对程序编译,有初步认识即可。

    剧情介绍

    有这么一个需求,需要一个服务展示多个视频流,并且录制视频流,然后就这些

    思路:

    整体结构是,用户端obs推流,服务器端nginx_rtmp做转发及录像

    整体有这么两种思路,一个是docker的nginx_rtmp,这个优点,速度非常快,非常方便,可以说瞬间解决了大部分问题(服务器端及转发),问题就是,,不能录像。
    另一个就是自己安装nginx和rtmp模块,改配置文件。自定义余量大,可控范围广,缺点,配置。。。。略微有点复杂。

    方法1

    安装docker(不会自己学习)

    sudo docker run      \
        -p 1935:1935        \
        -p 8080:8080        \
        -e RTMP_STREAM_NAMES=live,st1   \
        -d \
        --rm \
        jasonrivers/nginx-rtmp
    

    简单说两句
    -p 外部端口:内部端口
    -e 此次使用中的参数指开两个频道 live和st1
    -d 容器启动后,在后台运行
    --rm 容器终止运行后,自动删除容器文件
    jasonrivers/nginx-rtmp 这个就是此次使用的镜像https://github.com/JasonRivers/Docker-nginx-rtmp

    执行完上面的命令,就可以开始推流和查看了
    这里说一下踩过的一些坑,本来打算的是-v挂载文件系统,直接使用宿主机的配置文件替换掉镜像里面的配置文件,但是执行后发现不行,仔细看了一下上面image的dockfile文件里面有个run脚本,整体流程是,启动镜像,然后用run.sh脚本生成了最后的nginx.conf文件,这个也就是上面-e参数的作用,直接-v指定是无效的。

    方法2

    这个思路说简单也简单,说复杂也复杂
    下载nginx安装包,下载rtmp模块,编译,安装,修改配置文件,启动运行
    先说踩的一个坑,不要用ubuntu,即便关了selinux,后面也会出现http播放正常,rtmp无法播放的问题,我是用centos7解决这个问题。

    关键的nginx配置文件来了

    worker_processes 1;
    
    events {
        worker_connections 1024;
    }
    http {
        include             mime.types;
        default_type        application/octet-stream;
        sendfile            on;
        keepalive_timeout   65;
    
        server {
            listen          8080;
            server_name     localhost;
    
            location /hls {
                types {
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2ts ts;
                }
                root /tmp;
                add_header  Cache-Control no-cache;
            }
    
            location /on_publish {
                return  201;
            }
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                root /opt/nginx/conf/stat.xsl;
            }
            location /control {
                rtmp_control all;
            }
    
            error_page  500 502 503 504 /50x.html;
            location = /50x.html {
                root html;
            }
        }
    }
    
    rtmp {
        server {
            listen 1935;
            chunk_size 4096;
            application live {
                live on;
                record off;
                on_publish http://localhost:8080/on_publish;
                hls on;
                hls_path /tmp/hls;
            }
            application st1 {
                live on;
                record video;
                record_path /tmp/;
                record_suffix -%d-%b-%y-%T.flv;
                record_unique on;
                record_max_size 100M;
                on_publish http://localhost:8080/on_publish;
            }
        }
    }
    

    root /opt/nginx/conf/stat.xsl;这个需要根据实际情况进行配置,看你的文件在那,记住就是了,回来可以通过访问http://ip:port/stat 查看服务器状态,接入频道,用户,chrome看有问题,无法直接显示内容,查看页面源文件,可以看到内容。

    这个配置文件的效果是生成两个频道,live,st1,其中st1进行录像

    record [off|all|audio|video|keyframes|manual]*

    record video;
    record_path /tmp/rec;
    

    mystream-24-Apr-13-18:23:38.flv

    record_suffix -%d-%b-%y-%T.flv;
    

    If turned on appends current timestamp to recorded files. Otherwise the same file is re-written each time

    new recording takes place
    record_unique on;
    record_max_size 100M;
    

    其他record的参数,参见

    https://github.com/arut/nginx-rtmp-module/wiki/Directives#record
    

    2018年3月6日17:20:31

    相关文章

      网友评论

          本文标题:2018-03-06-nginx_rtmp.md

          本文链接:https://www.haomeiwen.com/subject/golrfftx.html