在 ffmpeg 上启用缓存以记录流式传输

Enable cache on ffmpeg to record streaming

现在我正在使用steamlink和ffmpeg来录制流并将它们保存到文件中,很多时候保存的视频文件有很多延迟。我找到了这个 link https://www.reddit.com/r/Twitch/comments/62601b/laggy_stream_on_streamlinklivestreamer_but_not_on/ 他们声称延迟问题是由于未在播放器上启用缓存而发生的。 我尝试放置选项 -hls_allow_cache allowcache -segment_list_flags cache 结果是 ffmpeg 进程开始或多或少 8 秒,之后它结束并立即再次启动而不返回视频文件,如果我不放置这两个选项视频是记录正确,但大部分时间都有一些延迟。

显然,如果我从浏览器访问流媒体,我没有延迟问题

这是代码

from streamlink import Streamlink, NoPluginError, PluginError
streamlink = Streamlink()
#this code is just a snippet, it is inside a while loop to restart the process
try:
    streams = streamlink.streams(m3u8_url)
    stream_url = streams['best'].url
    #note hls options not seem to work
    ffmpeg_process = Popen(
        ["ffmpeg", "-hide_banner", "-loglevel", "panic", "-y","-hls_allow_cache", "allowcache", "-segment_list_flags", "cache","-i", stream_url, "-fs", "10M", "-c", "copy",
        "-bsf:a", "aac_adtstoasc", fileName])

    ffmpeg_process.wait()

except NoPluginError:
    print("noplugin")

except PluginError:
    print("plugin")

except Exception as e:
    print(e)

启用缓存并尽可能限制延迟的最佳选项是什么?

您可以阅读 FFmpeg StreamingGuide 以了解有关延迟的更多详细信息。例如,您有

an option -fflags nobuffer which might possibly help, usually for receiving streams ​reduce latency.

你可以阅读 here 关于 nobuffer

Reduce the latency introduced by buffering during initial input streams analysis.

我简单的解决了卡顿问题,避免使用ffmpeg保存视频而是直接使用streamlink写一个.mp4文件

streamlink = Streamlink()
try:
    streams = streamlink.streams(m3u8_url)
    stream_url = streams['480p']
    fd = stream_url.open()

    out = open(fileName,"wb")

    while True:
        data = fd.read(1024)
        if data is None or data == -1 or data == 0:
           break
        else:
            out.write(data)
      fd.flush()
      fd.close()
      out.flush()
      out.close()
except NoPluginError:
    #handle exception        
except PluginError:
    #handle exception        
except StreamError:
    #handle exception        
except Exception as e:
    #handle exception