Search code examples
pythonyoutube-dl

youtube-dl setting options in python


I am trying to save livestreams using youtube-dl API in python with the following code. Since it's a continuous live stream there is no end to the video, so I am using hls-use-mpegts as a way to periodically read the video for processing, that flag makes .mp4.part files playable.

Although the hls-use-mpegts option works well with the command-line thus:

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

it doesn't seem to work with this code. I don't see any errors but don't see the file being saved in mpegts format. Do I have the options setting correct?

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

Solution

  • It's because (sorry for saying that) the docs is somewhat good&shitt* at the same time. I found every switches/cli-options that you were to use within Python you have to replace - (dash) to _ (sub dash).

    Solution

    In your case, hls_use_mpegts is the solution.

    Why?

    Read/explore about that here: https://github.com/ytdl-org/youtube-dl/blob/5208ae92fc3e2916cdccae45c6b9a516be3d5796/youtube_dl/downloader/common.py#L50 and here: https://github.com/ytdl-org/youtube-dl/blob/5208ae92fc3e2916cdccae45c6b9a516be3d5796/youtube_dl/__init__.py#L428

    or just browse as I do usually for those inconveniences: https://github.com/ytdl-org/youtube-dl/search?q=hls_use_mpegts%3A (fortunately GitHub does a really good job at this, and u don't have to have the src code downloaded to be searched)

    Otherwise it's fun to use yt-dl, thanks for them!