How do I create an HLS master playlist with ffmpeg? - bash

There is a command that generates hls videos in different sizes.
Now I need to merge them into one master playlist. (What is a master playlist) How do I do this? What and where do I need to add in this command?
ffmpeg -hide_banner -y -i input.mp4 \
-vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename output/360p_%03d.ts output/360p.m3u8 \
-vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename output/480p_%03d.ts output/480p.m3u8 \
-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename output/720p_%03d.ts output/720p.m3u8 \
-vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename output/1080p_%03d.ts output/1080p.m3u8
What I am interested in is the automatic creation of a master playlist using ffmpeg in this command.
UPD: I answered my own question
After several attempts, I still got the desired result. I modified the command and now it looks like this
ffmpeg -hide_banner -re -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -c:a aac -ar 48000 \
-filter:v:0 scale=w=640:h=360:force_original_aspect_ratio=decrease -maxrate:v:0 856k -bufsize:v:0 1200k -b:a:0 96k \
-filter:v:1 scale=w=842:h=480:force_original_aspect_ratio=decrease -maxrate:v:1 1498k -bufsize:v:1 2100k -b:a:1 128k \
-filter:v:2 scale=w=1280:h=720:force_original_aspect_ratio=decrease -maxrate:v:2 2996k -bufsize:v:2 4200k -b:a:2 128k \
-filter:v:3 scale=w=1920:h=1080:force_original_aspect_ratio=decrease -maxrate:v:3 5350k -bufsize:v:3 7500k -b:a:3 192k \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 v:3,a:3" \
-hls_time 4 \
-hls_list_size 0 \
-master_pl_name master.m3u8 \
-hls_segment_filename output/%v_%03d.ts output/%v.m3u8

After several attempts, I still got the desired result. I modified the command and now it looks like this
ffmpeg -hide_banner -re -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -c:a aac -ar 48000 \
-filter:v:0 scale=w=640:h=360:force_original_aspect_ratio=decrease -maxrate:v:0 856k -bufsize:v:0 1200k -b:a:0 96k \
-filter:v:1 scale=w=842:h=480:force_original_aspect_ratio=decrease -maxrate:v:1 1498k -bufsize:v:1 2100k -b:a:1 128k \
-filter:v:2 scale=w=1280:h=720:force_original_aspect_ratio=decrease -maxrate:v:2 2996k -bufsize:v:2 4200k -b:a:2 128k \
-filter:v:3 scale=w=1920:h=1080:force_original_aspect_ratio=decrease -maxrate:v:3 5350k -bufsize:v:3 7500k -b:a:3 192k \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 v:3,a:3" \
-hls_time 4 \
-master_pl_name master.m3u8 \
-hls_segment_filename output/%v_%03d.ts output/%v.m3u8

In case anyone facing an issue with the above command.
ffmpeg -hide_banner -re -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -c:a aac -ar 48000 -filter:v:0 scale=w=640:h=360:force_original_aspect_ratio=decrease -maxrate:v:0 856k -bufsize:v:0 1200k -b:a:0 96k -filter:v:1 scale=w=960:h=540:force_original_aspect_ratio=decrease -maxrate:v:1 1498k -bufsize:v:1 2100k -b:a:1 128k -filter:v:2 scale=w=1280:h=720:force_original_aspect_ratio=decrease -maxrate:v:2 2996k -bufsize:v:2 4200k -b:a:2 128k -filter:v:3 scale=w=1920:h=1080:force_original_aspect_ratio=decrease -maxrate:v:3 5350k -bufsize:v:3 7500k -b:a:3 192k -var_stream_map "v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:540p v:3,a:3,name:360p" -master_pl_name master.m3u8 -f hls -hls_time 10 -hls_playlist_type vod -hls_list_size 0 -hls_segment_filename "v%v/segment%d.ts" v%v/index.m3u8
Full text-
ffmpeg -hide_banner -re -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:v:0
-map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -c:a aac -ar 48000 -filter:v:0 scale=w=640:h=360:force_original_aspect_ratio=decrease -maxrate:v:0
856k -bufsize:v:0 1200k -b:a:0 96k -filter:v:1
scale=w=960:h=540:force_original_aspect_ratio=decrease -maxrate:v:1
1498k -bufsize:v:1 2100k -b:a:1 128k -filter:v:2
scale=w=1280:h=720:force_original_aspect_ratio=decrease -maxrate:v:2
2996k -bufsize:v:2 4200k -b:a:2 128k -filter:v:3
scale=w=1920:h=1080:force_original_aspect_ratio=decrease -maxrate:v:3
5350k -bufsize:v:3 7500k -b:a:3 192k -var_stream_map
"v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:540p
v:3,a:3,name:360p" -master_pl_name master.m3u8 -f hls -hls_time 10
-hls_playlist_type vod -hls_list_size 0 -hls_segment_filename "v%v/segment%d.ts" v%v/index.m3u8

