File size: 1,033 Bytes
5156398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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