End FFMPEG execution when RTMP input is closed - nginx

I have an RTMP arut server inside which I call a python script to run an FFMPEG command and create HLS packaging.
Problem is that when I stop the RTMP stream, FFMPEG still run processes in background.
FFMPEG string is like this:
ffmpeg -re -v verbose -i rtmp://localhost:1935/live/testlive -rw_timeout 500 -http_persistent 1 -method PUT -http_user_agent test -f hls -hls_list_size 5 -hls_flags discont_start+delete_segments -vf "scale=426:trunc(ow/a/2)*2" -c:a libfdk_aac -ar 48000 -c:v h264 -profile:v main -crf 24 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type event -preset veryfast -b:v 300k -maxrate 856k -bufsize 1200k -b:a 128k -vcodec libx264 -hls_segment_filename http://mystream/123/v1/testlive/0_%03d.ts http://mystream/123/v1/testlive/index.m3u8

The RTMP server should send an NetStream.Play.Stop event to subscribers (or disconnect them) to signal end of stream. If you are using the nginx rtmp module, look at the play_restart or idle_streams directives.

Related

NGINX and FFMPEG generate dynamic adaptive streaming

In this configuration file
https://github.com/TareqAlqutami/rtmp-hls-server/blob/master/conf/nginx.conf#L24-L30
for each received stream, transcode for adaptive streaming This single
ffmpeg command takes the input and transforms the source into 4
different streams with different bitrates and qualities. # these
settings respect the aspect ratio.
How we can dynamically generate variants? i.e for 1080p input generate all variants, but for 240p input generate no variants
My startup works without error
You need to configure the log and see what error it gives.
But another solution is to check manually
You may be using a codec that is not installed
I will check your tank, maybe I can contribute here
application live {
live on; # Allows live input
exec ffmpeg -i rtmp://localhost/live/$name -threads 8
-c:v libx264 -profile:v baseline -b:v 768K -s 640x360 -vf "drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: text='360': x=10: y=10:" -f flv -c:a aac -ac 1 -strict -2 -b:a 96k rtmp://localhost/show/$name_360
-c:v libx264 -profile:v baseline -b:v 1024K -s 960x540 -vf "drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: text='480': x=10: y=10:" -f flv -c:a aac -ac 1 -strict -2 -b:a 128k rtmp://localhost/show/$name_480
-c:v libx264 -profile:v baseline -b:v 1920K -s 1280x720 -vf "drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: text='720': x=10: y=10:" -f flv -c:a aac -ac 1 -strict -2 -b:a 128k rtmp://localhost/show/$name_720
-c:v libx264 -profile:v baseline -b:v 4000K -s 1920x1080 -vf "drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: text='720': x=10: y=10:" -f flv -c:a aac -ac 1 -strict -2 -b:a 128k rtmp://localhost/show/$name_1080;
}
application show {
live on; # Allows live input from above
hls on; # Enable HTTP Live Streaming
# hls_fragment 5s;
# Pointing this to an SSD is better as this involves lots of IO
hls_path /dest;
#hls_variant _240 BANDWIDTH=288000;
hls_variant _360 BANDWIDTH=448000;
hls_variant _480 BANDWIDTH=1152000;
hls_variant _720 BANDWIDTH=2048000;
hls_variant _1080 BANDWIDTH=4096000;
}

Can't livestream with FFmpeg on Nginx?

I am trying to livestream through RTMP on FFmpeg in Nginx. Doing sudo ffmpeg -re -f video4linux2 -i test.mp4 -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/show/stream gives me ioctl(VIDIOC_QUERYCAP): Inappropriate ioctl for device
test.mp4: Inappropriate ioctl for device
Am I even doing this right?

watermark on transcoding rtmp video nginx