Related

How do I enable gpu acceleration in ffmpeg?

my code:
sudo ffmpeg -i rtsp://rtsp.stream/pattern \
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v h264_cuvid -crf 22 -c:a aac -ar 48000 \
-filter:v:0 scale=w=480:h=360 -maxrate:v:0 600k -b:a:0 500k \
-filter:v:1 scale=w=640:h=480 -maxrate:v:1 1500k -b:a:1 1000k \
-filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
-var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
-preset fast -hls_list_size 10 -threads 0 -f hls \
-hls_time 3 -hls_flags independent_segments \
-master_pl_name "livestream.m3u8" \
-y -vsync 0 -hwaccel cuda "/var/www/livestream-%v.m3u8"
Option hwaccel (use HW accelerated decoding) cannot be applied to output url /var/www/livestream-%v.m3u8 -- you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to.
Error parsing options for output file /var/www/livestream-%v.m3u8.
It doesn't work with the above error.
working cpu code:
sudo ffmpeg -i rtsp://rtsp.stream/pattern \
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v libx264 -crf 22 -c:a aac -ar 48000 \
-filter:v:0 scale=w=480:h=360 -maxrate:v:0 600k -b:a:0 500k \
-filter:v:1 scale=w=640:h=480 -maxrate:v:1 1500k -b:a:1 1000k \
-filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
-var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
-preset fast -hls_list_size 10 -threads 0 -f hls \
-hls_time 3 -hls_flags independent_segments \
-master_pl_name "livestream.m3u8" \
-y "/var/www/livestream-%v.m3u8"
The above operates as a cpu and annoys my cpu. I don't know how to write the command.=
Any ideas?

Multiple bitrates/resolutions and creating HLS playlist issue

When I run the command below, only the 1280x720 line works. The other two lines do not work. Where am I missing?
ffmpeg -i test.mp4 -s:v:0 1280x720 -c:a aac -c:v:0 libx264 -b:v:0 2000k \
-s:v:1 640x480 -c:a aac -c:v:1 libx264 -b:v:1 1000k \
-s:v:2 320x240 -c:a aac -c:v:2 libx264 -b:v:2 600k \
-f hls -hls_playlist_type vod -master_pl_name test.m3u8 \
-hls_segment_filename test_%v/test%06d.ts -use_localtime_mkdir 1 stream_%v.m3u8
You need to map the input video stream for each output video stream explicitly like this:
ffmpeg -i test.mp4 \
-map 0:v:0 -s:v:0 1280x720 -c:v:0 libx264 -b:v:0 2000k \
-map 0:v:0 -s:v:1 640x480 -c:v:1 libx264 -b:v:1 1000k \
-map 0:v:0 -s:v:2 320x240 -c:v:2 libx264 -b:v:2 600k \
-c:a aac \
-f hls -hls_playlist_type vod -master_pl_name test.m3u8 \
-hls_segment_filename test_%v/test%06d.ts -use_localtime_mkdir 1 stream_%v.m3u8

How can I make this command by using ffmpeg-python?

ffmpeg -i brooklynsfinest_clip_1080p.mp4 \
-filter_complex \
"[0:v]split=3[v1][v2][v3]; \
[v1]copy[v1out]; [v2]scale=w=1280:h=720[v2out]; [v3]scale=w=640:h=360[v3out]" \
-map [v1out] -c:v:0 libx264 -x264-params "nal-hrd=cbr:force-cfr=1" -b:v:0 5M -maxrate:v:0 5M -minrate:v:0 5M -bufsize:v:0 10M -preset slow -g 48 -sc_threshold 0 -keyint_min 48 \
-map [v2out] -c:v:1 libx264 -x264-params "nal-hrd=cbr:force-cfr=1" -b:v:1 3M -maxrate:v:1 3M -minrate:v:1 3M -bufsize:v:1 3M -preset slow -g 48 -sc_threshold 0 -keyint_min 48 \
-map [v3out] -c:v:2 libx264 -x264-params "nal-hrd=cbr:force-cfr=1" -b:v:2 1M -maxrate:v:2 1M -minrate:v:2 1M -bufsize:v:2 1M -preset slow -g 48 -sc_threshold 0 -keyint_min 48 \
-map a:0 -c:a:0 aac -b:a:0 96k -ac 2 \
-map a:0 -c:a:1 aac -b:a:1 96k -ac 2 \
-map a:0 -c:a:2 aac -b:a:2 48k -ac 2 \
-f hls \
-hls_time 2 \
-hls_playlist_type vod \
-hls_flags independent_segments \
-hls_segment_type mpegts \
-hls_segment_filename stream_%v/data%02d.ts \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" stream_%v.m3u8
how can I convert this command to ffmpeg-python code?
I don't know how to do this😭
Use subprocess.
proc = subprocess.Popen("ffmpeg", "next arg", "next arg")
Remember to import subprocess.

