Datasets:
| from tqdm import tqdm | |
| from yt_dlp import YoutubeDL | |
| from yt_dlp.utils import DownloadError | |
| from load_data import get_data | |
| target_path = "videos" | |
| video_ids = list(get_data()['video_id']) | |
| # formats are sorted by priority, if possible use highest-priority format | |
| formats = [ | |
| "bestvideo[height>=360][height<=640][protocol!=http_dash_segments]", | |
| "bestvideo[height=360]", | |
| ] | |
| def get_config_dict(format): | |
| return { | |
| 'format': format, | |
| 'outtmpl': target_path + '/' + '%(id)s.%(ext)s', | |
| 'quiet': True, | |
| } | |
| yt_downloaders = [YoutubeDL(get_config_dict(format)) for format in formats] | |
| fixed_bar = tqdm(total=len(video_ids), position=0, leave=True) | |
| for video_id in video_ids: | |
| for yt_downloader in yt_downloaders: | |
| try: | |
| yt_downloader.download([f"https://www.youtube.com/watch?v={video_id}"]) | |
| fixed_bar.set_description(f"Downloading {video_id}") | |
| fixed_bar.update(1) | |
| except DownloadError: | |
| continue | |
| break |