Search code examples
pythonpython-3.xerror-handlingyoutube-dl

(python) Youtube-dl Connection Error Handling


I am running a script to download videos using youtube-dl in python

def dl_videos():
    while True:
        try:
            while True:
                ydl_opts = {
                    'ignoreerrors': 'True',
                    'download_archive': 'archive',
                    'format': 'bestaudio/best',
                    'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
                    'postprocessors': [{
                        'key': 'FFmpegExtractAudio',
                        'preferredcodec': 'mp3',
                        'preferredquality': '193',
                    }],
                }
                with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                    with open ('PlaylistOnly', 'r') as r:
                        d = r.readlines()
                        for line in d:
                            ydl.download([line])
                time.sleep(24.0 * 60.0 * 60.0)

        except(FileNotFoundError):
            time.sleep(5)
            continue

dl_videos()

however, I want this script to handle connection drops. so when I cut the connection in the middle of the program, it is put to a complete hault with this error: [0;31mERROR:[0m Unable to download webpage: <urlopen error [Errno -3] Temporary failure in name resolution> (caused by URLError(gaierror(-3, 'Temporary failure in name resolution')))

Note: error only arrises during a certain point of youtube-dl's process if online connection in lost

I would like the program to wait a little while with time then retry the module but i'm not sure how to handle this error at all. Idk if this is a specific error type i can handle with an exception. Any help appreciated

-Edit- (solution)

def dl_videos():
    while True:
        try:
            while True:
                ydl_opts = {
                    'ignoreerrors': 'True',
                    'download_archive': 'archive',
                    'format': 'bestaudio/best',
                    'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
                    'postprocessors': [{
                        'key': 'FFmpegExtractAudio',
                        'preferredcodec': 'mp3',
                        'preferredquality': '193',
                    }],
                }
                with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                    with open ('PlaylistOnly', 'r') as r:
                        d = r.readlines()
                        for line in d:
                            ydl.download([line])
                #Checks if there's a connection to youtube.com, if there's none it loops back before the "freeze" which my dumb a didnt realize was just the next time.sleep function
                if assets.connect() == False:
                    time.sleep(10)
                    continue
                time.sleep(24.0 * 60.0 * 60.0)

        except(FileNotFoundError):
            time.sleep(5)
            continue

dl_videos()

Solution

  • Try updating youtube-dl

    Anytime I have run into odd connection issues with it, it was resolved by updating it.

    Also, see
    https://github.com/ytdl-org/youtube-dl/issues/618
    https://github.com/ytdl-org/youtube-dl/issues/7586

    Also, if you are trying to download many links at the same time, maybe try breaking it up into smaller chunks. Is it the same link that has the problem each time? Are some downloaded successfully?

    /// EDIT

    I understand your question better now that you have edited. For a basic try and except solution you could just

    for line in d:
        try:
            ydl.download([line])
        except:
            print(f"error with {line}")