Hello i am transcoding video with rtmp and i want to put some text...i put Arial.ttf in my home directory of the server but doesnt work.. this is my command..
-map 0:0 -map 0:1 -strict -2 -crf 26 -vcodec libx264 -preset superfast
-acodec aac -b:a 128k -vf scale=-1:720 -aspect 16:9 -g 50 -r 30 -ar 48000
-ac 2 -vf drawtext="fontfile=/home/Arial.ttf: text='TEXT': fontcolor=white: fontsize=24: box=1: boxcolor=black: x=10:y=10"
-f flv
can someone help me please?
thanks
Make sure you include absolute path to font and file is there:
drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf:
text='Test Text': x=100: y=50: fontsize=24: fontcolor=yellow#0.2:
box=1: boxcolor=red#0.2"
Redirect output to a log file and see if command outputs any errors.
You can also include a watermark image instead:
-i /path/to/watermark.png -filter_complex "overlay=main_w-overlay_w-4:4"

Use FFMPEG to restream RTMP source to YouTube - no video stream in output

I am attempting to grab the .m3u8 file from an nginx-rtmp server and pass it along to YouTube rtmp. I believe this to be possible (for example here: https://stackoverflow.com/a/11978820/1552594 although this is on the same host). The command I am using is:
ffmpeg -analyzeduration 0 -i \
http://source.rtmp.server/hls/stream.m3u8 -pix_fmt yuv420p \
-f flv rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx
However the output contains only audio and YouTube doesn't like it. The command produces following:
As you can see no Video stream in output metadata, stream mapping shows only audio and the trace shows 0kb of Video for 651kb of Audio
Any help much appreciated
MORE INFO
Improved version of the command lifted from this article:
https://judge2020.com/restreaming-a-m3u8-hls-stream-to-youtube-using-ffmpeg/
"Restreaming a m3u8 HLS stream to Youtube using FFMPEG" AKA exactly what I am trying to do.
The command I am sending is now:
ffmpeg -re -i "http://source.rtmp.server/hls/stream.m3u8" \
-strict -2 -c:v copy -c:a aac -ar 44100 -ab 128k -ac 2 -flags \
+global_header -bsf:a aac_adtstoasc -bufsize 3000k -f flv \
"rtmp://a.rtmp.youtube.com/live2/xxx-xxxx-xxxx-xxxx"
I got pretty much exactly the same response except with the Audio being read and output using aac codec.
MORE MORE INFO
I have found that adding a mapping can force the video stream into the output:
ffmpeg -re -i "http://source.rtmp.server/hls/stream.m3u8" \
-strict -2 -c:v copy -c:a -map 0:0 -map 0:1 -ar 44100 -ab 128k -ac 2 \
-flags +global_header -bsf:a aac_adtstoasc -bufsize 1000k \
-f flv "rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx"
This throws up the error that has presumably been resulting in the video stream being silently dropped:
Finally worked it out. The last issue above was a red herring and was due to missing a codec argument for the audio -c:a.
The complete working command is as follows:
ffmpeg -probesize 100M -analyzeduration 20M -re \
-i "http://source.rtmp.server/hls/stream.m3u8" -strict -2 -c:v \
libx264 -pix_fmt yuv420p -c:a aac -map 0:0 -map 0:1 -ar 44100 \
-ab 128k -ac 2 -b:v 2567k -flags +global_header -bsf:a aac_adtstoasc \
-bufsize 1000k -f flv "rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx"
The important parts are -probesize and -analyeduration - these need tweaking until they work. The -re flag is important to indicate restreaming. The various video codec declarations are also important - -c:v libx264 -pix_fmt yuv420p or it will throw errors about the output size being 0x0. Finally the map flags ensure that both streams are included in the output: -map 0:0 -map 0:1

Stream from file .avi - Nginx + Rtmp

I want stream to rtmp server. I try:
ffmpeg.exe -f dshow -i "myvideo.avi" -s 480x288 -r 10 -vcodec libx264 -b:v 30k -pix_fmt yuv420p -acodec libvo_aacenc -ac 1 -b:a 4k -f flv rtmp://ip/live/test
And did not work... Error:
http://i.stack.imgur.com/06Rcf.png
how to do? Thx

Resources