discord.pyhttp-status-code-403http-errorytdl

I get too often the HTTP error 403 Forbidden using discord.py and ytdl


I built a discord bot using ytdl, it works fine but randomly, I get this error and I can't figure out the cause: (I truncated the link with ... because it was too long)

[https @ 000001ead63ee980] HTTP error 403 Forbidden
https://rr2---sn-uxaxpu5ap5-jp5l.googlevideo.com/videoplayback?expire=...: Server returned 403 Forbidden (access denied)

I thought the problem was this portion of code:

with youtube_dl.YoutubeDL(ytdlopts) as ydl:
    ydl.cache.remove()
    info = ydl.extract_info(search_query, download=False)

So I put it inside a try/catch, but it doesn't catch the error. I tried also:

    ydl.cache.remove()

But the problem still there, in the odious event that the problem is not in my code but on the server side, is there a way to fix or mitigate the problem?

These are my ffmpeg/ytdl options:

    ytdlopts = {
        'format': 'bestaudio/best',
        'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
        'restrictfilenames': True,
        'nocheckcertificate': True,
        'ignoreerrors': False,
        'logtostderr': False,
        'quiet': True,
        'extract_flat': True,
        'skip_download': True,
        'default_search': 'auto',
        'source_address': '0.0.0.0'  # ipv6 addresses cause issues sometimes
    }
    
    ffmpegopts = {'before_options': '-nostdin','options': '-vn'}
    opts = {'extract_flat': True, 'skip_download': True}
    ytdl = YoutubeDL(ytdlopts)
    ```

Solution

  • try doing ipv4: True and 'cachedir': False in ytdlopts, doing this, you force ipv4 and clear cache, I'm not sure it works but on me works fine.

    ytdlopts = {
            'format': 'bestaudio/best',
            'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
            'restrictfilenames': True,
            'nocheckcertificate': True,
            'ignoreerrors': False,
            'logtostderr': False,
            'quiet': True,
            'extract_flat': True,
            'skip_download': True,
            'default_search': 'auto',
            'source_address': '0.0.0.0'  # ipv6 addresses cause issues sometimes
            'force-ipv4': True,
            'cachedir': False
        }