-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (75 loc) · 4.13 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
main.py
"""
from __future__ import annotations
from argparse import ArgumentParser
from os.path import expanduser as os_path_expanduser, join as os_path_join
from tempfile import gettempdir
from ytdlp_handler import (download_video, filter_audio_streams, filter_video_streams, get_best_audio_streams,
get_best_video_streams,
get_video_info, init_downloader)
VERSION = "ytdlh: beta-0.1.0; yt-dlp: 2022.12.30; pyinstaller: 6.3.0; python: 3.11.7"
# todo add longer help message to help users understand what the script does
parser = ArgumentParser(description="Download videos with the best or chosen quality")
parser.add_argument("video_url", help="The url of the video to download", type=str)
parser.add_argument("--version", help="Show the version of the script", action="version", version=VERSION)
parser.add_argument("--verbose", help="Verbose output", action="store_true")
parser.add_argument("-o", "--output", help="Output file name. Default is the video title", type=str, required=False)
parser.add_argument("-d", "--directory", help="Set output directory. Default is user/Downloads", type=str,
required=False)
parser.add_argument("-f", "--output-format", help="The output format of the video", type=str, required=False)
# todo implement the following options
# parser.add_argument("-m", "--manual", help="Choose the video and audio streams manually. "
# "User will be prompted to enter audio and video stream IDs",
# action="store_true")
# parser.add_argument("-r", "--resolution", help="The resolution of the video to download. "
# "The highest framerate and bitrate will be preferred by default.",
# choices=[144, 240, 360, 480, 720, 1080, 1440, 2160], type=int, required=False)
# parser.add_argument("-fr", "--framerate",
# help="Video framerate. User will be prompted to choose from available options",
# type=str, required=False)
# parser.add_argument("-b", "--bitrate",
# help="Audio bitrate. User will be prompted to choose from available options",
# type=str, required=False)
# parser.add_argument("-s", "--samplerate",
# help="The samplerate of the audio to download. "
# "User will be prompted to choose from available options",
# type=str, required=False)
args = parser.parse_args()
def main(arguments):
"""
main function
:return:
"""
# init settings
downloader_options: dict = {
"verbose": arguments.verbose,
"merge_output_format": arguments.output_format or "mp4",
"paths": {
"home": arguments.directory or os_path_join(os_path_expanduser("~"), "Downloads"), # downloads directory
"temp": gettempdir() # os temp directory
},
"outtmpl": {
"default": f"{arguments.output}.%(ext)s" if arguments.output else "%(title)s.%(ext)s",
"chapter": "%(title)s - %(section_number)03d %(section_title)s [%(id)s].%(ext)s"
}
}
print(f"Using {downloader_options['paths']['temp']} as temporary working directory")
print(f"The video will be downloaded to {downloader_options['paths']['home']}")
# init downloader
init_downloader(downloader_options)
# get available streams
video_info: dict = get_video_info(arguments.video_url)
# get available streams
streams: list[dict] = video_info["streams"]
# get best video streams
best_video_streams: list[dict] = get_best_video_streams(streams)
# get best audio streams
best_audio_streams = get_best_audio_streams(streams)
# selected audio and video streams
video_stream: dict = filter_video_streams(best_video_streams) # , arguments.resolution, arguments.framerate)
audio_stream: dict = filter_audio_streams(best_audio_streams) # , arguments.bitrate, arguments.samplerate)
# download video
download_video(arguments.video_url, video_stream["format_id"], audio_stream["format_id"])
if __name__ == "__main__":
main(args)