How to check if a url is valid that youtube-dl supports

Question:

I am developing a project, where user submits a URL.
I need to check if that URL is valid url , to download data from youtube-dl supported sites.

Please help.

Asked By: My Projects

||

Answers:

Try this function:

import youtube-dl

url = 'type your url here'

def is_supported(url):
    extractors = youtube_dl.extractor.gen_extractors()
    for e in extractors:
        if e.suitable(url) and e.IE_NAME != 'generic':
            return True
    return False

print (is_supported(url))

Remember: you need to import youtube_dl

Answered By: m8factorial

Here’s an example code in Python using the youtube-dl library to check if a URL is for a video or not:

import youtube_dl

def check_url_video(url):
    ydl = youtube_dl.YoutubeDL({'quiet': True})
    try:
        info = ydl.extract_info(url, download=False)
        return True
    except Exception:
        return False

url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
if check_url_video(url):
    print("The URL is for a video.")
else:
    print("The URL is not for a video.")

or

import youtube_dl

ydl = youtube_dl.YoutubeDL()

try:
    info = ydl.extract_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ", download=False)
    print("The URL is for a video.")
except youtube_dl.DownloadError:
    print("The URL is not for a video.")

This code uses the youtube_dl library to try to extract information from the URL using ydl.extract_info(). If the extraction is successful, the URL is for a video. If it fails, the URL is not for a video.

Answered By: Ricardo Reis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.