def download(url, tempdir): yt = YouTube() yt.url = url video = None if len(yt.videos) == 0: raise NotFound(url) if len(yt.filter('webm')) > 0: video = yt.filter('webm')[-1] # Best resolution elif len(yt.filter('mp4')) > 0: video = yt.filter('mp4')[-1] # Best resolution else: video = yt.videos[-1] # Best resolution video.download(tempdir) return video.filename