{"id":30,"date":"2024-11-24T00:00:00","date_gmt":"2024-11-24T00:00:00","guid":{"rendered":"https:\/\/www.avgator.com\/post\/rtsp-to-hls-repacking-service"},"modified":"2024-11-24T00:00:00","modified_gmt":"2024-11-24T00:00:00","slug":"rtsp-to-hls-repacking-service","status":"publish","type":"post","link":"https:\/\/content.avgator.com\/?p=30","title":{"rendered":"RTSP to HLS repacking service"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/RTSP-to-HLS-repacking-service-293e2128.avif\" alt=\"RTSP to HLS\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>RTSP (Real-Time Streaming Protocol) is widely used for live video streaming, especially in IP cameras and surveillance systems. However, RTSP isn&#8217;t natively supported by most web browsers, making it less versatile for modern applications. <\/p>\n\n\n\n<p>On the other hand, HLS (HTTP Live Streaming) is a browser-friendly protocol supported across devices and platforms. <\/p>\n\n\n\n<p>By leveraging FFmpeg and Nginx, you can efficiently repack RTSP streams into HLS for seamless streaming. Here&#8217;s how this powerful duo works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is FFmpeg?<\/h2>\n\n\n\n<p>FFmpeg is a robust multimedia framework capable of processing, converting, and streaming audio and video in various formats. It plays a crucial role in transforming RTSP streams into HLS playlists (.m3u8).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Nginx?<\/h2>\n\n\n\n<p>Nginx is a high-performance web server and reverse proxy that serves as the delivery mechanism for the HLS files generated by FFmpeg. It ensures efficient file hosting, supports cross-origin requests, and scales to handle many simultaneous viewers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How FFmpeg Helps Repack RTSP to HLS<\/h2>\n\n\n\n<p>FFmpeg repackages the RTSP stream into the HLS format without re-encoding, which saves computational resources and reduces latency.<\/p>\n\n\n\n<p>Use FFmpeg to process the RTSP input and generate HLS segments.<\/p>\n\n\n\n<p><strong>Example Command<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ffmpeg -i rtsp:\/\/admin:Multimedia@10.14.25.151:8554\/ch01 \\\n       -c:v copy -c:a copy -f hls -hls_time 1 -hls_list_size 6 \\\n       -hls_time 1 -hls_list_size 6 -hls_flags delete_segments \\\n       \/var\/www\/streams\/AV05SD082a.m3u8<\/code><\/pre>\n\n\n\n<p><strong>Key Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-c:v copy:Copies the video codec without re-encoding.<\/li>\n\n\n\n<li>-c:a copy:Copies the audio codec without re-encoding.<\/li>\n\n\n\n<li>-hls_time:Sets the duration (in seconds) of each HLS segment.<\/li>\n\n\n\n<li>-hls_list_size:Defines the number of segments in the playlist.<\/li>\n<\/ul>\n\n\n\n<p>FFmpeg continuously processes the RTSP stream and generates a live.m3u8 playlist and media segments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Nginx Serves HLS Streams<\/h2>\n\n\n\n<p>Nginx acts as the web server that delivers the HLS files to clients. Configure Nginx to serve HLS files with CORS support.<\/p>\n\n\n\n<p><strong>Configuration File Location:<\/strong> \/etc\/nginx\/sites-enabled\/strapi.conf<\/p>\n\n\n\n<p><strong>Sample Configuration<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen 80;\n    server_name 10.14.25.18;\n\n    location \/streams\/ {\n        types {\n            application\/vnd.apple.mpegurl m3u8;\n            video\/mp2t ts;\n        }\n        root \/var\/www;\n        add_header Cache-Control no-cache;\n        add_header Access-Control-Allow-Origin *;\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The types block specifies MIME types for .m3u8 and .ts files.<\/li>\n\n\n\n<li>The Access-Control-Allow-Origin * header supports cross-origin requests.<\/li>\n<\/ul>\n\n\n\n<p>Restart Nginx after configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart nginx<\/code><\/pre>\n\n\n\n<p>Clients can now access the HLS stream through a URL like: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><a href=\"http:\/\/10.14.25.18\/streams\/stream.m3u8.\">http:\/\/10.14.25.18\/streams\/stream.m3u8<\/a><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Repacking RTSP streams into HLS using FFmpeg and Nginx is an effective solution for delivering high-quality video streams to a wide range of devices. This setup ensures compatibility, scalability, and efficiency, making it ideal for applications like IP camera streaming, live broadcasting, or multimedia projects. <\/p>\n\n\n\n<p>With HLS support, <a href=\"https:\/\/avstudio.app\/\"><strong>AV Studio<\/strong><\/a> allows developers to create projects without the need for third-party plug-ins. Just integrate into your project AV Studio a video element that supports HLS playback.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RTSP to HLS repacking service<\/p>\n","protected":false},"author":1,"featured_media":4780,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[21],"class_list":["post-30","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integrations","tag-integrations"],"_links":{"self":[{"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/content.avgator.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":0,"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/content.avgator.com\/index.php?rest_route=\/wp\/v2\/media\/4780"}],"wp:attachment":[{"href":"https:\/\/content.avgator.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/content.avgator.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/content.avgator.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}