obs-自定义推流

obs-自定义推流


前篇


协议对比

在查阅资料后找到了以下常用协议:

  • rtmp:娱乐直播平台常见直播协议,便于分发,但延迟较高
    • obs 实测, 同一台电脑都会有 1s 左右的延迟…
  • rtsp:网络摄像机常见直播协议,基于udp或tcp,延迟较低
  • HLS:常用于播放各种节目,可回放,延迟很高
  • webrtc:低延迟的直播协议,但是资料很少,配套推流设施不完善

综上, rtsp 延迟点会好点.


思路

  1. 搭建一个串流服务

    比如: rmtp 协议服务地址 192.168.2.200:1935

  2. obs 推流到 串流服务 上

    文件 -> 设置 -> 推流, 选择 自定义, 然后配置 串流服务 的地址: rtmp://192.168.2.200:1935/live

  3. 客户端 (potplayer/vlc 等) 打开 串流服务: rtmp://192.168.2.200:1935/live/hello (hello 是 串流密钥)

    (如果有 用户名密码, 则地址为: rtmp://用户名:密码@192.168.2.200:1935/live/hello)

    • potplayer

    • vlc


ffmpeg 工具


ffmpeg 命令

  • 推流 到 rtsp 服务.

    命令: ffmpeg -re -stream_loop -1 -i H:/output_obs/xichengbian01.mp4 -c copy -f rtsp rtsp://192.168.2.200:8554/mystream

    1
    2
    3
    4
    $ ffmpeg -re -stream_loop -1 -i H:/output_obs/xichengbian01.mp4 -c copy -f rtsp rtsp://192.168.2.200:8554/mystream

    ...
    frame= 4299 fps= 60 q=-1.0 size=N/A time=00:01:11.61 bitrate=N/A speed= 1x
    • -re : 是以流的方式读取
    • -stream_loop -1 : 表示无限循环读取
    • -i : 就是输入的文件
    • -f : 格式化输出到哪里
  • 从 rtsp 服务 拉流

    命令: ffmpeg -i rtsp://192.168.2.200:8554/mystream -c copy C:/Users/wilker/Desktop/output.mp4

    1
    2
    3
    4
    $ ffmpeg -i rtsp://192.168.2.200:8554/mystream -c copy C:/Users/wilker/Desktop/output.mp4

    ...
    frame= 1289 fps= 57 q=-1.0 Lsize= 16242kB time=00:00:23.19 bitrate=5737.3kbits/s speed=1.02x
    • -i : 输入推流服务地址

ffplay 命令

  • FFmpeg之ffplay命令使用 - https://blog.csdn.net/xuyankuanrong/article/details/77529468

  • 常用命令

    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
      //1) 播放 test.mp4 ,播放完成后自动退出
    $ ffplay -autoexit test.mp4

    //2) 以 320 x 240 的大小播放 test.mp4
    $ ffplay -x 320 -y 240 test.mp4

    //3) 将窗口标题设置为 "myplayer",循环播放 2 次
    $ ffplay -window_title myplayer -loop 2 test.mp4

    //4) 播放 双通道 32K 的 PCM 音频数据
    $ ffplay -f s16le -ar 32000 -ac 2 test.pcm



    ---

    ### rtsp 服务器搭建

    - rtsp 系列专题(一)之 rtsp 服务器搭建 - https://xie.infoq.cn/article/123e589178ccb2d74830d5686
    - GitHub - https://github.com/ZLMediaKit/ZLMediaKit
    - linux_手把手教ubuntu搭建rtsp视频推送服务 - https://hexo.yuanjh.cn/hexo/090a8054/



    ---

    #### rtsp-simple-server

    - 下载 - https://github.com/aler9/rtsp-simple-server/releases
    - 以 windows 为例 - https://github.com/aler9/rtsp-simple-server/releases/download/v0.17.16/rtsp-simple-server_v0.17.16_windows_amd64.zip

    - RTSP服务器实现推流和拉流例子 - https://blog.csdn.net/li_xue_zhao/article/details/107317509
    - RTMP,RTSP,HLS比较与分析 - https://blog.csdn.net/xiaoliouc/article/details/8613935



    ---

    ##### 使用流程

    1. 直接双击启动

    ![](https://pic05.wilker.cn/20220203204000-260.webp)

    2. 测试

    1. 推流一个 mp4 文件到 rtsp 协议, 命令: `ffmpeg -re -stream_loop -1 -i H:/output_obs/xichengbian01.mp4 -c copy -f rtsp rtsp://192.168.2.200:8554/mystream`

    ```json
    $ ffmpeg -re -stream_loop -1 -i H:/output_obs/xichengbian01.mp4 -c copy -f rtsp rtsp://192.168.2.200:8554/mystream
    ...
    frame= 701 fps= 60 q=-1.0 size=N/A time=00:00:11.66 bitrate=N/A speed= 1x
    - 此时会看到服务多了个推流日志, 路径是 mystream
    
      ![](https://pic05.wilker.cn/20220203211426-800.webp)
    
    1. 拉流, 使用 vls 客户端

      文件 -> 打开网络串流, 输入地址: rtsp://192.168.2.200:8554/mystream


obs rtsp 推流实现

obs 不能直接在 推流 中使用 rtsp 协议推流, 不然一旦 开始推流 就会重复 断开 和 连接

有个曲线救国的方式可以实现 obs rtsp 推流, 就是采用录制输出到 url 的方式, 这种方式延迟也是比较大, 还不如直接 rtmp 推流.

  1. 输出 -> 录像 中设置, 修改参数

    1. 输出类型 改为 输出到 url

    2. 容器格式 改为 rtsp

  2. 然后点击 开始录制 就可以实时推流到 rtsp 服务