【问题标题】:Enable cache on ffmpeg to record streaming在 ffmpeg 上启用缓存以记录流式传输
【发布时间】:2020-06-19 04:20:36
【问题描述】:

现在我正在使用 steamlink 和 ffmpeg 来录制流并将它们保存到一个文件中,很多时候保存的视频文件都有很多延迟。我找到了这个链接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)

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

【问题讨论】:

标签: python caching ffmpeg http-live-streaming streamlink


【解决方案1】:

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

一个选项-fflags nobuffer 可能有帮助,通常用于 接收流减少延迟。

您可以阅读here 关于nobuffer

减少在初始输入期间缓冲引入的延迟 流分析。

【讨论】:

  • 似乎效果不佳,视频继续滞后
  • @james 最好在这两种情况下都包含指标,也许理想的情况是什么?
  • 找到了一个更好的解决方案,不是使用 ffmpeg,而是通过编写 mp4 文件来保存视频。无论如何谢谢
  • 哦,太好了!考虑到您的目标是消除滞后,这仍然是一个有效的答案。
【解决方案2】:

我只是通过避免使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2018-07-25
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多