目录
前言
开局废话
软件环境
整体思路
详细方案
下载mediaMtx
配置mediamtx.yml
启动
API使用
查询所有路径:http://localhost:9997/v2/paths/list
添加一个名称为111的通道路径(不转码):http://192.168.1.227:9997/v2/config/paths/add/111
添加一个名称为222的通道路径(转码):http://192.168.1.227:9997/v2/config/paths/add/222
我最欣赏马斯克的一句话,我不在乎专利,专利只会抑制科技的发展。我找遍了所有教程都没有关于webrtc-streamer可以播放H265的rtsp视频流的方案,我摸索到了我就分享出来,不管你们怎么想,我希望你们看了我的贴子,并且收益的话,希望你们也可以像我一样,知识只有分享才能共同进步!
前端使用webrtc-streamer进行rtsp流播放,目前的webrtc-streamer只支持H264的视频,这在大多数场合都是适用的,但是在一些特殊的场合,相机端必须使用H265的编码格式,用来减少存储占用。这个时候我们要么等待webrtc-streamer的开源作者发布支持265的版本,要么就考虑自己转码成264来播放,我就属于后者。
目录结构如下:
打开mediamtx.yml进行配置,由于配置项比较多,我只截取关键配置:
开启api,因为我们需要通过代码进行配置,因此需要开启mediaMtx的API
api描述文档地址:MediaMTX API (bluenviron.github.io)
关闭rtsp:rtspDisable:no 注意这里是关闭为no 就是开启的意思。其他端口根据情况自行 配置
重点配置:
如果我们不用api进行paths配置,可以在这里进行配置,如果使用了api进行配置,这里默认 留一个all就行了,all的意思是当路径找不到匹配的key时,则全部按all配置。
我测试的时候使用这里配置,实际使用我使用api
转码,当我们需要转码时,则需要配置runOnInit,这里配置需要转码的命令,即调用ffmpeg进 行转码的命令。
我提供一个命令: "runOnInit": "ffmpeg -rtsp_transport tcp -i \"rtsp://admin:[email protected]:554/h264/ch1/main/av_stream\" -vcodec libx264 -preset:v ultrafast -r 25 -threads 4 -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH"
FFMPEG转码命令不做解释,自行百度,大致意思就是将一个rtsp流转码H264后输出新的rtsp流
代码层自行解决,我这里使用postman进行演示
默认返回值为空,说明流媒体还没有配置通道路径。
查看mediaMtx dos窗口:
我这个环境有点丢包,正常可以看到【path 111】【rtsp source】 started 则说明流媒体拉流成功
再次查询所有路径:不转码时 只关注source即可。
{
"itemCount": 1,
"pageCount": 1,
"items": [
{
"name": "111",
"confName": "111",
"conf": {
"source": "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream",
"sourceFingerprint": "",
"sourceOnDemand": false,
"sourceOnDemandStartTimeout": "10s",
"sourceOnDemandCloseAfter": "10s",
"publishUser": "",
"publishPass": "",
"publishIPs": [],
"readUser": "",
"readPass": "",
"readIPs": [],
"disablePublisherOverride": false,
"fallback": "",
"sourceProtocol": "automatic",
"sourceAnyPortEnable": false,
"rtspRangeType": "",
"rtspRangeStart": "",
"sourceRedirect": "",
"rpiCameraCamID": 0,
"rpiCameraWidth": 1920,
"rpiCameraHeight": 1080,
"rpiCameraHFlip": false,
"rpiCameraVFlip": false,
"rpiCameraBrightness": 0,
"rpiCameraContrast": 1,
"rpiCameraSaturation": 1,
"rpiCameraSharpness": 1,
"rpiCameraExposure": "",
"rpiCameraAWB": "",
"rpiCameraDenoise": "",
"rpiCameraShutter": 0,
"rpiCameraMetering": "",
"rpiCameraGain": 0,
"rpiCameraEV": 0,
"rpiCameraROI": "",
"rpiCameraTuningFile": "",
"rpiCameraMode": "",
"rpiCameraFPS": 30,
"rpiCameraIDRPeriod": 60,
"rpiCameraBitrate": 1000000,
"rpiCameraProfile": "main",
"rpiCameraLevel": "4.1",
"rpiCameraAfMode": "",
"rpiCameraAfRange": "",
"rpiCameraAfSpeed": "",
"rpiCameraLensPosition": 0,
"rpiCameraAfWindow": "",
"rpiCameraTextOverlayEnable": false,
"rpiCameraTextOverlay": "%Y-%m-%d %H:%M:%S - MediaMTX",
"runOnInit": "",
"runOnInitRestart": false,
"runOnDemand": "",
"runOnDemandRestart": false,
"runOnDemandStartTimeout": "10s",
"runOnDemandCloseAfter": "10s",
"runOnReady": "",
"runOnReadyRestart": false,
"runOnRead": "",
"runOnReadRestart": false
},
"source": {
"type": "rtspSource",
"id": ""
},
"sourceReady": false,
"tracks": [],
"bytesReceived": 0,
"readers": []
}
]
}
此时,可以使用mediaMtx地址加路径name可以预览播放
播放画面我就不展示了,不方便。
当使用vlc进行拉流时
dos下会有如下打印信息,mediaMtx会创建一个连接。
查询所有路径
{
"itemCount": 1,
"pageCount": 1,
"items": [
{
"name": "222",
"confName": "222",
"conf": {
"source": "publisher",
"sourceFingerprint": "",
"sourceOnDemand": false,
"sourceOnDemandStartTimeout": "10s",
"sourceOnDemandCloseAfter": "10s",
"publishUser": "",
"publishPass": "",
"publishIPs": [],
"readUser": "",
"readPass": "",
"readIPs": [],
"disablePublisherOverride": false,
"fallback": "",
"sourceProtocol": "automatic",
"sourceAnyPortEnable": false,
"rtspRangeType": "",
"rtspRangeStart": "",
"sourceRedirect": "",
"rpiCameraCamID": 0,
"rpiCameraWidth": 1920,
"rpiCameraHeight": 1080,
"rpiCameraHFlip": false,
"rpiCameraVFlip": false,
"rpiCameraBrightness": 0,
"rpiCameraContrast": 1,
"rpiCameraSaturation": 1,
"rpiCameraSharpness": 1,
"rpiCameraExposure": "",
"rpiCameraAWB": "",
"rpiCameraDenoise": "",
"rpiCameraShutter": 0,
"rpiCameraMetering": "",
"rpiCameraGain": 0,
"rpiCameraEV": 0,
"rpiCameraROI": "",
"rpiCameraTuningFile": "",
"rpiCameraMode": "",
"rpiCameraFPS": 30,
"rpiCameraIDRPeriod": 60,
"rpiCameraBitrate": 1000000,
"rpiCameraProfile": "main",
"rpiCameraLevel": "4.1",
"rpiCameraAfMode": "",
"rpiCameraAfRange": "",
"rpiCameraAfSpeed": "",
"rpiCameraLensPosition": 0,
"rpiCameraAfWindow": "",
"rpiCameraTextOverlayEnable": false,
"rpiCameraTextOverlay": "%Y-%m-%d %H:%M:%S - MediaMTX",
"runOnInit": "ffmpeg -rtsp_transport tcp -i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream -vcodec libx264 -preset:v ultrafast -r 25 -threads 4 -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH",
"runOnInitRestart": true,
"runOnDemand": "",
"runOnDemandRestart": false,
"runOnDemandStartTimeout": "10s",
"runOnDemandCloseAfter": "10s",
"runOnReady": "",
"runOnReadyRestart": false,
"runOnRead": "",
"runOnReadRestart": false
},
"source": null,
"sourceReady": false,
"tracks": [],
"bytesReceived": 20054902,
"readers": []
}
]
}
mediaMtx DOS页面可以看到开始调用ffmpeg进行转码
至此 使用rtsp://192.168.1.227:8554/222,就可以预览了。
使用vlc可以进行预览,当然你使用webrtc-streamer 也是同理,只不过webrtc-streamer就不能直接使用相机的rtsp地址了。
这里有个细节忘记说了,ffmpeg需要配置环境变量,这里各位自行百度。否则mediaMtx使用命令无效。