Blog/content/posts/2023/webcam-to-rtsp.md

5.1 KiB
Raw Blame History

title date draft tags
📷 Преобразование видео с USB камеры в RTSP поток 2023-09-26T23:29:24+03:00 false
video
tools
tips

mediamtx

В качестве сервера будет использоваться mediamtx.

Из описания на GitHub:
Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy and record video and audio streams.

Сервер написан на Go, есть сборки для RaspberryPi. Хороший файл конфигурации.

Преобразовываем v4l2 в rtsp поток

В файле mediamtx.yml в самый конец (в параметр path) добавляем следующий код, где /dev/video0 это захватываемое устройство.

  cam:
    runOnInit: ffmpeg -f v4l2 -i /dev/video0 -pix_fmt yuv420p -preset ultrafast -b:v 600k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH
    runOnInitRestart: yes

Запускаем сервер и получаем такой выхлоп.

2023/09/26 23:35:32 INF MediaMTX v1.1.1
2023/09/26 23:35:32 INF [path cam] runOnInit command started
2023/09/26 23:35:32 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2023/09/26 23:35:32 INF [RTMP] listener opened on :1935
2023/09/26 23:35:32 INF [HLS] listener opened on :8888
2023/09/26 23:35:32 INF [WebRTC] listener opened on :8889 (HTTP)
2023/09/26 23:35:32 INF [SRT] listener opened on :8890 (UDP)
ffmpeg version n6.0 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13.2.1 (GCC) 20230801
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-amf --enable-avisynth --enable-cuda-llvm --enable-lto --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libjxl --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librav1e --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-nvdec --enable-nvenc --enable-opencl --enable-opengl --enable-shared --enable-version3 --enable-vulkan
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 9930.712260, bitrate: 147456 kb/s
  Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn
[out#0/rtsp @ 0x56233731f280] Codec AVOption preset (Encoding preset) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
2023/09/26 23:35:32 INF [RTSP] [conn [::1]:54944] opened
2023/09/26 23:35:32 INF [RTSP] [session 13c1b9af] created by [::1]:54944
2023/09/26 23:35:32 INF [RTSP] [session 13c1b9af] is publishing to path 'cam', 1 track (MPEG-4 Video)
Output #0, rtsp, to 'rtsp://localhost:8554/cam':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: mpeg4, yuv420p(tv, progressive), 640x480, q=2-31, 600 kb/s, 30 fps, 90k tbn
    Metadata:
      encoder         : Lavc60.3.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/600000 buffer size: 0 vbv_delay: N/A
frame=  546 fps= 31 q=7.0 size=N/A time=00:00:18.20 bitrate=N/A dup=281 drop=0 speed=1.03x   

Видим, что сервер запустил различчные протоколы: RTSP, RTMP, HLS, WebRTC, SRT.

INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
INF [RTMP] listener opened on :1935
INF [HLS] listener opened on :8888
INF [WebRTC] listener opened on :8889 (HTTP)
INF [SRT] listener opened on :8890 (UDP)

mediamtx запустил ffmpeg, который стримит видео из /dev/video0 в rtsp://localhost:8554/cam.

Input #0, video4linux2,v4l2, from '/dev/video0':
Output #0, rtsp, to 'rtsp://localhost:8554/cam':

Проверить можно с помощью любого проигрывателя, например mpv.

mpv rtsp://localhost:8554/cam --profile=low-latency --untimed