+3 votes
in Programming Languages by (1.9k points)
I have 100s of youtube video link. I want to check whether those videos still exist or have been deleted. Opening each link in a browser will take several hours. How can I check the status of those video using Python code?

1 Answer

0 votes
by (25.6k points)

If a video is deleted from youtube, its thumbnail is also deleted. So, you can check the HTTP response code for the thumbnail URL to know whether the video exists or has been deleted.

Here is an example to check the status of two youtube videos:

import requests

def check_yt_video_status(id):
    """
    This function will check the status of a youtube video.
    """
    link = "https://i.ytimg.com/vi/"
    picUrl = link + id + "/mqdefault.jpg"
    req = requests.head(picUrl)
    return req.status_code

if __name__ == "__main__":
    vids = ["ZB_cxKau8iQ", "-6OHlIVsWbc"]
    for vid in vids:
        if check_yt_video_status(vid) == 404:
            print("Video {0} has been deleted".format(vid))
        else:
            print("Video {0} exists".format(vid))

The output of the above code is as follows:

Video ZB_cxKau8iQ exists
Video -6OHlIVsWbc has been deleted

...