Last active
August 22, 2018 12:55
-
-
Save khuangaf/0e6d3c9d55f7eb70e4de47951749eebe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_video(video_id, path="videos", verbose=True): | |
""" | |
Download the videos | |
Parameters | |
---------- | |
video_id : str | |
A Youtube video id. | |
path: str | |
The directory which stores videos. | |
verbose: bool | |
Whether to log the intermediate results. | |
Returns | |
------- | |
True/False | |
Successfully downloaded the video or not. | |
""" | |
try: | |
# get video url | |
video_url = "https://www.youtube.com/watch?v=" + video_id | |
try: | |
video = pafy.new(video_url) | |
# get best video format | |
best = video.getbest(preftype="mp4") | |
# download video | |
best.download(filepath=path + "/" + video_id + "." + best.extension, | |
quiet=False) | |
# log | |
if verbose == True: | |
print("- {id} video downloaded.".format(id=video_id)) | |
return True | |
except Exception as e: | |
print("- {exception}".format(exception=e)) | |
print("- {id} video cannot be downloaded.".format(id=video_id)) | |
return False | |
except Exception as e: | |
print('Failed to download video: {}'.format(e)) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment