{"id":915,"date":"2023-11-10T19:50:43","date_gmt":"2023-11-10T19:50:43","guid":{"rendered":"https:\/\/james-batchelor.com\/?p=915"},"modified":"2023-11-10T19:51:12","modified_gmt":"2023-11-10T19:51:12","slug":"install-mediamtx-on-raspbian-bookworm","status":"publish","type":"post","link":"https:\/\/james-batchelor.com\/index.php\/2023\/11\/10\/install-mediamtx-on-raspbian-bookworm\/","title":{"rendered":"Install MediaMTX on Raspbian Bookworm"},"content":{"rendered":"\n<p>For a number of years, I\u2019ve been using MotionEyeOS on my CCTV cameras, exclusively for the \u201cFast Network Camera\u201d mode that enables RTSP for low bandwidth ingress to the MotionEye (running on CentOS) while maintaining good image quality.<\/p>\n\n\n\n<p>Finding more uses for these Pi\u2019s necessitates moving to Raspbian with MotionEye as the OS version is very bare bones by. Moving from the OS also means losing the Fast Network Camera and streams on the network jump from 2 Mbps to 25 Mbps.<\/p>\n\n\n\n<p>Over multiple camera\u2019s this really adds up, so I need a way replicate the low bandwidth, high quality streams with the versatility of Raspbian. The solution found is to install MediaMTX (formally rtsp-simple-server) \u2026<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/20231109_141557.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1949\" height=\"1097\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/20231109_141557.jpg\" alt=\"\" class=\"wp-image-918\"\/><\/a><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p>This guide is based on a Raspberry Pi 3 board and using a Pi Camera Module v2.1. The micro SD card has a new install of Raspbian Bookworm 32bit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install<\/h2>\n\n\n\n<p>Logging in via SSH, run an update to have the latest repo\u2019s available&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update<\/code><\/pre>\n\n\n\n<p>Using MediaMTX with the Raspberry Pi camera module requires a couple of libraries, install or upgrade them<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install libfreetype6 libcamera0<\/code><\/pre>\n\n\n\n<p>MediaMTX can now be downloaded. Which version you use depends on Raspbian version and Pi used. For 32bit versions of Raspbian use the \u201carmv7\u201d variant, and \u201carm4\u201d for 64bit. To check which one is running&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uname -m<\/code><\/pre>\n\n\n\n<p>32bit versions will respond with armv71 and 64bit with arm64.<\/p>\n\n\n\n<p>Visit <a href=\"https:\/\/github.com\/bluenviron\/mediamtx\/releases\">https:\/\/github.com\/bluenviron\/mediamtx\/releases<\/a> for latest versions and download links.<\/p>\n\n\n\n<p>Copy the link and enter in the terminal with wget prefix, below example I\u2019m using a 32bit version\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget <a href=\"https:\/\/github.com\/bluenviron\/mediamtx\/releases\/download\/v1.2.1\/mediamtx_v1.2.1_linux_armv7.tar.gz\">https:\/\/github.com\/bluenviron\/mediamtx\/releases\/download\/v1.2.1\/mediamtx_v1.2.1_linux_armv7.tar.gz<\/a><\/code><\/pre>\n\n\n\n<p>Extract the files&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar xzvf mediamtx_v1.2.1_linux_armv7.tar.gz<\/code><\/pre>\n\n\n\n<p>Of the files extracted, mediamtx is the binary and mediamtx.yml contains its configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Config and Test<\/h2>\n\n\n\n<p>Firstly edit the yml file so it uses the Pi camera<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano mediamtx.yml<\/code><\/pre>\n\n\n\n<p>Scroll to the bottom of the file, <strong>replace<\/strong> the following lines of code at the end of the file\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>paths:<br>&nbsp; # example:<br>&nbsp; # my_camera:<br>&nbsp; #&nbsp;&nbsp; source: rtsp:\/\/my_camera<br>&nbsp; # Settings under path \"all_others\" are applied to all paths that<br>&nbsp; # do not match another entry.<br>&nbsp; all_others:<\/code><\/pre>\n\n\n\n<p>With the following&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>paths:<br>&nbsp; cam:<br>&nbsp;&nbsp;&nbsp; source: rpiCamera<br>&nbsp;&nbsp;&nbsp; rpiCameraWidth: 1280<br>&nbsp;&nbsp;&nbsp; rpiCameraHeight: 720<br>&nbsp;&nbsp;&nbsp; rpiCameraVFlip: true<br>&nbsp;&nbsp;&nbsp; rpiCameraHFlip: true<br>&nbsp;&nbsp;&nbsp; rpiCameraBitrate: 1500000<\/code><\/pre>\n\n\n\n<p>Notes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>YML files require the indentation.<\/li>\n\n\n\n<li>cam: &#8211; this will be included in the path when accessing the stream.<\/li>\n\n\n\n<li>rpiCamera\u2026 &#8211; These are extra settings that can be specified, for example the VFlip and HFlip are due the camera being mounted upside down, a full list of options are included further up in the file.<\/li>\n<\/ul>\n\n\n\n<p>I also took this opportunity to disable all other protocols, just leaving RTSP on. When finished editing, Ctrl O and Ctrl X to same and exit.<\/p>\n\n\n\n<p>To test this config, run the binary\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/mediamtx<\/code><\/pre>\n\n\n\n<p>While running, this can be tested using VLC on another computer on the local network, open a network stream and enter the following, replacing {ip address} with the IP of the PI\u2026.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-10-190357-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-10-190357-1.png\" alt=\"\" class=\"wp-image-919\" width=\"433\" height=\"237\"\/><\/a><\/figure><\/div>\n\n\n<p>All well and the camera output appears.<\/p>\n\n\n\n<p>Use Ctrl C to stop the program, as we can now add it as a service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add as Service<\/h2>\n\n\n\n<p>To allow it to start automatically and for easier control of it the program can be created as service. To start let\u2019s move the files to a safer directory\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir \/opt\/mediamtx<br>sudo cp mediamtx \/opt\/mediamtx\/<br>sudo cp mediamtx.yml \/opt\/mediamtx\/<\/code><\/pre>\n\n\n\n<p>Create a new service file for editing&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/systemd\/system\/mediamtx.service<\/code><\/pre>\n\n\n\n<p>and add the following\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]&nbsp;<br>Wants=network.target<br>&#91;Service]&nbsp;<br>ExecStart=\/opt\/mediamtx\/mediamtx \/opt\/mediamtx\/mediamtx.yml<br>&#91;Install]&nbsp;<br>WantedBy=multi-user.target<\/code><\/pre>\n\n\n\n<p>Ctrl O and Ctrl X to same and exit.<\/p>\n\n\n\n<p>As it\u2019s a new service file, need to reload systemctl&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload<\/code><\/pre>\n\n\n\n<p>Now can start the service and enable at the same time so it starts when the Pi starts&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable --now mediamtx<\/code><\/pre>\n\n\n\n<p>To check its running, use the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status mediamtx<\/code><\/pre>\n\n\n\n<p>Should get the following&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-141316.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1655\" height=\"463\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-141316.png\" alt=\"\" class=\"wp-image-920\"\/><\/a><\/figure><\/div>\n\n\n<p>Now you can access the stream again via VLC, or add as a network camera to a MotionEye setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Results<\/h2>\n\n\n\n<p>To goal of this exercise was to reduce bandwidth on the network and return to similar levels observed when using MotionEyeOS while using a Raspbian OS.<\/p>\n\n\n\n<p>Below are iftop comparisons while a camera stream is open and at 1280 x 720.<\/p>\n\n\n\n<p>Using MotionEye, with http connection to a MotionEye server:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-142333.png\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"82\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-142333.png\" alt=\"\" class=\"wp-image-921\"\/><\/a><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>Using MediaMTX streaming via RTSP to VLC:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-142229.png\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"118\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-09-142229.png\" alt=\"\" class=\"wp-image-922\"\/><\/a><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Considerations<\/h2>\n\n\n\n<p>While searching for a solution I tried StreamEye, a simple program written by the same developer as MotionEye. <\/p>\n\n\n\n<p>While a very easy program to get up and running and it offered features close to &#8220;Fast Network Camera&#8221; on MotionEyeOS, the pure MJPEG stream resulted in approx. 40Mbps bandwidth utilisation on a 720p stream.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a number of years, I\u2019ve been using MotionEyeOS on my CCTV cameras, exclusively for the \u201cFast Network Camera\u201d mode that enables RTSP for low bandwidth ingress to the MotionEye (running on CentOS) while maintaining good image quality. Finding more uses for these Pi\u2019s necessitates moving to Raspbian with MotionEye as the OS version is &hellip; <a href=\"https:\/\/james-batchelor.com\/index.php\/2023\/11\/10\/install-mediamtx-on-raspbian-bookworm\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Install MediaMTX on Raspbian Bookworm&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212,4],"tags":[180,376,374,375,63,378,377],"class_list":["post-915","post","type-post","status-publish","format-standard","hentry","category-network","category-raspberry-pi","tag-camera","tag-convert","tag-mediamtx","tag-motioneyeos","tag-raspberry-pi","tag-raspbian","tag-rtsp-simple-server"],"_links":{"self":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/comments?post=915"}],"version-history":[{"count":4,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":925,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/915\/revisions\/925"}],"wp:attachment":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/media?parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/categories?post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/tags?post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}