Skip to main content

Convert RTSP to HLS using FFmpeg

TST, Hong Kong

Convert RTSP to HLS using FFmpeg

FFmpeg supports convert the RTSP streaming to HLS HTTP Live Streaming. Based on standard HTTP transactions, HTTP Live Streaming can traverse any firewall or proxy server that lets through standard HTTP traffic, unlike UDP-based protocols such as RTP. This also allows content to be offered from conventional HTTP servers.

Start by creating a file index.html and embed the soon to be created stream:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HLS Stream</title>
</head>
<body>
<video id="video" autoplay="true" controls="controls">
<source src="/stream/index.m3u8" type="application/x-mpegURL" />
Your browser does not support HTML5 streaming!
</video>
</body>
</html>

Create the stream folder mkdir stream, install FFMPEG, e.g. apt install ffmpeg:

ffmpeg -version
ffmpeg version n5.1.2 Copyright (c) 2000-2022 the FFmpeg developers

and for an IP camera stream:

INSTAR 2k+ WQHD Cameras

rtsp://admin:instar@192.168.2.120:554/livestream/12

run the following command:

ffmpeg -fflags nobuffer \
-loglevel debug \
-rtsp_transport tcp \
-i rtsp://admin:instar@192.168.2.120:554/livestream/11 \
-vsync 0 \
-copyts \
-vcodec copy \
-movflags frag_keyframe+empty_moov \
-an \
-hls_flags delete_segments+append_list \
-f hls \
-hls_time 1 \
-hls_list_size 3 \
-hls_segment_type mpegts \
-hls_segment_filename './stream/%d.ts' \
./stream/index.m3u8

INSTAR Full HD Cameras

ffmpeg -fflags nobuffer \
-loglevel debug \
-rtsp_transport tcp \
-i rtsp://admin:instar@192.168.2.31:554/11 \
-vsync 0 \
-copyts \
-vcodec copy \
-movflags frag_keyframe+empty_moov \
-an \
-hls_flags delete_segments+append_list \
-f hls \
-hls_time 1 \
-hls_list_size 3 \
-hls_segment_type mpegts \
-hls_segment_filename './stream/%d.ts' \
./stream/index.m3u8

By starting a web server on port 3000 on a PC with the local IP 192.168.2.112 the stream will now be served on the path http://192.168.2.112:3000/stream/index.m3u8. You can test the stream using ffplay:

RTSP to HLS with FFMPEG

Note that the Content-Type: audio/x-mpegurl is not supported on desktop systems - I had to use Google Chrome on Android to display the video embedded in the HTML file:

RTSP to HLS with FFMPEG