EasyDarwin开源流媒体服务器(www.easydarwin.org)现在使用Go版本实现了。最新的代码提交,已经支持了推流(或者拉流)的同时进行本地存储。
本地存储的原理,是在推流的同时启动ffmpeg作为一路Client来做存储。ffmpeg在demux和mux的工作方面已经十分成熟,我们没必要再重复发明***。因此这种做法十分取巧而且简单有效,也不会侵入原来的代码架构。如下图所示:
使用方法如下:
安装ffmpeg。
Downloads/ffmpeg-20180719-9cb3d8f-macos64-shared/
配置存储
EasyDarwin的配置文件,默认为与可执行文件同目录的EasyDarwin.ini文件。EasyDarwin也支持使用用户指定的配置文件,这样很方便在调试环境和生产环境来切换。自定义配置文件如何配置,参考我的另一篇文章的说明。
我们使用文本编辑器打开EasyDarwin.ini,内容如下:
[http]
port=10008
default_username=admin
default_password=admin
[rtsp]
port=554
timeout=28800
gop_cache_enable=1
save_stream_to_mp4=0
ffmpeg_path=
mp4_dir_path=
其中,rtsp小结里面,有如下几项是事关存储的,我们要做些修改:
/Users/apple/Downloads/ffmpeg-20180719-9cb3d8f-macos64-shared/bin/ffmpeg/Users/apple/Downloads/EasyDarwinGoMP4[EasyDarwin] 2018/11/17 12:45:35 rtsp-server.go:67: Prepare to save stream to local....
测试验证
我们可以安装一个手机端的EasyPusher来方便地测试推送(EasyPusher是手机端的一个RTSP推流器,详见:https://github.com/EasyDarwin/EasyPusher)。
RTSP://SERVER_IPrtsp server start --> rtsp://192.168.0.108
Input #0, rtsp, from \'rtsp://192.168.0.108:554/439622.sdp\':
Metadata:
title : EasyDarwin
comment : EasyDarwin
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: h264 (Baseline), yuv420p(tv, unknown/bt470bg/unknown, progressive), 360x640, 18.67 tbr, 90k tbn, 180k tbc
Stream #0:1: Audio: aac (LC), 8000 Hz, mono, fltp
Output #0, mp4, to \'/Users/apple/Downloads/EasyDarwinGoMP4/439622.sdp/20181117150909.mp4\':
Metadata:
title : EasyDarwin
comment : EasyDarwin
encoder : Lavf58.17.101
Stream #0:0: Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv, unknown/bt470bg/unknown, progressive), 360x640, q=2-31, 18.67 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 38 fps=0.0 q=-1.0 size= 0kB time=00:00:02.60 bitrate= 0.1kbits/s speed=5.21x
frame= 48 fps= 47 q=-1.0 size= 0kB time=00:00:03.15 bitrate= 0.1kbits/s speed=3.07x
frame= 57 fps= 37 q=-1.0 size= 0kB time=00:00:03.71 bitrate= 0.1kbits/s speed=2.39x
frame= 67 fps= 33 q=-1.0 size= 0kB time=00:00:04.17 bitrate= 0.1kbits/s speed=2.03x
frame= 77 fps= 30 q=-1.0 size= 0kB time=00:00:04.70 bitrate= 0.1kbits/s speed=1.83x
frame= 86 fps= 28 q=-1.0 size= 0kB time=00:00:05.20 bitrate= 0.1kbits/s speed=1.69x
frame= 96 fps= 27 q=-1.0 size= 256kB time=00:00:05.74 bitrate= 365.2kbits/s speed=1.59x
frame= 106 fps= 26 q=-1.0 size= 256kB time=00:00:06.28 bitrate= 333.7kbits/s speed=1.51x
frame= 115 fps= 25 q=-1.0 size= 256kB time=00:00:06.78 bitrate= 309.3kbits/s speed=1.45x
frame= 126 fps= 24 q=-1.0 size= 256kB time=00:00:07.37 bitrate= 284.4kbits/s speed=1.41x
frame= 135 fps= 23 q=-1.0 size= 256kB time=00:00:07.85 bitrate= 266.9kbits/s speed=1.36x
这些日志持续在打印,表示ffmpeg已经启动了存储功能。
- 我们在手机端停止推送,然后可看到服务器端打印如下日志,表示mp4文件存储成功。
[EasyDarwin] 2018/11/17 15:13:30 rtsp-server.go:183: session[pusher][TCP][/439622.sdp][VJ4vgafiR] end, now pusher size[0]
[EasyDarwin] 2018/11/17 15:13:30 rtsp-server.go:104: prepare to SIGTERM to process:&{11662 0 0 {{0 0} 0 0 0 0}}
[EasyDarwin] 2018/11/17 15:13:30 rtsp-server.go:109: delete ffmpeg from pull stream from pusher[session[pusher][TCP][/439622.sdp][VJ4vgafiR]]
frame= 4864 fps= 18 q=-1.0 Lsize= 15348kB time=00:04:21.37 bitrate= 481.0kbits/s speed=0.97x
video:14730kB audio:524kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.615662%
Exiting normally, received signal 15.
[EasyDarwin] 2018/11/17 15:13:40 pusher.go:258: session[player][UDP][/439622.sdp][Y-4vgaBig] end, now player size[0]
/Users/apple/Downloads/EasyDarwinGoMP4439622.sdp
至此,EasyDarwin的录像存储功能已经给大家介绍完了。EasyDarwin是一款开源的RTSP流媒体服务器。我们后续将会持续迭代,增加一些有趣的功能,比如录像查询与回放等等。欢迎大家关注和使用,地址:https://github.com/EasyDarwin/EasyDarwin