{"id":1720,"date":"2024-01-10T23:20:53","date_gmt":"2024-01-11T04:20:53","guid":{"rendered":"https:\/\/cd34.com\/blog\/?p=1720"},"modified":"2024-01-11T18:01:19","modified_gmt":"2024-01-11T23:01:19","slug":"live-streaming-from-a-raspberry-pi-via-nginx","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/webserver\/live-streaming-from-a-raspberry-pi-via-nginx\/","title":{"rendered":"Live Streaming from a Raspberry Pi via Nginx"},"content":{"rendered":"\n<p>I had a Raspberry Pi 3B with the PiCam 2 that stopped working.  To make the video better, I needed a shroud to prevent picking up light in the room when it was dark outside and ultimately, three PiCam 2&#8217;s died while working with that shroud. I ordered a PiCam 3 the other day to see if I could get it to work with my Pi 3B, and alas, it wouldn&#8217;t work. So, I used a 2GB Raspberry Pi 4.<\/p>\n\n\n\n<p>To set up streaming in nginx:<\/p>\n\n\n\n<p>In your main nginx.conf you need:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rtmp {\n    server {\n        listen some_ip_address:1935;\n        application live {\n            live on;\n            interleave on;\n\n            hls on;\n            hls_path \/tmp\/hls;\n            hls_fragment 5s;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Then in your site config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    location \/live\/ {\n        alias \/tmp\/hls\/;\n    }<\/code><\/pre>\n\n\n\n<p>Your Raspberry Pi should run a recent image. You&#8217;ll need to install <strong>rpicam-apps<\/strong> to replace <strong>rpicam-apps-lite<\/strong> which is installed by default:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install rpicam-apps<\/code><\/pre>\n\n\n\n<p>The script you&#8217;ll create a script called livevid:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ndate\n\nwhile &#91; 1 == 1 ]; do\n    rpicam-vid -v 0 -n -t 0 -g 10 --bitrate 2500000 --inline --width 960 --height 540 --framerate 30 --codec libav --libav-format flv --libav-audio 0 --audio-bitrate 8600 --av-sync 200000 -n -o 'rtmp:\/\/url_of_nginx\/live\/cam' --awb daylight\n\ndone;<\/code><\/pre>\n\n\n\n<p>And your crontab entry (crontab -e)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@reboot cd \/home\/user;\/usr\/bin\/nohup \/home\/user\/livevid &amp;<\/code><\/pre>\n\n\n\n<p>With this, you can at least alter the parameters to make the image quality better, or use less bandwidth.<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/live-streaming-from-a-raspberry-pi-via-nginx\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>I had a Raspberry Pi 3B with the PiCam 2 that stopped working. To make the video better, I needed a shroud to prevent picking up light in the room when it was dark outside and ultimately, three PiCam 2&#8217;s died while working with that shroud. I ordered a PiCam 3 the other day to [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/live-streaming-from-a-raspberry-pi-via-nginx\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1720","post","type-post","status-publish","format-standard","hentry","category-webserver"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":1,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1721,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1720\/revisions\/1721"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}