Search code examples
pythonyoutube-dl

How to abort youtube-dl download if file grows above a certain size in Python


In python, is it possible to stop a youtube-dl download if it becomes larger than a specified size?

I am using youtube_dl imported into python:

import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(urls)

Is it possible to stop the .download() function early if it gets too big?


Solution

  • Like @larsks said in the comments, adding max_filesize to the ydl_opts works

    import youtube_dl
    
    ydl_opts = {
      'max_filesize': <number_of_bytes>
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(urls)