如何使用ffmpeg最小化实时流中的延迟 [英] How to minimize the delay in a live streaming with ffmpeg

查看:345
本文介绍了如何使用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实验性,最后 -strict 指定严格遵守标准的方式,实验性选项允许进行非标准化的实验性内容,实验性(未完成/在进行中/未经良好测试)的解码器和编码器。此选项是可选,并且请记住, 实验解码器 可能会带来安全风险,请勿将其用于解码不受信任的输入。

    • -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 throwcorrupt 丢弃损坏的数据包,但我认为这是非常积极的方法。 这可能会中断音频视频同步

    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 可以设置为 udp tcp 根据您的流式传输。在此示例中,我使用的是 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天全站免登陆