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

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.

Related

How to generate HLS stream multiple qualities (Representations) for webcam capture

I want to generate multiple qualities (Representations) for HLS stream while capturing from webcam. I am using ffmpeg. I want to take video from camera, scale it and generate .m3u8 playlist that contains three qualities.
This is what I have tried:
ffmpeg \
-input_format yuyv422 \
-f v4l2 -video_size 640x480 \
-i /dev/video0 \
-c:v libx264 -crf 21 -preset veryfast \
-b:v 100M -b:a 128k \
"[0:v]split=3[v1][v2][v3]; \
[v1]copy[v1out]; [v2]scale=w=1280:h=720[v2out]; [v3]scale=w=854:h=480[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 1+3M -maxrate:v:2 3M -minrate:v:2 3M -bufsize:v:2 3M -preset slow -g 48 -sc_threshold 0 -keyint_min 48 \
-f hls \
-hls_time 6 \
-hls_flags independent_segments \
-hls_flags delete_segments \
-hls_segment_type mpegts \
-hls_list_size 2 \
-hls_playlist_type event \
-hls_segment_filename stream_%v/data%02d.ts \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,v:1,v:2" stream_%v.m3u8
As a result, I get only one quality in the playlist and that is the last one in order (854 x 480).

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?

Video captured from x11grab is not playable in HLS format on Firefox/Safari

I'm trying to produce an HLS playlist from a video file.
It's a file captured from a virtual screen with ffmpeg and x11grab module:
ffmpeg -y -t 10 -copyts -draw_mouse 0 -framerate 24 -f x11grab -thread_queue_size 1024 -i :0 -f pulse -i default -acodec copy /tmp/test_preprocessed.mkv
But when I'm trying to produce HLS from this file, the output seems not correct, as it doesn't play in Firefox/Safari, but does play in Chrome (using hls.js or https://www.hlsplayer.net/ for example)
Original file: https://transfer.sh/y1a6Fv/735171b9-42a1-461a-a118-981119bdf74e_preprocessed.mkv
HLS Playlist generated from this file: https://video-staging-bucket.ams3.digitaloceanspaces.com/c08584ca-4731-4820-b5c7-71f12955c0b2/stackoverflow/playlist.m3u8
(Works in https://www.hlsplayer.net/ with chrome, doesn't works with Firefox/Safari)
How I'm producing the HLS:
ffmpeg -y \
-t 10 \
-i 735171b9-42a1-461a-a118-981119bdf74e_preprocessed.mkv \
-r 24 \
-filter_complex '[0:v]split=3[v1][v2][v3]; [v1]scale=w=1280:h=720[v1out]; [v2]scale=w=854:h=480[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 3200k -maxrate:v:0 3200k -minrate:v:0 2500k -bufsize:v:0 3200k -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 1600k -maxrate:v:1 1600k -minrate:v:1 1250k -bufsize:v:1 1600k -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 900k -maxrate:v:2 900k -minrate:v:2 700k -bufsize:v:2 900k -preset slow -g 48 -sc_threshold 0 -keyint_min 48 \
-map a:0 -c:a:0 aac -b:a:0 96k \
-map a:0 -c:a:1 aac -b:a:1 128k \
-map a:0 -c:a:2 aac -b:a:2 128k \
-f hls \
-hls_time 4 \
-hls_playlist_type vod \
-hls_flags independent_segments \
-hls_segment_type mpegts \
-hls_segment_filename 'hls'/stream_%v_data%02d.ts \
-master_pl_name playlist.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
-t 10 \
'hls'/stream_%v.m3u8
Is there something I missed ? Maybe a standard ? Or maybe problem is related to PTS/DTS of my file ?
Thanks !

How do I create an HLS master playlist with ffmpeg?

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

Ffmpeg - Create a HLS video with different versions and creating a master playlist

I have this ffmpeg command in order to create an HLS video with four versions with different quality features, but I don't manage it creates the .m3u8 playlist. I have added at the end of the command the control -master_pl_name, but it doesn't work.
Can someone help me to create the command I want?
ffmpeg -hide_banner -y \
-i "MySourceVideo.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_key_info_file "MyKey.keyinfo" \
-hls_playlist_type vod \
-b:v 800k \
-maxrate 856k \
-bufsize 1200k \
-b:a 96k \
-hls_segment_filename "MyNewVideo_version_360p_%03d.ts" \
"MyNewVideo_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_key_info_file "MyKey.keyinfo" \
-hls_playlist_type vod \
-b:v 1400k \
-maxrate 1498k \
-bufsize 2100k \
-b:a 128k \
-hls_segment_filename "MyNewVideo_version_480p_%03d.ts" \
"MyNewVideo_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_key_info_file "MyKey.keyinfo" \
-hls_playlist_type vod \
-b:v 2800k \
-maxrate 2996k \
-bufsize 4200k \
-b:a 128k \
-hls_segment_filename "MyNewVideo_version_720p_%03d.ts" \
"MyNewVideo_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_key_info_file "MyKey.keyinfo" \
-hls_playlist_type vod \
-b:v 5000k \
-maxrate 5350k \
-bufsize 7500k \
-b:a 192k \
-hls_segment_filename "MyNewVideo_version_1080p_%03d.ts" \
"MyNewVideo_1080p.m3u8" \
\
-master_pl_name "MyNewVideo_index.m3u8"
I'm working on a similar problem and am equally surprised by the seeming lack of resources about this.
Anyway, I think you'll be able to get a step further if you add a single output file name after -master_pl_name "MyNewVideo_index.m3u8" output_%v.m3u8. I would then recommend removing the individual names for the quality streams, otherwise FFmpeg will create them twice.
It's possible you'll then have other issues, as I am, but that should solve your first problem.

Resources