2

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)

  • 1
    Just in case you did not know: There already is a video downloader software in python: youtube-dl. Also if you cannot figure out if a url leads to a valid video, I suspect your project will become quite frustrating. – Niklas Mertsch Aug 09 '20 at 12:09
  • Related: https://stackoverflow.com/questions/49752459/how-to-check-if-video-has-been-deleted-or-removed-in-youtube-using-python ; https://stackoverflow.com/questions/2742813/how-to-validate-youtube-video-ids – Tomerikoo Aug 09 '20 at 12:31

2 Answers2

2

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

Just for fun
  • 4,102
  • 1
  • 5
  • 11
2
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
XY L
  • 25,431
  • 14
  • 84
  • 143
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Dec 22 '20 at 10:26
  • this might help https://stackoverflow.com/questions/65284073/getting-oembed-using-youtubeurl-returns-403-forbidden – uwe Oct 24 '21 at 18:29
  • 1
    it's 200 also if the video is unavailble. – shisui Feb 21 '23 at 20:45