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

问题描述 投票:1回答:2

现在,我正在使用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
2个回答
2
投票

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

选项-fflags nobuffer可能会有所帮助,通常对于接收流减少延迟。

您可以阅读有关herenobuffer

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


0
投票

我避免使用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
© www.soinside.com 2019 - 2024. All rights reserved.