SRS(简单实时视频服务) 笔记(5)- 视频录制

2021-05-18 12:17:30 浏览数 (1)

1. 背景

在SRS使用中实现视频录制功能。

2. 思路

方案1:实时方式

  • 拍照:操作者点击拍照按钮,触发网络请求,后端收到请求后启动一个ffmpeg 命令行进行截图。
  • 录像:操作者点击开始录像按钮,触发网络请求,后端收到请求后启动一个ffmpeg 命令行录像,待操作者点击结束录像按钮后录像完成。
  • 方案优点:容易实现
  • 方案缺点:操作和响应的延时,即点击按钮后,约有2-5秒延迟(网络响应时间 ffmpeg启动时间 ffmpeg打开流时间 ffmpeg拍照响应时间。 争议:看到的视频的当前播放内容(时间) != 点击按钮时间 != ffmpeg拍照时间

方案2:DVR录制后拍照和截取

  • 前提:开启DVR功能,对每一个来自客户端发布的流都录制,并以时间分段成多个物理文件,然后就可以操作文件截图和截取视频了。
  • 拍照:点击按钮获得点击时间,从已经完成的DVR文件中识别文件名(包含了时间),读取文件后按指定时间差值截图。
  • 录像:获得 开始录制时间和结束录制时间。从已完成的DVR文件中识别文件名(包含了时间),读取文件截取时间段内的截图。
  • 方案优点:相比较于 方案1,时间误差的延迟小。
  • 方案缺点:依赖于DVR录制后的文件。需要考虑DVR临时录制文件的清理处置。

3. 实现

DVR 视频录制: SRS支持将RTMP流录制成FLV或MP4文件。下面的描述以FLV为例。

代码语言:javascript复制
vhost your_vhost {
    # DVR RTMP stream to file,
    # start to record to file when encoder publish,
    # reap flv/mp4 according by specified dvr_plan.
    dvr {
        # whether enabled dvr features
        # default: off
        enabled         on;
        # the filter for dvr to apply to.
        #       all, dvr all streams of all apps.
        #       <app>/<stream>, apply to specified stream of app.
        # for example, to dvr the following two streams:
        #       live/stream1 live/stream2
        # default: all
        dvr_apply       all;
        # the dvr plan. canbe:
        #       session reap flv/mp4 when session end(unpublish).
        #       segment reap flv/mp4 when flv duration exceed the specified dvr_duration.
        # @remark The plan append is removed in SRS3 , for it's no use.
        # default: session
        dvr_plan        session;
        # the dvr output path, *.flv or *.mp4.
        # we supports some variables to generate the filename.
        #       [vhost], the vhost of stream.
        #       [app], the app of stream.
        #       [stream], the stream name of stream.
        #       [2006], replace this const to current year.
        #       [01], replace this const to current month.
        #       [02], replace this const to current date.
        #       [15], replace this const to current hour.
        #       [04], replace this const to current minute.
        #       [05], replace this const to current second.
        #       [999], replace this const to current millisecond.
        #       [timestamp],replace this const to current UNIX timestamp in ms.
        # @remark we use golang time format "2006-01-02 15:04:05.999" as "[2006]-[01]-[02]_[15].[04].[05]_[999]"
        # for example, for url rtmp://ossrs.net/live/livestream and time 2015-01-03 10:57:30.776
        # 1. No variables, the rule of SRS1.0(auto add [stream].[timestamp].flv as filename):
        #       dvr_path ./objs/nginx/html;
        #       =>
        #       dvr_path ./objs/nginx/html/live/livestream.1420254068776.flv;
        # 2. Use stream and date as dir name, time as filename:
        #       dvr_path /data/[vhost]/[app]/[stream]/[2006]/[01]/[02]/[15].[04].[05].[999].flv;
        #       =>
        #       dvr_path /data/ossrs.net/live/livestream/2015/01/03/10.57.30.776.flv;
        # 3. Use stream and year/month as dir name, date and time as filename:
        #       dvr_path /data/[vhost]/[app]/[stream]/[2006]/[01]/[02]-[15].[04].[05].[999].flv;
        #       =>
        #       dvr_path /data/ossrs.net/live/livestream/2015/01/03-10.57.30.776.flv;
        # 4. Use vhost/app and year/month as dir name, stream/date/time as filename:
        #       dvr_path /data/[vhost]/[app]/[2006]/[01]/[stream]-[02]-[15].[04].[05].[999].flv;
        #       =>
        #       dvr_path /data/ossrs.net/live/2015/01/livestream-03-10.57.30.776.flv;
        # 5. DVR to mp4:
        #       dvr_path ./objs/nginx/html/[app]/[stream].[timestamp].mp4;
        #       =>
        #       dvr_path ./objs/nginx/html/live/livestream.1420254068776.mp4;
        # @see https://github.com/ossrs/srs/wiki/v3_CN_DVR#custom-path
        # @see https://github.com/ossrs/srs/wiki/v3_CN_DVR#custom-path
        #       segment,session apply it.
        # default: ./objs/nginx/html/[app]/[stream].[timestamp].flv
        dvr_path        ./objs/nginx/html/[app]/[stream].[timestamp].flv;
        # the duration for dvr file, reap if exceed, in seconds.
        #       segment apply it.
        #       session,append ignore.
        # default: 30
        dvr_duration    30;
        # whether wait keyframe to reap segment,
        # if off, reap segment when duration exceed the dvr_duration,
        # if on, reap segment when duration exceed and got keyframe.
        #       segment apply it.
        #       session,append ignore.
        # default: on
        dvr_wait_keyframe       on;
        # about the stream monotonically increasing:
        #   1. video timestamp is monotonically increasing,
        #   2. audio timestamp is monotonically increasing,
        #   3. video and audio timestamp is interleaved monotonically increasing.
        # it's specified by RTMP specification, @see 3. Byte Order, Alignment, and Time Format
        # however, some encoder cannot provides this feature, please set this to off to ignore time jitter.
        # the time jitter algorithm:
        #   1. full, to ensure stream start at zero, and ensure stream monotonically increasing.
        #   2. zero, only ensure stream start at zero, ignore timestamp jitter.
        #   3. off, disable the time jitter algorithm, like atc.
        # apply for all dvr plan.
        # default: full
        time_jitter             full;

        # on_dvr, never config in here, should config in http_hooks.
        # for the dvr http callback, @see http_hooks.on_dvr of vhost hooks.callback.srs.com
        # @read https://github.com/ossrs/srs/wiki/v3_CN_DVR#http-callback
        # @read https://github.com/ossrs/srs/wiki/v3_CN_DVR#http-callback
    }
}

仍需要配置 HTTP回调

代码语言:javascript复制
vhost your_vhost {
    dvr {
        enabled             on;
        dvr_path            ./objs/nginx/html/[app]/[stream]/[2006]/[01]/[02]/[15].[04].[05].[999].flv;
        dvr_plan            segment;
        dvr_duration        30;
        dvr_wait_keyframe   on;
    }
    http_hooks {
        enabled         on;
        on_dvr          http://127.0.0.1:8085/api/v1/dvrs;
    }
}

通过 HTTP回调将收到录制后的文件路径

代码语言:javascript复制
[2015-01-03 15:25:48][trace] post to dvrs, req={"action":"on_dvr","client_id":108,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"livestream","cwd":"/home/winlin/git/srs/trunk","file":"./objs/nginx/html/live/livestream/2015/1/3/15.25.18.442.flv"}

4. 参考

https://gitee.com/winlinvip/srs.oschina/wikis/v3_CN_DVR?sort_id=1701916

0 人点赞