본문 바로가기
Dev. Environment/Unclassified

How to broadcast RTSP with a webcam and configure a stream server with MediaMTX

by codingwalks.en 2024. 10. 23.
728x90
반응형

Hello. This is codingwalks.

When learning computer vision or image processing, there are times when real-time video streaming is required. In particular, RTSP (Real-Time Streaming Protocol) is used for various purposes such as surveillance cameras, real-time broadcasting, and remote monitoring. In addition, software that acts as a server is required, and MediaMTX is a lightweight RTSP server that is easy to install and use, while also managing streams stably. This article explains step-by-step how to use MediaMTX to transmit a webcam RTSP stream and how to set up a client to access and view the video transmitted from the webcam. Since the implementation method is covered in both Linux and Windows environments, you can refer to it according to your environment.

 

1. What is RTSP?

RTSP is a network protocol used to control real-time media streaming. It can control multimedia streaming, including video and audio. RTSP can easily control streams between servers and clients, so it is widely used in surveillance systems, media servers, etc.

 

2. What is MediaMTX?

MediaMTX is an open source RTSP server developed in the Go language, and was previously known as rtsp-simple-server. It is a lightweight RTSP server, but it supports RTSP, RTMP, and HLS streaming, and can manage real-time video streams from webcams, etc.

Key features:

Support for RTSP and RTMP protocols
Support for web-based streaming via HLS
Video streaming possible by linking with FFmpeg
Clients can connect to RTSP streams and watch live video

 

3. Preparation

  • Webcam
  • FFmpeg (for streaming webcam streams)
  • MediaMTX RTSP server
  • Linux or Windows PC
  • Network connection

 

4. Install MediaMTX server

4.1 Install MediaMTX on Windows

1. Download the latest release from the MediaMTX GitHub page. (Instructions are based on v1.9.1)

 

GitHub - bluenviron/mediamtx: Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read,

Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy, record and playback video and audio streams. - bluenviron/mediamtx

github.com

2. Unzip the file and start the server by running the executable file (mediamtx.exe) from a command prompt.

C:\Users\codingwalks\Downloads\mediamtx_v1.9.1_windows_amd64>mediamtx.exe
2024/09/24 14:33:54 INF MediaMTX v1.9.1
2024/09/24 14:33:54 INF configuration loaded from C:\Users\codingwalks\Downloads\mediamtx_v1.9.1_windows_amd64\mediamtx.yml
2024/09/24 14:33:54 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2024/09/24 14:33:54 INF [RTMP] listener opened on :1935
2024/09/24 14:33:54 INF [HLS] listener opened on :8888
2024/09/24 14:33:54 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
2024/09/24 14:33:54 INF [SRT] listener opened on :8890 (UDP)

4.2 Installing MediaMTX on Linux

1. First, open a terminal and install MediaMTX.

wget https://github.com/bluenviron/mediamtx/releases/download/v1.9.1/mediamtx_v1.9.1_linux_amd64.tar.gz
tar -xzf mediamtx_v1.9.1_linux_amd64.tar.gz
cd mediamtx_linux_amd64
sudo ./mediamtx

When the server is running, MediaMTX handles RTSP streams on port 8554 by default.

 

5. MediaMTX Configuration

MediaMTX works with very basic configurations, but you can customize the details through a configuration file. The default configuration file is mediamtx.yml, located in the folder where MediaMTX is running.

• Example configuration file (mediamtx.yml):

rtspAddress: 8554
rtmpAddress: 1935
hls: yes
hlsSegmentCount: 7
hlsSegmentDuration: 6s
hlsSegmentMaxSize: 50M

 

Set RTSP port to 8554
Set RTMP port to 1935
Enable HLS streaming, keep 7 segments, and set each segment to 6 seconds long

 

6. Stream webcam to MediaMTX using FFmpeg

You can stream live from your webcam directly to a MediaMTX server using FFmpeg. FFmpeg works on a variety of platforms, so it works on both Windows and Linux.

6.1 Install FFmpeg (Windows)

1. Download FFmpeg for Windows from the official FFmpeg website. The author downloaded and used ffmpeg-release-full.7z from the Windows builds from gyan.dev.

FFmpeg official homepage

 

Download FFmpeg

If you find FFmpeg useful, you are welcome to contribute by donating. More downloading options Git Repositories Since FFmpeg is developed with Git, multiple repositories from developers and groups of developers are available. Release Verification All FFmpe

ffmpeg.org

Download ffmpeg-release-full.7z

 

Builds - CODEX FFMPEG @ gyan.dev

FFmpeg is a widely-used cross-platform multimedia framework which can process almost all common and many uncommon media formats. It has over 1000 internal components to capture, decode, encode, modify, combine, stream media, and it can make use of dozens o

www.gyan.dev

2. After downloading, unzip it and add the path of the ffmpeg.exe file in the bin folder to the environment variables.

• How to set environment variables:

a. Go to My PC → Properties → Advanced System Settings → Environment Variables
b. Select Path in System Variables and edit
c. Add the path to the bin folder of FFmpeg and confirm

To check if FFmpeg is installed, type the following command in cmd (command prompt).

ffmpeg -version

 

6.2 Start webcam RTSP stream (Windows)

You can also use FFmpeg to stream webcam video to RTSP in Windows. First, you need to check the connected webcam device.

1. To check the webcam, use the command below.

ffmpeg.exe -list_devices true -f dshow -i dummy

2. Now run the FFmpeg command to stream the webcam to RTSP.

ffmpeg.exe -f dshow -i video="webcam device name" -vcodec libx264 -f rtsp rtsp://[IP address]:[port]/stream
-f dshow : Get webcam input via DirectShow on Windows
-i video="webcam device name" : Specify the device name (enter the webcam name you found in the command)
-vcodec libx264 : Encode using H.264 video codec
-f rtsp : Specify output format as RTSP
rtsp://[IP address]:[port]/stream : RTSP address to broadcast

6.3 Installing FFmpeg (Linux)

sudo apt update
sudo apt install ffmpeg

6.4 Start the webcam RTSP stream (Linux)

To stream a webcam to RTSP with FFmpeg, check the webcam device file and then run the stream command.

ffmpeg -f v4l2 -i /dev/video0 -vcodec libx264 -f rtsp rtsp://[IP address]:[port]/stream

6.5 Additional Settings

• Adjust video resolution: You can set the resolution to 720p by adding it to the command.

-s 1280x720

• Add Audio: If you want to capture audio from your webcam's microphone, you can add the following options:

-f dshow -i audio="microphone device name"

 

7. Checking the RTSP stream on the client

To check the RTSP stream, you can use software such as VLC Media Player.

1. After downloading VLC Media Player from https://www.videolan.org/, run VLC.

VLC official website

 

VLC: 공식 사이트 - 모든 OS의 무료 멀티미디어 솔루션! - VideoLAN

2024-06-10 VideoLAN and the VLC team are publishing the 3.0.21 release of VLC today, which is the 22nd update to VLC's 3.0 branch: it updates codecs, adds Super Resolution and VQ Enhancement filtering with AMD GPUs, NVIDIA TrueHDR to generate a HDR represe

www.videolan.org

2. Go to Media > Open Network Stream, then enter rtsp://[IP address]:[port]/stream to play.

You can check live webcam stream through VLC.

 

8. HLS Streaming Settings

MediaMTX supports not only RTSP but also HLS (HTTP Live Streaming). With HLS, you can view live streams on your web browser.

8.1 HLS Activation

Enable the HLS option in the mediamtx.yml file. (See 4. MediaMTX Settings above.)

If MediaMTX supports HLS streaming, you can view live streams by accessing an address such as http://[IP address]:8888/stream/index.m3u8 on your web browser.

 

9. Conclusion

In this article, we have explained how to use MediaMTX and FFmpeg to broadcast webcams to RTSP in Windows and Linux environments and check it in real time on the client. MediaMTX provides simple but powerful features and supports various streaming protocols such as RTMP and HLS in addition to RTSP. RTSP is a standard protocol for live streaming and is suitable for use in various applications such as surveillance cameras, live broadcasting, and remote monitoring. You can optimize the settings according to your network conditions and requirements to create a stable streaming environment. Adjust the settings to suit your environment and create the best streaming quality.

 

If you found this post useful, please like and subscribe below. ^^

Buy me a coffee

 

[Codingwalks]에게 송금하기 - AQR

[Codingwalks]에게 송금하기 - AQR

aq.gy

728x90
반응형