Add PNG overlay to multi output HLS M3U8 with FFmpeg

I've been banging my head for 2 days now. I can currently output 3 M3U8 "HLS" outputs resized but now I need to add an overlay to each output. The overlay image would need to be resized as well. My take on this is that the image should be applied to the source and then the 3 outputs can be generated. I have read that -vf cannot be used since there are 2 inputs. Here is what I am currently using which works.
How could I add an image overlay?
ffmpeg -hide_banner -y -i input.mov^
-vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_flags single_file^
360p.m3u8
-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_flags single_file 720p.m3u8^
-vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_flags single_file 1080p.m3u8
Any advice would be greatly appreciated.
Thanks in advance.
You need to use a filter_complex for the overlay filter, and then splitting the result into 3 and scaling each copy:
ffmpeg -hide_banner -y -i input.mov -i image.png
-filter_complex "[0][1]overlay,split=3[a][b][c];[a]scale=w=640:h=360:force_original_aspect_ratio=decrease[a];[b]scale=w=1280:h=720:force_original_aspect_ratio=decrease[b];[c]scale=w=1920:h=1080:force_original_aspect_ratio=decrease[c]"
-map "[a]" -map 0:a -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_flags single_file 360p.m3u8
-map "[b]" -map 0:a -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_flags single_file 720p.m3u8
-map "[c]" -map 0:a -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 72 -keyint_min 72 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_flags single_file 1080p.m3u8
(If your h264 encoder is x264, b:v has no effect when used with crf, but maxrate and bufsize do.)

How to scale and add correct a logo in ffmpeg command?

I am trying to apply a watermark and also to scale it to the current video size via ffmpeg command:
Here is my inital comand that works without watermark
ffmpeg -v 0 -vcodec h264_qsv -i 'udp://#some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' -vf scale=iw:ih -profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k -deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k -preset veryfast -vb 2000k -f flv rtmp://127.0.0.1/app/720
Now I tried to add the picture as a watermark. There was a conflict while using with -vf scale=-1:ih*.5, in order to eliminate the problem I used -s 1280x720 to specify the resolution for the video stream, it worked but not properly.
ffmpeg -v 0 -vcodec h264_qsv -i 'udp://#some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' -i logo.png -filter_complex "overlay=10:10" -s 1280x720 -profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k -deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k -preset veryfast -vb 2000k -f flv rtmp://some.ip/app/720
The problem:
How can I specify in the ffmpeg command the both sizes of video and logo(watermark) so they don't conflict with each other and they auto adjust like -vf scale=-1:ih*.5 dose.
Thank you!
The scale2ref filter allows one to a video/image stream with reference to the dimensions of another video or image stream
e.g.
ffmpeg -v 0 -vcodec h264_qsv -i 'udp://#some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' \
-loop 1 -i logo.png \
-filter_complex "[1:v][0:v]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v]" \
-map "[v]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k \
-preset veryfast -vb 2000k \
-f flv rtmp://some.ip/app/720
Here 1:v - the logo image - is being scaled to 1/8th the width of [0:v], the H.264 stream.
For the command given in the comments:
ffmpeg -v 0 -vcodec h264_qsv -i 'input' \
-loop 1 -i logo.png \
-filter_complex "[0:v]scale=iw:ih[v0]; \
[1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v]" \
-map "[v]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k \
-preset veryfast -vb 2000k \
-f flv out1 \
-filter_complex "[0:v]scale=-1:ih/2[v0]; \
[1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v2]" \
-map "[v2]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 2000k \
-preset veryfast -vb 1000k \
-f flv out2 \
-filter_complex "[0:v]scale=-1:ih/4[v0]; \
[1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v3]" \
-map "[v3]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 1000k \
-preset veryfast -vb 512k \
-f flv out3 \

Resources