very low latency streaminig with ffmpeg using a webcam

userDtrm picture userDtrm · Mar 22, 2017 · Viewed 14.9k times · Source

I'm trying to configure ffmpeg to do a real-time video streaming using a webcam. The ffmpeg encoder command I use is as follows.

ffmpeg -f v4l2 -input_format yuyv422 -s 640x480 -i /dev/video0 -c:v libx264 -profile:v baseline -trellis 0 -subq 1 -level 32 -preset superfast -tune zerolatency -me_method epzs -crf 30 -threads 0 -bufsize 1 -refs 4 -coder 0 -b_strategy 0 -bf 0 -sc_threshold 0 -x264-params vbv-maxrate=2000:slice-max-size=1500:keyint=30:min-keyint=10: -pix_fmt yuv420p -an -f mpegts udp://192.168.1.8:5001

The ffplay command used to display the video feed is,

ffplay -analyzeduration 1 -fflags -nobuffer -i udp://192.168.1.8:5001

However, I'm experiencing a latency of 0.5 - 1.0s latency in the video stream. Is there a way to reduce this to a number less than 100ms. Also, when I replace the v4l2 camera capture with a screen capture using x11grab, the stream is almost real-time and I experience no noticeable delays. Moreover, changing the encoder from x264 to mpeg2 had no effect on the latency. In addition, the statistics from the ffmpeg shows that the encoder is performing at a 30fps rate, which I believe indicates that the encoding is real-time. This leaves me with only one reason for the experienced delay.

  • Is there a significant delay in buffers when using v4l2 during video capturing in a webcam?
  • I don't think the transmission delay is in effect in this case as I see no latencies when screen capture is used under the same conditions.
  • Can this latency be further reduced?. Can someone think of a different encoder configuration to be used instead of the one that I've been using?

Answer

chrifdexter97 picture chrifdexter97 · Aug 2, 2018

i used the same send instruction and i tried this with the ffplay and it worked for me:

ffplay -analyzeduration 1 -fflags -nobuffer -probesize 32 -sync ext -i rtmp://localhost/live/STREAM_NAME