user13733104
user13733104

Reputation:

How do I check if a Youtube video Url is valid or not in Python?

I am creating a video downloader software with Python. And I want to check if the Youtube video link that the user entered is a valid Youtube link or not. How do I do that? (I am using Python 3.5)

Upvotes: 5

Views: 7877

Answers (2)

XYZ
XYZ

Reputation: 27397

def check_video_url(video_id):
    checker_url = "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v="
    video_url = checker_url + video_id

    request = requests.get(video_url)

    return request.status_code == 200

Upvotes: 2

Just for fun
Just for fun

Reputation: 4225

Use requests.get and check if "Video unavailable" in response

r = requests.get("https://www.youtube.com/watch?v=OpA2ZxnRs6") # random video id
"Video unavailable" in r.text
>>> True

If its False, then the Video ID/Video is valid

Upvotes: 2

Related Questions