This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideos.py
61 lines (46 loc) · 1.66 KB
/
videos.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
from distutils import dir_util
from pathlib import Path
from pytube import YouTube
import json
ROOT = Path(__file__).parent
def videos(
overwrite: bool = False,
) -> None:
"""Download the train video and the test video from YouTube and save them to files.
Args:
overwrite (bool, optional): overwrite existing files. Defaults to False.
"""
# Load settings.json
with open(ROOT / "settings.json") as f:
settings = json.load(f)
# Create varaibles
videos = ROOT / settings["videos"]
urls = settings["vidoes_urls"]
names = settings["vidoes_names"]
# Remove all files in directory if overwrite is true
if overwrite and videos.exists():
dir_util.remove_tree(str(videos))
# Create the directory if it doesn't exist
videos.mkdir(exist_ok=True)
# Download all vidoes
for url, name in zip(urls, names):
stream = YouTube(url).streams.get_highest_resolution()
stream.download(videos, name)
def parse_opt(known: bool = False) -> argparse.Namespace:
"""Set up command line arguments
Args:
known (bool, optional): if arguments are known, throw an error if an unknown argument are passed in. Defaults to False.
Returns:
argparse.Namespace: parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-o", "--overwrite", action="store_true", help="overwrite the directory"
)
opt = parser.parse_known_args()[0] if known else parser.parse_args()
return opt
# Run this code if this script is called from a command line
if __name__ == "__main__":
opt = parse_opt()
videos(overwrite=opt.overwrite)