This is a series of articles. Follow the link here to get an overview over all articles.
on Using FFmpeg as a HLS streaming server (Part 4) – Multiple Video Resolutions.
The command until now looks like the following:
./ffmpeg -listen 1 -i rtmp://martin-riedl.de/stream01 \ -filter_complex "[v:0]split=2[vtemp001][vout002];[vtemp001]scale=w=960:h=540[vout001]" \ -preset veryfast -g 25 -sc_threshold 0 \ -map [vout001] -c:v:0 libx264 -b:v:0 2000k \ -map [vout002] -c:v:1 libx264 -b:v:1 6000k \ -map a:0 -map a:0 -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 4 -hls_playlist_type event \ -master_pl_name master.m3u8 \ -var_stream_map "v:0,a:0 v:1,a:1" stream_%v.m3u8
But all stream variants are placed in one folder. We want to do here a custom folder structure.
Data Folder per Stream Variant
This parameters sets the name of the segment file. In my case a folder name is added named “stream_%v”. %v is the stream variant. The %06d in the file name “data%6d.ts” is a formatted number. 06 means fill the number up with 0 up to 6 digits.
And the result looks great:
But when we try to watch the stream, no video is playing. Hmm, why? A look in the stream_0.m3u8 explains it:
The path for the data.ts files is missing. So the files are not found by the player.
Segment Folder Declaration
For this a base-url must be added to each line of data.ts.
This can be done by easily adding the line above. The documentation tells us following:
Used […] it will create all subdirectories which is expanded in filename.
Now the folder path is added correctly in the segment playlist.
Now the full command looks like this:
./ffmpeg -listen 1 -i rtmp://martin-riedl.de/stream01 \ -filter_complex "[v:0]split=2[vtemp001][vout002];[vtemp001]scale=w=960:h=540[vout001]" \ -preset veryfast -g 25 -sc_threshold 0 \ -map [vout001] -c:v:0 libx264 -b:v:0 2000k \ -map [vout002] -c:v:1 libx264 -b:v:1 6000k \ -map a:0 -map a:0 -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 4 -hls_playlist_type event \ -master_pl_name master.m3u8 \ -hls_segment_filename stream_%v/data%06d.ts \ -use_localtime_mkdir 1 \ -var_stream_map "v:0,a:0 v:1,a:1" stream_%v.m3u8
the command is working and is creating files but when i try to play the master file with vlc it does not start.
are you use url for m3u file?
Make sure that the files are available over HTTP(s). Then use the URL to the m3u8 file for the VLC and choose “Open Network”.
Hello. What is the way to add a logo to HLS Stream?
Have made a short article about how to add a logo to the input video. This can be easily combined with the command in this article.
Your whole tutorial looks very interesting, thanks for putting this together! Will test it as sokn as possible.
Hi Martin, awesome tutorial man…loved it…working great… but just want to know how to put this entire thing in a folder….I mean, like, master.m3u8, stream folders and stream m3u8 files…all these in a folder
You mean like it is in the first command of this article? Just make sure that you don’t use a folder in the parameter hls_segment_filename.
Thank you for sharing !
Really usefull .
instead of adding options in the ffmpeg command line for creating subfolder,
and you are using nginx for streaming, you could add this option in your nginx.conf (in the application block) =>
# on|off. In this mode a subdirectory of hls_path is created for each stream. Default=off.
my 2 cents.
The free / open source version of NGINX doesn’t support HLS. You are talking about a additional module for that must be installed / compiled.
If you use the RTMP module of arut (https://github.com/arut/nginx-rtmp-module) then you need also FFmpeg for additional features like multiple bitrates or resolutions.
about “If you use the RTMP module of arut (https://github.com/arut/nginx-rtmp-module) then you need also FFmpeg for additional features like multiple bitrates or resolutions.”
Sorry my message wasn’t clear, yes I’m using this module, and of course I need ffmpeg.
we all need ffmpeg 😉
What if we have multiple audio how can we make it work
Check out part 9: https://www.martin-riedl.de/2020/05/31/using-ffmpeg-as-a-hls-streaming-server-part-9-multiple-audio-languages/
Thanks for this suburb list of tutorials!
In my ffmpeg (5.0.1 on MacOS with brew) the use_localtime_mkdir option is not recognized.
However, with [-var_stream_map “v:0,a:0 v:1,a:1” stream_%v/index.m3u8] the same folder structure can be realized
thanks and keep up the good work!
How do I add base url to every entry in master playlist? I tried the hls_base_url and segment_list_entry_prefix options but it only adds the value to the playlist of the different variants and not that of the master playlist. Is there an option or way to do? Or do I have to temper with the master playlist after it has been generated?