How to reduce audio delay in FFmpeg using HTTP FLV stream? - ffmpeg

I have problem with live http flv stream. I'm using the following command and it works great except for the audio. Audio has delay that increasing over time. I can fix the this by removing refresh rate option on the output (-r 30) but then stream latency goes higher for about half second.
ffmpeg -f v4l2 -threads 0 -video_size 672X420 -i /dev/video1 -f alsa -thread_queue_size 512 -i hw:1,0 -c:a aac -ar 44100 -b:a 128k -c:v libx264 -s 672x420 -r 30 -g 60 -preset superfast -tune zerolatency -strict -2 -f flv rtmp://localhost/live/primary
Can someone explain why this happening and what possible fixes are?
On the client I'm using Chrome with flv.js library.
On the server Node-Media-Server.

Related

ffmpeg to YouTube with tee mux giving resolution warning (65535x65535), which is not optimal

I am streaming to YouTube and Facebook using ffmpeg , also writing data into disk (recording).
it's working fine on Facebook and recording but on YouTube it's giving warning that is
Please check the video resolution. The current resolution is (65535x65535), which is not optimal.
and output on YouTube is also 1:1 aspect ratio due to the above resolution.
I am using tee mux in ffmpeg Command.
ffmpeg -f dshow -framerate 30 -i video="Integrated Webcam":audio="Microphone Array (IntelĀ® Smart Sound Technology (IntelĀ® SST))" -s 1920x1080 -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 28 -pix_fmt yuv420p -c:a aac -strict -2 -ac 2 -b:a 128k -t 4 -map 0 -f tee "[f=ismv]pipe:1 | [f=flv]rtmps://youtube | [f=flv]facebook"

ffmpeg live stream transcoding. A/V sync issues on fast camera movement

I create a webrtc peer connection with my server(only stun)
Using pion webrtc for the server
I write the received RTP packets as VP8 and opus streams, as described here, to two pipes (the writers; created with os.Pipe() in golang)
The read ends of these two pipes are received by ffmpeg as inputs (via exec.Command.ExtraFiles) for transcoding using libx264 and aac into a single stream. The command:
ffmpeg -re -i pipe:3 -re -r pipe:4 -c:a aac -af aresample=48000 -c:v libx264 -x264-params keyint=48:min-keyint=24 -profile:v main -preset ultrafast -tune zerolatency -crf 20 -fflags genpts -avoid_negative_ts make_zero -vsync vfr -map 0:0,0:0 -map 1:0,0:0 -f matroska -strict -2 pipe:5
The above command outputs to a pipe(:5) the read end of which is being taken as input by the following:
ffmpeg -hide_banner -y -re -i pipe:3 -sn -vf scale=-1:'min(ih,360)' -c:v libx264 -pix_fmt yuv420p -ca aac -b:a 128k -b:v 1400k -maxrate 1498k -bufsize 2100k -hls_time 1 -hls_playlist_type event -hls_base_url /workdir/streamID/360p -hls_segment_filename /workdir/streamID/360p/360_%%03d.ts -f hls /workdir/streamID/360p.m3u8
This works fine as long as there are no movements of my webcam. The moment that happens the video speed suddenly increases for a split second and audio delay gets introduced. This delay keeps increasing each time I shake my webcam.
The first command in point 4 above - if written to a file separately will be absolutely fine, in terms of a/v sync, even with vigorous camera shaking. The weird audio delay is only when transcoding for hls output irrespective of whether I'm actually viewing it live or playing it back later.
This is my first time working with ffmpeg/hls/webrtc - would be really helpful if I could be pointed in the correct direction at least to be able to debug this or even know why this happens. Any and all help is greatly appreciated

Stop ffmpeg rtmp live stream without cutting off video

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?

Arecord->FFMPEG works but FFMPEG w/ ALSA stutters?

I am trying to stream audio from my Pi Zero and my I2s MEMS mic. I would like to stream using FFMPEG and ALSA (which I have already compiled) but I'm running into stuttering issues.
FFMPEG + ALSA
~/special/ffmpeg/ffmpeg -report -f alsa -ar 48000 -ac 2 -acodec pcm_s32le -i mic_sv -f lavfi -i testsrc -c:v h264_omx -c:a aac -ab 32k -bufsize 32k -f flv rtmp://209.85.230.23/live2/KEY
This results in constant stuttering and choppiness.
Arecord piped directly to FFMPEG
arecord -Dmic_sv -c2 -r48000 -fS32_LE | ~/special/ffmpeg/ffmpeg -report -acodec pcm_s32le -i - -f lavfi -i testsrc -c:v h264_omx -acodec aac -ab 32k -bufsize 32k -f flv rtmp://209.85.230.23/live2/KEY
This results in a coherent audio stream, but with skipping every 5 seconds or so.
Arecord recorded to a wav file, piped into FFMPEG
arecord -Dmic_sv -c2 -r48000 -fS32_LE -twav temp.v &
~/special/ffmpeg/ffmpeg -report -re -i temp.v -f lavfi -i testsrc -c:v h264_omx -ac 2 -acodec aac -ab 32k -bufsize 32k -async 2 -f flv rtmp://209.85.230.23/live2/KEY
This results in a perfect audio stream.
I don't know why #3 works but #2 and #1 cause problems. Any suggestions?
Well well off we go to my every first Stackoverflow post :)
So you probably experience a message saying [alsa buffer run] or similar.
The reason for this is indeed because the buffer fills up faster than it can be processed. The only real mitigation I found was to change the sampling rate to 22050hz
here is some code that should work:
ffmpeg -re -v verbose -hide_banner -f v4l2 -thread_queue_size 1024 -re -i temp.v -ar 11025 -f alsa -ac 1 -thread_queue_size 1024 -i default -b:a 64k -c:v libx264 -preset ultrafast -strict experimental -f flv -g 20 -keyint_min 20 -maxrate 2M -bufsize 6M -acodec aac rtmp://209.85.230.23/live2/KEY
p.s. Sorry for the late reply.

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