python 中的 youtube-dl 设置选项

youtube-dl setting options in python

我正在尝试使用以下代码在 python 中使用 youtube-dl API 保存直播。由于它是一个连续的直播流,因此视频没有结束,所以我使用 hls-use-mpegts 作为一种定期读取视频进行处理的方式,该标志使 .mp4.part 个文件可以播放。

尽管 hls-use-mpegts 选项与命令行配合使用效果很好,因此:

youtube-dl -f worst <some URL> --retries infinite --continue --hls-use-mpegts

它似乎不适用于此代码。我没有看到任何错误,但没有看到文件以 mpegts 格式保存。我的选项设置正确吗?

    ydl_opts = {
        'format': 'worst',
        'retries': 99,
        'continue': True,
        'hls-use-mpegts': True
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

这是因为(很抱歉这么说)文档同时还不错&糟糕*。 我发现你要在 Python 中使用的每个 switches/cli-options 都必须将 - (dash) 替换为 _ (短划线).

解决方案

对于您的情况,hls_use_mpegts 是解决方案。

为什么?

Read/explore 关于这里:https://github.com/ytdl-org/youtube-dl/blob/5208ae92fc3e2916cdccae45c6b9a516be3d5796/youtube_dl/downloader/common.py#L50 和 这里:https://github.com/ytdl-org/youtube-dl/blob/5208ae92fc3e2916cdccae45c6b9a516be3d5796/youtube_dl/__init__.py#L428

或者像我通常那样浏览这些不便:https://github.com/ytdl-org/youtube-dl/search?q=hls_use_mpegts%3A(幸运的是 GitHub 在这方面做得很好,你不必下载 src 代码就可以了已搜索)

否则使用 yt-dl 会很有趣,谢谢他们!