Stop ffmpeg rtmp live stream without cutting off video - ffmpeg

I can stream fine using the command
ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1280x720 \
-i :0.0+0,0 -c:v libx264 -preset veryfast -maxrate 1984k -bufsize 3968k \
-vf "format=yuv420p" -g 60 -c:a aac -b:a 128k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/<stream key>
Platforms generally have a delay of ~10 seconds for viewers. When I terminate the ffmpeg command, the stream ends immediately for the viewers and they miss the last 10 seconds of video.
How can I terminate the stream without the end of the video cutting off abruptly?

Related

Why ffmpeg ignores output fps?

I have the following ffmpeg command that streams an input to an RTMP endpoint :
ffmpeg
-re
-i -
-r 30
-vf scale=852:480
-c:v libx264
-pix_fmt yuv420p
-profile:v main
-preset veryfast
-x264opts "nal-hrd=cbr:no-scenecut"
-minrate 3000
-maxrate 3000
-g 60
-c:a aac
-b:a 160k
-ac 2
-ar 44100
-f flv
Some RTMPURL/ Some RTMPKey
This command works but the output frame rate is not respected. It drops to 6 fps.
I need it to be always 30 fps.
Does anyone know why it's not respected please ?
Thanks

ffmpeg youtube livestream stops after a while

I'll update this question
ffmpeg -version
ffmpeg -version
ffmpeg version 4.3.1-4ubuntu1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 10 (Ubuntu 10.2.0-9ubuntu2)
I run this command to use ffmpeg to stream to youtube ;
ffmpeg -y -threads 12 \
-loop 1 -framerate 30 -re \
-i ./1280x720.jpg \
-i ./audio.mp3 \
-video_size 1280x720 \
-vcodec libx264 -pix_fmt yuv420p \
-b:v 4500k -maxrate 5500k -bufsize 22000k \
-preset ultrafast -crf 23 -tune stillimage \
-b:a 128k -ar 44100 -ac 2 -acodec aac \
-filter_complex "dynaudnorm=f=150:g=15" \
-r 30 -g 60 \
-f flv rtmp://a.rtmp.youtube.com/live2/xxxx 2>&1 | tee _LOG
The stream is excellent for 45-53 minutes then i'll get an error like this from ffmpeg:
[flv # 0x56077027cd80] Delay between the first packet and last packet in the muxing queue is 10034000 > 10000000: forcing output
then youtube starts to say, no data being received and the stream will end, which it does.
This is the full log: http://0x0.st/-zUH.txt
Your MP3 duration is 00:49:57.42 so the stream messes up after it ends. Loop the audio with -stream_loop -1 and add -re for real-time reading of the input:
ffmpeg -y \
-loop 1 -framerate 30 -re -i ./1280x720.jpg \
-re -stream_loop -1 -i ./audio.mp3 \
-c:v libx264 -pix_fmt yuv420p \
-b:v 4500k -maxrate 5500k -bufsize 22000k \
-preset ultrafast -tune stillimage \
-b:a 128k -ar 44100 -ac 2 -c:a aac \
-filter_complex "dynaudnorm=f=150:g=15" \
-g 60 -f flv rtmp://a.rtmp.youtube.com/live2/xxxx
Alternatively, remove -re -stream_loop -1 and add the output option -shortest if you want the stream to end when the audio ends.
Unrelated changes:
No need to set -threads. Let ffmpeg auto choose.
-video_size 1280x720 is an input option for certain demuxers and does nothing in your command. Removed. Your input is already 1280x720 anyway: otherwise, see Resizing videos with ffmpeg to fit a specific size.
-b:v and -crf are mutually exclusive. In your case -b:v is being ignored. For streaming you probably want to use -b:v. Removed -crf.
You already set the frame rate with -framerate 30 so -r 30 is not needed. Removed.
Recommend using the slowest -preset that still encodes fast enough.

FFMPEG SCREEN RECORDING: How to get H265 (libx265) recording using ffmpeg with xorg?

I really would appreciate all the help I can get here.
I'm trying to use the libx265 codec for recording an xorg dummy screen. The command that currently works for H264 (libx264 codec) is:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx264 -preset fast
-profile:v main -level 3.1 -pix_fmt yuv420p -r 30 -crf 21 -g 60 -tune zerolatency -f mp4 capture.mp4
In trying to get H265 instead, I first changed the codec to libx265 like below:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx265 -preset fast
-profile:v main -level 3.1 -pix_fmt yuv420p -r 30 -crf 21 -g 60 -tune zerolatency -f mp4 capture.mp4
But that didn't do it. Although it didn't error, it was producing a file that was playing at twice the recorded speed (i.e. twice the speed of the clip that was recorded).
Then I tried using -x265-params to specify the parameters like this:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx264 -preset fast
-x265-params profile=main:level=3.1:crf=21 -pix_fmt yuv420p -r 30 -g 60 -tune zerolatency -f mp4 capture.mp4
And this gave me an error with the following message:
"output file #0 does not contain any stream ffmpeg"
I've tried all sorts of combinations, searched extensively online (for both how to set 265 parameters and on the output file error), but I'm not making a headway. I'm really new to all this. Can anyone please help (with the most simple terms and directions)?

FFMPEG -re Insurance

FFMPEG -re according to the ffmpeg docs:
Read input at native frame rate. Mainly used to simulate a grab
device, or live input stream (e.g. when reading from a file). Should
not be used with actual grab devices or live input streams (where it
can cause packet loss).
My ffmpeg stream command is:
ffmpeg -re -i https://www.example.com/video.mp4 -filter_complex tpad=start_duration=10:stop_duration=15:start_mode=add:color=black:stop_mode=add -af adelay=10000|10000 -maxrate 2M -crf 24 -bufsize 6000k -c:v libx264 -preset superfast -tune zerolatency -strict -2 -c:a aac -ar 44100 -attempt_recovery 1 -max_recovery_attempts 5 -drop_pkts_on_overflow 1 -f flv rtmp://live.example.com/123453
Except this does not always work, and sometimes I have livestreams ending early because ffmpeg is playing faster than the frame rate. Is there another command that can be used to ensure ffmpeg streams the video in real time?
Remove -re and add filter-based throttling.
ffmpeg -i https://www.example.com/video.mp4 -filter_complex tpad=start_duration=10:stop_duration=15:start_mode=add:color=black:stop_mode=add,fifo,realtime -af adelay=10000|10000,afifo,arealtime -maxrate 2M -crf 24 -bufsize 6000k -c:v libx264 -preset superfast -tune zerolatency -strict -2 -c:a aac -ar 44100 -attempt_recovery 1 -max_recovery_attempts 5 -drop_pkts_on_overflow 1 -f flv rtmp://live.example.com/123453

live streamming using ffmpeg for more than stream on the same time

I'm using this command to stream video in ffmpeg but when I stream more than 3 or 4 streams at the same time interruption happen although the process in my device doesn't exceed 50%
I tried to use ffmpeg lib for each stream but interruption still happened
this is my command :
ffmpeg -re -i test.mp4 -i logo.png -vcodec libx264 -pix_fmt yuv420p -vb 2000000 -g 60
-vprofile main -acodec aac -ab 128000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb
-strict experimental -filter_complex "[0][1]overlay=0:0"
-pass 1 -f mpegts udp://127.0.0.1:1234?pkt_size=1316

Resources