Search code examples
pythonyoutube-dl

How to hide error message from youtube-dl / yt-dlp?


I'm using yt-dlp (in Python) to extract information from Twitch videos.

If I try to extract information from a non-existing video or a private one, I get an exception, which is the expected behaviour. But if I set to "quiet" mode and if I catch the potential exception, and I still get the error logged. Here is the code:

import yt_dlp as youtube_dl
from yt_dlp.utils import DownloadError


url = "https://www.twitch.tv/videos/1410795876"

options = {
    "quiet": True,
    "format": "bestaudio/worst",
}

with youtube_dl.YoutubeDL(options) as ydl:
    try:
        info = ydl.extract_info(url, download=False)
    except DownloadError:
        print("An exception has been caught")

When using this script, the output is the following:

ERROR: [twitch:vod] 1410795876: Failed to download m3u8 information: HTTP Error 403: Forbidden (caused by <HTTPError 403: 'Forbidden'>); please report this issue on  https://github.com/yt-dlp/yt-dlp , filling out the "Broken site" issue template properly. Confirm you are on the latest version using -U (caused by <HTTPError 403: 'Forbidden'>); please report this issue on  https://github.com/yt-dlp/yt-dlp , filling out the "Broken site" issue template properly. Confirm you are on the latest version using -U
An exception has been caught

Is there a way to hide this error log? And if not, is there a way using yt-dlp to check that the video is accessible so that I will not call extract_info if the video is not? Thank you.


Solution

  • I guess that the easiest way to hide all logging errors is by implementing your own log handler. Consider the following change to your code, I'm sure you'll figure out how to implement what you want from here:

    import yt_dlp as youtube_dl
    from yt_dlp.utils import DownloadError
    
    
    url = "https://www.twitch.tv/videos/1410795876"
    
    class loggerOutputs:
        def error(msg):
            print("Captured Error: "+msg)
        def warning(msg):
            print("Captured Warning: "+msg)
        def debug(msg):
            print("Captured Log: "+msg)
    
    
    options = {
        "quiet": True,
        "format": "bestaudio/worst",
        "logger": loggerOutputs,
    }
    
    with youtube_dl.YoutubeDL(options) as ydl:
        try:
            info = ydl.extract_info(url, download=False)
        except DownloadError:
            print("An exception has been caught")
     
    

    In the following example they disable logging by implementing a "FakeLogger": https://github.com/ytdl-org/youtube-dl/blob/master/test/test_http.py