环境
- ubuntu 18.04 64bit
- ZLMediaKit 20210924
- ffmpeg 3.4.8
支持的http api
目前版本的 ZLMediaKit 支持以下 http api,这些接口都是支持 GET/POST 的方式
"/index/api/addFFmpegSource",
"/index/api/addStreamProxy",
"/index/api/close_stream",
"/index/api/close_streams",
"/index/api/delFFmpegSource",
"/index/api/delStreamProxy",
"/index/api/getAllSession",
"/index/api/getApiList",
"/index/api/getMediaList",
"/index/api/getServerConfig",
"/index/api/getThreadsLoad",
"/index/api/getWorkThreadsLoad",
"/index/api/kick_session",
"/index/api/kick_sessions",
"/index/api/restartServer",
"/index/api/setServerConfig",
"/index/api/isMediaOnline",
"/index/api/getMediaInfo",
"/index/api/getRtpInfo",
"/index/api/getMp4RecordFile",
"/index/api/startRecord",
"/index/api/stopRecord",
"/index/api/getRecordStatus",
"/index/api/getSnap",
"/index/api/openRtpServer",
"/index/api/closeRtpServer",
"/index/api/listRtpServer",
"/index/api/startSendRtp",
"/index/api/stopSendRtp",
"/index/api/getStatistic",
"/index/api/addStreamPusherProxy",
"/index/api/delStreamPusherProxy"
由于接口更新比较频繁,上面的接口可能会有更新,具体可以参考代码实现 https://github.com/xia-chu/ZLMediaKit/blob/master/server/WebApi.cpp#L386
http api使用示例
当我们启动 MediaServer 时,可以注意到 http api 接口默认已经启动

下面,我们拿 /index/api/getApiList 这个接口为例,它使用的是 GET 的方式。如果是在本地使用 http api, 那非常简单,可以直接在浏览器中输入地址 http://127.0.0.1/index/api/getApiList

如果是在其它主机上操作的话,接口会返回错误

从提示中可以看到,接口中需要添加参数 secret,也就是需要进行鉴权,这是出于安全的考虑,这个 secret 在配置文件 config.ini 中有定义,直接拷贝出来,跟在 /index/api/getApiList 后面

考虑到接口调试的便利性,推荐大家使用工具 Postman

最后,我们使用 http api 的方式来拉取一路 rtsp 摄像头视频流,来代替前面教程中使用的 ffmpeg 命令行推流。这里面对应的 api 是 /index/api/addFFmpegSource
-
参数:
参数 是否必选 释意 secret Y api操作密钥,如果操作ip是127.0.0.1,则不需要此参数 src_url Y FFmpeg拉流地址,支持任意协议或格式(只要FFmpeg支持即可) dst_url Y FFmpeg rtmp推流地址,一般都是推给自己,例如rtmp://127.0.0.1/live/stream_form_ffmpeg timeout_ms Y FFmpeg推流成功超时时间 enable_hls Y 是否开启hls录制 enable_mp4 Y 是否开启mp4录制 ffmpeg_cmd_key N FFmpeg命令参数模板,置空则采用配置项:ffmpeg.cmd
使用 Postman 进行请求

请求成功后,服务器就开始去拉取 rtsp 视频流了,然后打开播放器,就可以进行播放了

最后,如果要取消拉流的话,可以使用 /index/api/delFFmpegSource 接口进行关闭
