如何使用 ffmpeg 最大程度地减少直播的延迟 [英] How to minimize the delay in a live streaming with ffmpeg

查看:68
本文介绍了如何使用 ffmpeg 最大程度地减少直播的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我想从我的网络摄像头使用 ffmpeg 进行直播.

i have a problem. I would to do a live streaming with ffmpeg from my webcam.

  1. 我启动了 ffserver,它工作正常.
  2. 我从另一个终端启动 ffmpeg 以使用此命令进行流式传输它有效:

  1. I launch the ffserver and it works.
  2. From another terminal I launch ffmpeg to stream with this command and it works:

sudo ffmpeg -re -f video4linux2 -i /dev/video0 -fflags nobuffer -an http://localhost:8090/feed1.ffm

  • 在我的配置文件中,我有这个流:

  • In my configuration file I have this stream:

    <Stream test.webm>
    Feed feed1.ffm
    Format webm
     NoAudio
     VideoCodec libvpx
     VideoSize 720x576
     VideoFrameRate 25
     # Video settings
        VideoCodec libvpx
        VideoSize 720x576           # Video resolution
        VideoFrameRate 25           # Video FPS
        AVOptionVideo flags +global_header  # Parameters passed to encoder 
                                        # (same as ffmpeg command-line parameters)
        AVOptionVideo cpu-used 0
        AVOptionVideo qmin 10
        AVOptionVideo qmax 42
        #AVOptionVideo quality good
        PreRoll 5
         StartSendOnKey
        VideoBitRate 400            # Video bitrate
     </Stream>
    

  • 我用

  • I launch the stream with

    ffplay http://192.168.1.2:8090/test.webm它有效,但我有 4 秒的延迟,我会尽量减少此延迟,因为这对我的应用程序至关重要.谢谢

    ffplay http://192.168.1.2:8090/test.webm It works but I have a delay of 4 seconds and I would to minimize this delay because is essential for my application. Thanks

    推荐答案

    我发现了三个帮助我减少直播延迟的命令.第一个命令非常基本且直接,第二个命令与其他选项结合使用,这些选项在每个环境中的工作方式可能不同,最后一个命令是我在文档中找到的 hacky 版本它在开始时很有用,但现在第一个选项更稳定.

    I found three commands that helped me reduce the delay of live streams. The first command its very basic and straight forward, the second one it's been combined with other options which might work differently on each environment and the last command it is a hacky version that I found in the documentation It was useful at the beginning but now the first option is more stable.

    此格式标志减少了在初始输入流分析期间由缓冲引入的延迟.此命令将显着减少延迟,并且不会引入音频故障.

    This format flag reduces the latency introduced by buffering during initial input streams analysis. This command will reduce noticeable the delay and will not introduce audio glitches.

    ffplay -fflags nobuffer -rtsp_transport tcp rtsp://<host>:<port>
    

    2.高级 -flags low_delay 和其他选项.

    我们可以将之前的 -fflags nobuffer 格式标志与其他通用选项和高级选项结合起来,以获得更详细的命令:

    2. Advanced -flags low_delay and other options.

    We can combine the previous -fflags nobuffer format flag with other generic options and advanced options for a more elaborated command:

    • -flags low_delay 此编解码器通用标志将强制低延迟.
    • -framedrop:如果视频不同步,则丢弃视频帧.如果主时钟未设置为视频,则默认启用.使用此选项为所有主时钟源启用丢帧
    • -strict Experimental,最后-strict指定如何严格遵守标准,experimental选项允许非标准化的实验性的东西,实验性的(未完成/正在进行中/未经过充分测试)解码器和编码器.此选项是可选,请记住实验性解码器可能会带来安全风险,请勿将其用于解码不受信任的输入.
    • -flags low_delay this codec generic flag will force low delay.
    • -framedrop: to drop video frames if video is out of sync. Enabled by default if the master clock is not set to video. Use this option to enable frame dropping for all master clock sources
    • -strict experimental, finally -strict specifies how strictly to follow the standards and the experimental option allows non standardized experimental things, experimental (unfinished/work in progress/not well tested) decoders and encoders. This option is optional and remember that experimental decoders can pose a security risk, do not use this for decoding untrusted input.
    ffplay -fflags nobuffer -flags low_delay -framedrop 
    -strict experimental -rtsp_transport tcp rtsp://<host>:<port>
    

    此命令可能会引入一些音频故障,但很少.

    This command might introduce some audio glitches, but rarely.

    您也可以尝试添加:* -avioflags direct 减少缓冲,和* -fflags discardcorrupt 丢弃损坏的数据包,但我认为是非常激进的方法.这可能会破坏音视频同步

    Also you can try adding: * -avioflags direct to reduce buffering, and * -fflags discardcorrupt to discard corrupted packets, but I think is very aggressive approach. This might break the audio-video synchronization

    ffplay -fflags nobuffer -fflags discardcorrupt -flags low_delay  
    -framedrop -avioflags direct -rtsp_transport tcp rtsp://<host>:<port>
    

    3.一个hacky选项(在旧文档中找到)

    这是一种基于将 -probesize-analyzeduration 设置为低值以帮助您的流更快启动的调试解决方案.

    3. A hacky option (found on the old documentation)

    This is an debugging solution based on setting -probesize and -analyzeduration to low values to help your stream start up more quickly.

    • -probesize 32 以字节为单位设置探测大小(即要分析以获取流信息的数据大小).如果信息分散到流中,较高的值将能够检测到更多信息,但会增加延迟.必须是不小于 32 的整数.默认为 5000000.
    • analyzeduration 0 指定分析多少微秒来探测输入.较高的值将能够检测到更准确的信息,但会增加延迟.默认为 5000000 微秒(5 秒).
    • -sync ext 将主时钟设置为外部源以尝试保持实时性.默认为音频.主时钟用于控制音视频同步.这意味着此选项将音频-视频同步设置为一种类型(即 type=audio/video/ext).
    • -probesize 32 sets the probing size in bytes (i.e. the size of the data to analyze to get stream information). A higher value will enable detecting more information in case it is dispersed into the stream, but will increase latency. Must be an integer not lesser than 32. It is 5000000 by default.
    • analyzeduration 0 specifies how many microseconds are analyzed to probe the input. A higher value will enable detecting more accurate information, but will increase latency. It defaults to 5000000 microseconds (5 seconds).
    • -sync ext sets the master clock to an external source to try and stay realtime. Default is audio. The master clock is used to control audio-video synchronization. This means this options sets the audio-video synchronization to a type (i.e. type=audio/video/ext).
    ffplay -probesize 32 -analyzeduration 0 -sync ext -rtsp_transport tcp rtsp://<host>:<port>
    

    此命令有时可能会引入一些音频故障.

    This command might introduce some audio glitches sometimes.

    -rtsp_transport 可以根据您的流媒体设置为 udptcp.对于这个例子,我使用 tcp.

    The -rtsp_transport can be setup as udp or tcp according to your streaming. For this example I'm using tcp.

    这篇关于如何使用 ffmpeg 最大程度地减少直播的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