This is a series of articles. Follow the link here to get an overview over all articles.
Previously
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
-hls_segment_filename stream_%v/data%06d.ts
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.
-use_localtime_mkdir 1
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.
Finally
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
hi,
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.
https://www.martin-riedl.de/2020/01/03/add-a-logo-to-a-video-using-ffmpeg/
Your whole tutorial looks very interesting, thanks for putting this together! Will test it as sokn as possible.
Thanks. 🙂
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.