]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
dcdb292f | 2 | # coding: utf-8 |
235b3ba4 | 3 | |
347182a0 | 4 | f'You are using an unsupported version of Python. Only Python versions 3.6 and above are supported by yt-dlp' # noqa: F541 |
a4bc4336 | 5 | |
235b3ba4 | 6 | __license__ = 'Public Domain' |
235b3ba4 | 7 | |
0d94f247 | 8 | import codecs |
8f563f32 | 9 | import io |
e9f4ccd1 | 10 | import itertools |
235b3ba4 | 11 | import os |
0f818663 | 12 | import random |
43820c03 | 13 | import re |
235b3ba4 | 14 | import sys |
235b3ba4 | 15 | |
2daabe49 PH |
16 | from .options import ( |
17 | parseOpts, | |
18 | ) | |
8c25f81b | 19 | from .compat import ( |
e68301af | 20 | compat_getpass, |
b69fd25c | 21 | compat_os_name, |
e9f4ccd1 | 22 | compat_shlex_quote, |
e07e9313 | 23 | workaround_optparse_bug9161, |
8c25f81b | 24 | ) |
f59f5ef8 | 25 | from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS |
8c25f81b | 26 | from .utils import ( |
a4fd0415 PH |
27 | DateRange, |
28 | decodeOption, | |
f304da8a | 29 | DownloadCancelled, |
a4fd0415 | 30 | DownloadError, |
76a264ac | 31 | error_to_compat_str, |
590bc6f6 | 32 | expand_path, |
717216b0 | 33 | GeoUtils, |
31c49255 | 34 | float_or_none, |
35 | int_or_none, | |
347de493 | 36 | match_filter_func, |
2d9ec704 | 37 | parse_duration, |
a4fd0415 | 38 | preferredencoding, |
62e609ab | 39 | read_batch_urls, |
df692c5a | 40 | render_table, |
a4fd0415 | 41 | SameFileError, |
e3946f98 | 42 | setproctitle, |
a4fd0415 PH |
43 | std_headers, |
44 | write_string, | |
a4fd0415 | 45 | ) |
c19bc311 | 46 | from .update import run_update |
92a86f4c | 47 | from .downloader import ( |
a4fd0415 PH |
48 | FileDownloader, |
49 | ) | |
2bad0e5d | 50 | from .extractor import gen_extractors, list_extractors |
eb8a4433 | 51 | from .extractor.common import InfoExtractor |
1b6712ab | 52 | from .extractor.adobepass import MSO_INFO |
e9f4ccd1 | 53 | from .postprocessor import ( |
81a23040 | 54 | FFmpegExtractAudioPP, |
55 | FFmpegSubtitlesConvertorPP, | |
56 | FFmpegThumbnailsConvertorPP, | |
57 | FFmpegVideoConvertorPP, | |
58 | FFmpegVideoRemuxerPP, | |
e9f4ccd1 | 59 | MetadataFromFieldPP, |
60 | MetadataParserPP, | |
81a23040 | 61 | ) |
8222d8de | 62 | from .YoutubeDL import YoutubeDL |
a4fd0415 | 63 | |
235b3ba4 | 64 | |
b8ad4f02 | 65 | def _real_main(argv=None): |
0d94f247 PH |
66 | # Compatibility fixes for Windows |
67 | if sys.platform == 'win32': | |
067aa17e | 68 | # https://github.com/ytdl-org/youtube-dl/issues/820 |
0d94f247 PH |
69 | codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None) |
70 | ||
e07e9313 PH |
71 | workaround_optparse_bug9161() |
72 | ||
7a5c1cfe | 73 | setproctitle('yt-dlp') |
e3946f98 | 74 | |
b8ad4f02 | 75 | parser, opts, args = parseOpts(argv) |
ee8dd27a | 76 | warnings, deprecation_warnings = [], [] |
59ae15a5 | 77 | |
59ae15a5 PH |
78 | # Set user agent |
79 | if opts.user_agent is not None: | |
80 | std_headers['User-Agent'] = opts.user_agent | |
1865ed31 | 81 | |
28535652 BH |
82 | # Set referer |
83 | if opts.referer is not None: | |
84 | std_headers['Referer'] = opts.referer | |
59ae15a5 | 85 | |
410afb20 | 86 | # Custom HTTP headers |
45016689 | 87 | std_headers.update(opts.headers) |
410afb20 | 88 | |
59ae15a5 PH |
89 | # Dump user agent |
90 | if opts.dump_user_agent: | |
7b0d1c28 | 91 | write_string(std_headers['User-Agent'] + '\n', out=sys.stdout) |
59ae15a5 PH |
92 | sys.exit(0) |
93 | ||
94 | # Batch file verification | |
62e609ab | 95 | batch_urls = [] |
59ae15a5 PH |
96 | if opts.batchfile is not None: |
97 | try: | |
98 | if opts.batchfile == '-': | |
b69fd25c | 99 | write_string('Reading URLs from stdin - EOF (%s) to end:\n' % ( |
100 | 'Ctrl+Z' if compat_os_name == 'nt' else 'Ctrl+D')) | |
59ae15a5 PH |
101 | batchfd = sys.stdin |
102 | else: | |
e2eca6f6 | 103 | batchfd = io.open( |
590bc6f6 | 104 | expand_path(opts.batchfile), |
e2eca6f6 | 105 | 'r', encoding='utf-8', errors='ignore') |
62e609ab | 106 | batch_urls = read_batch_urls(batchfd) |
05afc96b | 107 | if opts.verbose: |
a4bc4336 | 108 | write_string('[debug] Batch file urls: ' + repr(batch_urls) + '\n') |
59ae15a5 | 109 | except IOError: |
826dcff9 | 110 | sys.exit('ERROR: batch file %s could not be read' % opts.batchfile) |
b2fc1c4f | 111 | all_urls = batch_urls + [url.strip() for url in args] # batch_urls are already striped in read_batch_urls |
c774b3c6 | 112 | _enc = preferredencoding() |
41292a38 | 113 | all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls] |
59ae15a5 | 114 | |
59ae15a5 | 115 | if opts.list_extractors: |
05900629 | 116 | for ie in list_extractors(opts.age_limit): |
251ae04e | 117 | write_string(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie.working() else '') + '\n', out=sys.stdout) |
1a2c3c0f | 118 | matchedUrls = [url for url in all_urls if ie.suitable(url)] |
59ae15a5 | 119 | for mu in matchedUrls: |
7b0d1c28 | 120 | write_string(' ' + mu + '\n', out=sys.stdout) |
59ae15a5 | 121 | sys.exit(0) |
0f818663 | 122 | if opts.list_extractor_descriptions: |
05900629 | 123 | for ie in list_extractors(opts.age_limit): |
251ae04e | 124 | if not ie.working(): |
0f818663 PH |
125 | continue |
126 | desc = getattr(ie, 'IE_DESC', ie.IE_NAME) | |
15870e90 PH |
127 | if desc is False: |
128 | continue | |
96565c7e | 129 | if getattr(ie, 'SEARCH_KEY', None) is not None: |
50a0f6df | 130 | _SEARCHES = ('cute kittens', 'slithering pythons', 'falling cat', 'angry poodle', 'purple fish', 'running tortoise', 'sleeping bunny', 'burping cow') |
a4bc4336 | 131 | _COUNTS = ('', '5', '10', 'all') |
96565c7e | 132 | desc += f'; "{ie.SEARCH_KEY}:" prefix (Example: "{ie.SEARCH_KEY}{random.choice(_COUNTS)}:{random.choice(_SEARCHES)}")' |
7b0d1c28 | 133 | write_string(desc + '\n', out=sys.stdout) |
0f818663 | 134 | sys.exit(0) |
87148bb7 | 135 | if opts.ap_list_mso: |
1b6712ab | 136 | table = [[mso_id, mso_info['name']] for mso_id, mso_info in MSO_INFO.items()] |
797c636b | 137 | write_string('Supported TV Providers:\n' + render_table(['mso', 'mso name'], table) + '\n', out=sys.stdout) |
1b6712ab | 138 | sys.exit(0) |
0f818663 | 139 | |
59ae15a5 | 140 | # Conflicting, missing and erroneous options |
1cefca9e | 141 | if opts.format == 'best': |
b28bac93 | 142 | warnings.append('.\n '.join(( |
1cefca9e | 143 | '"-f best" selects the best pre-merged format which is often not the best option', |
144 | 'To let yt-dlp download and merge the best available formats, simply do not pass any format selection', | |
b28bac93 | 145 | 'If you know what you are doing and want only the best pre-merged format, use "-f b" instead to suppress this warning'))) |
59ae15a5 | 146 | if opts.usenetrc and (opts.username is not None or opts.password is not None): |
a4bc4336 | 147 | parser.error('using .netrc conflicts with giving username/password') |
59ae15a5 | 148 | if opts.password is not None and opts.username is None: |
a4bc4336 | 149 | parser.error('account username missing\n') |
1b6712ab RA |
150 | if opts.ap_password is not None and opts.ap_username is None: |
151 | parser.error('TV Provider account username missing\n') | |
1a241a2d S |
152 | if opts.autonumber_size is not None: |
153 | if opts.autonumber_size <= 0: | |
154 | parser.error('auto number size must be positive') | |
155 | if opts.autonumber_start is not None: | |
156 | if opts.autonumber_start < 0: | |
157 | parser.error('auto number start must be positive or 0') | |
59ae15a5 | 158 | if opts.username is not None and opts.password is None: |
a4bc4336 | 159 | opts.password = compat_getpass('Type account password and press [Return]: ') |
1b6712ab RA |
160 | if opts.ap_username is not None and opts.ap_password is None: |
161 | opts.ap_password = compat_getpass('Type TV provider account password and press [Return]: ') | |
59ae15a5 PH |
162 | if opts.ratelimit is not None: |
163 | numeric_limit = FileDownloader.parse_bytes(opts.ratelimit) | |
164 | if numeric_limit is None: | |
a4bc4336 | 165 | parser.error('invalid rate limit specified') |
59ae15a5 | 166 | opts.ratelimit = numeric_limit |
51d9739f | 167 | if opts.throttledratelimit is not None: |
168 | numeric_limit = FileDownloader.parse_bytes(opts.throttledratelimit) | |
169 | if numeric_limit is None: | |
170 | parser.error('invalid rate limit specified') | |
171 | opts.throttledratelimit = numeric_limit | |
9e982f9e JC |
172 | if opts.min_filesize is not None: |
173 | numeric_limit = FileDownloader.parse_bytes(opts.min_filesize) | |
174 | if numeric_limit is None: | |
a4bc4336 | 175 | parser.error('invalid min_filesize specified') |
9e982f9e JC |
176 | opts.min_filesize = numeric_limit |
177 | if opts.max_filesize is not None: | |
178 | numeric_limit = FileDownloader.parse_bytes(opts.max_filesize) | |
179 | if numeric_limit is None: | |
a4bc4336 | 180 | parser.error('invalid max_filesize specified') |
9e982f9e | 181 | opts.max_filesize = numeric_limit |
065bc354 | 182 | if opts.sleep_interval is not None: |
183 | if opts.sleep_interval < 0: | |
1ad6b891 S |
184 | parser.error('sleep interval must be positive or 0') |
185 | if opts.max_sleep_interval is not None: | |
186 | if opts.max_sleep_interval < 0: | |
187 | parser.error('max sleep interval must be positive or 0') | |
050afa60 JZ |
188 | if opts.sleep_interval is None: |
189 | parser.error('min sleep interval must be specified, use --min-sleep-interval') | |
1ad6b891 S |
190 | if opts.max_sleep_interval < opts.sleep_interval: |
191 | parser.error('max sleep interval must be greater than or equal to min sleep interval') | |
192 | else: | |
193 | opts.max_sleep_interval = opts.sleep_interval | |
1cf376f5 | 194 | if opts.sleep_interval_subtitles is not None: |
195 | if opts.sleep_interval_subtitles < 0: | |
196 | parser.error('subtitles sleep interval must be positive or 0') | |
197 | if opts.sleep_interval_requests is not None: | |
198 | if opts.sleep_interval_requests < 0: | |
199 | parser.error('requests sleep interval must be positive or 0') | |
797c636b | 200 | if opts.ap_mso and opts.ap_mso not in MSO_INFO: |
87148bb7 | 201 | parser.error('Unsupported TV Provider, use --ap-list-mso to get a list of supported TV Providers') |
486fb179 | 202 | if opts.overwrites: # --yes-overwrites implies --no-continue |
0c3d0f51 | 203 | opts.continue_dl = False |
4cf1e5d2 | 204 | if opts.concurrent_fragment_downloads <= 0: |
f304da8a | 205 | parser.error('Concurrent fragments must be positive') |
f2ebc5c7 | 206 | if opts.wait_for_video is not None: |
38d79fd1 | 207 | min_wait, max_wait, *_ = map(parse_duration, opts.wait_for_video.split('-', 1) + [None]) |
208 | if min_wait is None or (max_wait is None and '-' in opts.wait_for_video): | |
f2ebc5c7 | 209 | parser.error('Invalid time range to wait') |
38d79fd1 | 210 | elif max_wait is not None and max_wait < min_wait: |
211 | parser.error('Minimum time range to wait must not be longer than the maximum') | |
f2ebc5c7 | 212 | opts.wait_for_video = (min_wait, max_wait) |
52bb437e | 213 | |
62bff2c1 | 214 | def parse_retries(retries, name=''): |
52bb437e S |
215 | if retries in ('inf', 'infinite'): |
216 | parsed_retries = float('inf') | |
baeaeffc PH |
217 | else: |
218 | try: | |
52bb437e | 219 | parsed_retries = int(retries) |
baeaeffc | 220 | except (TypeError, ValueError): |
62bff2c1 | 221 | parser.error('invalid %sretry count specified' % name) |
52bb437e S |
222 | return parsed_retries |
223 | if opts.retries is not None: | |
224 | opts.retries = parse_retries(opts.retries) | |
205a0654 EH |
225 | if opts.file_access_retries is not None: |
226 | opts.file_access_retries = parse_retries(opts.file_access_retries, 'file access ') | |
52bb437e | 227 | if opts.fragment_retries is not None: |
62bff2c1 | 228 | opts.fragment_retries = parse_retries(opts.fragment_retries, 'fragment ') |
229 | if opts.extractor_retries is not None: | |
230 | opts.extractor_retries = parse_retries(opts.extractor_retries, 'extractor ') | |
59ae15a5 PH |
231 | if opts.buffersize is not None: |
232 | numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize) | |
233 | if numeric_buffersize is None: | |
a4bc4336 | 234 | parser.error('invalid buffer size specified') |
59ae15a5 | 235 | opts.buffersize = numeric_buffersize |
ba515388 S |
236 | if opts.http_chunk_size is not None: |
237 | numeric_chunksize = FileDownloader.parse_bytes(opts.http_chunk_size) | |
238 | if not numeric_chunksize: | |
239 | parser.error('invalid http chunk size specified') | |
240 | opts.http_chunk_size = numeric_chunksize | |
a19fd00c | 241 | if opts.playliststart <= 0: |
f304da8a | 242 | raise parser.error('Playlist start must be positive') |
a19fd00c | 243 | if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart: |
f304da8a | 244 | raise parser.error('Playlist end must be greater than playlist start') |
59ae15a5 | 245 | if opts.extractaudio: |
0930b11f | 246 | opts.audioformat = opts.audioformat.lower() |
81a23040 | 247 | if opts.audioformat not in ['best'] + list(FFmpegExtractAudioPP.SUPPORTED_EXTS): |
a4bc4336 | 248 | parser.error('invalid audio format specified') |
59ae15a5 PH |
249 | if opts.audioquality: |
250 | opts.audioquality = opts.audioquality.strip('k').strip('K') | |
9af98e17 | 251 | audioquality = int_or_none(float_or_none(opts.audioquality)) # int_or_none prevents inf, nan |
252 | if audioquality is None or audioquality < 0: | |
a4bc4336 | 253 | parser.error('invalid audio quality specified') |
7851b379 | 254 | if opts.recodevideo is not None: |
81a23040 | 255 | opts.recodevideo = opts.recodevideo.replace(' ', '') |
256 | if not re.match(FFmpegVideoConvertorPP.FORMAT_RE, opts.recodevideo): | |
257 | parser.error('invalid video remux format specified') | |
17912249 | 258 | if opts.remuxvideo is not None: |
df692c5a | 259 | opts.remuxvideo = opts.remuxvideo.replace(' ', '') |
81a23040 | 260 | if not re.match(FFmpegVideoRemuxerPP.FORMAT_RE, opts.remuxvideo): |
17912249 | 261 | parser.error('invalid video remux format specified') |
e9fade72 | 262 | if opts.convertsubtitles is not None: |
81a23040 | 263 | if opts.convertsubtitles not in FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS: |
e9fade72 | 264 | parser.error('invalid subtitle format specified') |
8fa43c73 | 265 | if opts.convertthumbnails is not None: |
81a23040 | 266 | if opts.convertthumbnails not in FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS: |
8fa43c73 | 267 | parser.error('invalid thumbnail format specified') |
982ee69a | 268 | if opts.cookiesfrombrowser is not None: |
f59f5ef8 MB |
269 | mobj = re.match(r'(?P<name>[^+:]+)(\s*\+\s*(?P<keyring>[^:]+))?(\s*:(?P<profile>.+))?', opts.cookiesfrombrowser) |
270 | if mobj is None: | |
271 | parser.error(f'invalid cookies from browser arguments: {opts.cookiesfrombrowser}') | |
272 | browser_name, keyring, profile = mobj.group('name', 'keyring', 'profile') | |
273 | browser_name = browser_name.lower() | |
274 | if browser_name not in SUPPORTED_BROWSERS: | |
275 | parser.error(f'unsupported browser specified for cookies: "{browser_name}". ' | |
276 | f'Supported browsers are: {", ".join(sorted(SUPPORTED_BROWSERS))}') | |
277 | if keyring is not None: | |
278 | keyring = keyring.upper() | |
279 | if keyring not in SUPPORTED_KEYRINGS: | |
280 | parser.error(f'unsupported keyring specified for cookies: "{keyring}". ' | |
281 | f'Supported keyrings are: {", ".join(sorted(SUPPORTED_KEYRINGS))}') | |
282 | opts.cookiesfrombrowser = (browser_name, profile, keyring) | |
717216b0 | 283 | geo_bypass_code = opts.geo_bypass_ip_block or opts.geo_bypass_country |
284 | if geo_bypass_code is not None: | |
285 | try: | |
286 | GeoUtils.random_ipv4(geo_bypass_code) | |
287 | except Exception: | |
288 | parser.error('unsupported geo-bypass country or ip-block') | |
982ee69a | 289 | |
bd558525 JMF |
290 | if opts.date is not None: |
291 | date = DateRange.day(opts.date) | |
292 | else: | |
293 | date = DateRange(opts.dateafter, opts.datebefore) | |
59ae15a5 | 294 | |
31654882 | 295 | compat_opts = opts.compat_opts |
53ed7066 | 296 | |
19b824f6 | 297 | def report_conflict(arg1, arg2): |
298 | warnings.append(f'{arg2} is ignored since {arg1} was given') | |
299 | ||
53ed7066 | 300 | def _unused_compat_opt(name): |
301 | if name not in compat_opts: | |
302 | return False | |
303 | compat_opts.discard(name) | |
304 | compat_opts.update(['*%s' % name]) | |
305 | return True | |
306 | ||
e4f02757 | 307 | def set_default_compat(compat_name, opt_name, default=True, remove_compat=True): |
53ed7066 | 308 | attr = getattr(opts, opt_name) |
309 | if compat_name in compat_opts: | |
310 | if attr is None: | |
311 | setattr(opts, opt_name, not default) | |
312 | return True | |
313 | else: | |
314 | if remove_compat: | |
315 | _unused_compat_opt(compat_name) | |
316 | return False | |
317 | elif attr is None: | |
318 | setattr(opts, opt_name, default) | |
319 | return None | |
320 | ||
b1940459 | 321 | set_default_compat('abort-on-error', 'ignoreerrors', 'only_download') |
53ed7066 | 322 | set_default_compat('no-playlist-metafiles', 'allow_playlist_files') |
e4f02757 | 323 | set_default_compat('no-clean-infojson', 'clean_infojson') |
dac5df5a | 324 | if 'no-attach-info-json' in compat_opts: |
325 | if opts.embed_infojson: | |
326 | _unused_compat_opt('no-attach-info-json') | |
327 | else: | |
328 | opts.embed_infojson = False | |
53ed7066 | 329 | if 'format-sort' in compat_opts: |
330 | opts.format_sort.extend(InfoExtractor.FormatSort.ytdl_default) | |
331 | _video_multistreams_set = set_default_compat('multistreams', 'allow_multiple_video_streams', False, remove_compat=False) | |
332 | _audio_multistreams_set = set_default_compat('multistreams', 'allow_multiple_audio_streams', False, remove_compat=False) | |
333 | if _video_multistreams_set is False and _audio_multistreams_set is False: | |
334 | _unused_compat_opt('multistreams') | |
335 | outtmpl_default = opts.outtmpl.get('default') | |
19b824f6 | 336 | if opts.useid: |
337 | if outtmpl_default is None: | |
338 | outtmpl_default = opts.outtmpl['default'] = '%(id)s.%(ext)s' | |
339 | else: | |
340 | report_conflict('--output', '--id') | |
53ed7066 | 341 | if 'filename' in compat_opts: |
342 | if outtmpl_default is None: | |
19b824f6 | 343 | outtmpl_default = opts.outtmpl['default'] = '%(title)s-%(id)s.%(ext)s' |
53ed7066 | 344 | else: |
345 | _unused_compat_opt('filename') | |
346 | ||
76a264ac | 347 | def validate_outtmpl(tmpl, msg): |
348 | err = YoutubeDL.validate_outtmpl(tmpl) | |
349 | if err: | |
350 | parser.error('invalid %s %r: %s' % (msg, tmpl, error_to_compat_str(err))) | |
351 | ||
352 | for k, tmpl in opts.outtmpl.items(): | |
819e0531 | 353 | validate_outtmpl(tmpl, f'{k} output template') |
ad3dc496 | 354 | opts.forceprint = opts.forceprint or [] |
355 | for tmpl in opts.forceprint or []: | |
76a264ac | 356 | validate_outtmpl(tmpl, 'print template') |
7a340e0d | 357 | validate_outtmpl(opts.sponsorblock_chapter_title, 'SponsorBlock chapter title') |
819e0531 | 358 | for k, tmpl in opts.progress_template.items(): |
359 | k = f'{k[:-6]} console title' if '-title' in k else f'{k} progress' | |
360 | validate_outtmpl(tmpl, f'{k} template') | |
76a264ac | 361 | |
de3ef3ed PH |
362 | if opts.extractaudio and not opts.keepvideo and opts.format is None: |
363 | opts.format = 'bestaudio/best' | |
364 | ||
de6000d9 | 365 | if outtmpl_default is not None and not os.path.splitext(outtmpl_default)[1] and opts.extractaudio: |
a4bc4336 PH |
366 | parser.error('Cannot download a video and extract audio into the same' |
367 | ' file! Use "{0}.%(ext)s" instead of "{0}" as the output' | |
de6000d9 | 368 | ' template'.format(outtmpl_default)) |
0202b52a | 369 | |
eb8a4433 | 370 | for f in opts.format_sort: |
371 | if re.match(InfoExtractor.FormatSort.regex, f) is None: | |
372 | parser.error('invalid format sort string "%s" specified' % f) | |
29c7a63d | 373 | |
e9f4ccd1 | 374 | def metadataparser_actions(f): |
375 | if isinstance(f, str): | |
376 | cmd = '--parse-metadata %s' % compat_shlex_quote(f) | |
377 | try: | |
378 | actions = [MetadataFromFieldPP.to_action(f)] | |
379 | except Exception as err: | |
380 | parser.error(f'{cmd} is invalid; {err}') | |
381 | else: | |
382 | cmd = '--replace-in-metadata %s' % ' '.join(map(compat_shlex_quote, f)) | |
383 | actions = ((MetadataParserPP.Actions.REPLACE, x, *f[1:]) for x in f[0].split(',')) | |
384 | ||
385 | for action in actions: | |
386 | try: | |
387 | MetadataParserPP.validate_action(*action) | |
388 | except Exception as err: | |
389 | parser.error(f'{cmd} is invalid; {err}') | |
390 | yield action | |
391 | ||
392 | if opts.parse_metadata is None: | |
393 | opts.parse_metadata = [] | |
5bfa4862 | 394 | if opts.metafromtitle is not None: |
e9f4ccd1 | 395 | opts.parse_metadata.append('title:%s' % opts.metafromtitle) |
396 | opts.parse_metadata = list(itertools.chain(*map(metadataparser_actions, opts.parse_metadata))) | |
5bfa4862 | 397 | |
d2a1fad9 | 398 | any_getting = opts.forceprint or opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_json |
c0bdf32a | 399 | any_printing = opts.print_json |
590bc6f6 | 400 | download_archive_fn = expand_path(opts.download_archive) if opts.download_archive is not None else opts.download_archive |
525ef922 | 401 | |
f0884c8b | 402 | # If JSON is not printed anywhere, but comments are requested, save it to file |
403 | printing_json = opts.dumpjson or opts.print_json or opts.dump_single_json | |
404 | if opts.getcomments and not printing_json: | |
405 | opts.writeinfojson = True | |
406 | ||
7a340e0d NA |
407 | if opts.no_sponsorblock: |
408 | opts.sponsorblock_mark = set() | |
409 | opts.sponsorblock_remove = set() | |
410 | sponsorblock_query = opts.sponsorblock_mark | opts.sponsorblock_remove | |
411 | ||
7a340e0d NA |
412 | opts.remove_chapters = opts.remove_chapters or [] |
413 | ||
7a340e0d NA |
414 | if (opts.remove_chapters or sponsorblock_query) and opts.sponskrub is not False: |
415 | if opts.sponskrub: | |
416 | if opts.remove_chapters: | |
417 | report_conflict('--remove-chapters', '--sponskrub') | |
418 | if opts.sponsorblock_mark: | |
419 | report_conflict('--sponsorblock-mark', '--sponskrub') | |
420 | if opts.sponsorblock_remove: | |
421 | report_conflict('--sponsorblock-remove', '--sponskrub') | |
422 | opts.sponskrub = False | |
72755351 | 423 | if opts.sponskrub_cut and opts.split_chapters and opts.sponskrub is not False: |
424 | report_conflict('--split-chapter', '--sponskrub-cut') | |
425 | opts.sponskrub_cut = False | |
426 | ||
7a340e0d NA |
427 | if opts.remuxvideo and opts.recodevideo: |
428 | report_conflict('--recode-video', '--remux-video') | |
429 | opts.remuxvideo = False | |
430 | ||
63ad4d43 | 431 | if opts.allow_unplayable_formats: |
9222c381 | 432 | def report_unplayable_conflict(opt_name, arg, default=False, allowed=None): |
433 | val = getattr(opts, opt_name) | |
93e597ba | 434 | if (not allowed and val) or (allowed and not allowed(val)): |
9222c381 | 435 | report_conflict('--allow-unplayable-formats', arg) |
436 | setattr(opts, opt_name, default) | |
437 | ||
438 | report_unplayable_conflict('extractaudio', '--extract-audio') | |
439 | report_unplayable_conflict('remuxvideo', '--remux-video') | |
440 | report_unplayable_conflict('recodevideo', '--recode-video') | |
441 | report_unplayable_conflict('addmetadata', '--embed-metadata') | |
442 | report_unplayable_conflict('addchapters', '--embed-chapters') | |
443 | report_unplayable_conflict('embed_infojson', '--embed-info-json') | |
444 | opts.embed_infojson = False | |
445 | report_unplayable_conflict('embedsubtitles', '--embed-subs') | |
446 | report_unplayable_conflict('embedthumbnail', '--embed-thumbnail') | |
447 | report_unplayable_conflict('xattrs', '--xattrs') | |
448 | report_unplayable_conflict('fixup', '--fixup', default='never', allowed=lambda x: x in (None, 'never', 'ignore')) | |
63ad4d43 | 449 | opts.fixup = 'never' |
9222c381 | 450 | report_unplayable_conflict('remove_chapters', '--remove-chapters', default=[]) |
451 | report_unplayable_conflict('sponsorblock_remove', '--sponsorblock-remove', default=set()) | |
452 | report_unplayable_conflict('sponskrub', '--sponskrub', default=set()) | |
63ad4d43 | 453 | opts.sponskrub = False |
454 | ||
9222c381 | 455 | if (opts.addmetadata or opts.sponsorblock_mark) and opts.addchapters is None: |
456 | opts.addchapters = True | |
457 | ||
4f026faf | 458 | # PostProcessors |
3ae5e797 | 459 | postprocessors = list(opts.add_postprocessors) |
7a340e0d NA |
460 | if sponsorblock_query: |
461 | postprocessors.append({ | |
462 | 'key': 'SponsorBlock', | |
463 | 'categories': sponsorblock_query, | |
464 | 'api': opts.sponsorblock_api, | |
465 | # Run this immediately after extraction is complete | |
466 | 'when': 'pre_process' | |
467 | }) | |
e9f4ccd1 | 468 | if opts.parse_metadata: |
e7db87f7 | 469 | postprocessors.append({ |
e9f4ccd1 | 470 | 'key': 'MetadataParser', |
471 | 'actions': opts.parse_metadata, | |
56d868db | 472 | # Run this immediately after extraction is complete |
473 | 'when': 'pre_process' | |
474 | }) | |
475 | if opts.convertsubtitles: | |
476 | postprocessors.append({ | |
477 | 'key': 'FFmpegSubtitlesConvertor', | |
478 | 'format': opts.convertsubtitles, | |
479 | # Run this before the actual video download | |
480 | 'when': 'before_dl' | |
e7db87f7 | 481 | }) |
8fa43c73 | 482 | if opts.convertthumbnails: |
483 | postprocessors.append({ | |
484 | 'key': 'FFmpegThumbnailsConvertor', | |
485 | 'format': opts.convertthumbnails, | |
486 | # Run this before the actual video download | |
487 | 'when': 'before_dl' | |
488 | }) | |
5520aa2d | 489 | # Must be after all other before_dl |
490 | if opts.exec_before_dl_cmd: | |
491 | postprocessors.append({ | |
ad3dc496 | 492 | 'key': 'Exec', |
5520aa2d | 493 | 'exec_cmd': opts.exec_before_dl_cmd, |
494 | 'when': 'before_dl' | |
495 | }) | |
4f026faf PH |
496 | if opts.extractaudio: |
497 | postprocessors.append({ | |
498 | 'key': 'FFmpegExtractAudio', | |
499 | 'preferredcodec': opts.audioformat, | |
500 | 'preferredquality': opts.audioquality, | |
501 | 'nopostoverwrites': opts.nopostoverwrites, | |
502 | }) | |
efe87a10 FS |
503 | if opts.remuxvideo: |
504 | postprocessors.append({ | |
505 | 'key': 'FFmpegVideoRemuxer', | |
506 | 'preferedformat': opts.remuxvideo, | |
507 | }) | |
4f026faf PH |
508 | if opts.recodevideo: |
509 | postprocessors.append({ | |
510 | 'key': 'FFmpegVideoConvertor', | |
511 | 'preferedformat': opts.recodevideo, | |
512 | }) | |
7a340e0d | 513 | # If ModifyChapters is going to remove chapters, subtitles must already be in the container. |
4f026faf | 514 | if opts.embedsubtitles: |
b51d2ae3 | 515 | already_have_subtitle = opts.writesubtitles and 'no-keep-subs' not in compat_opts |
4f026faf PH |
516 | postprocessors.append({ |
517 | 'key': 'FFmpegEmbedSubtitle', | |
56d868db | 518 | # already_have_subtitle = True prevents the file from being deleted after embedding |
cffab0ee | 519 | 'already_have_subtitle': already_have_subtitle |
4f026faf | 520 | }) |
b51d2ae3 | 521 | if not opts.writeautomaticsub and 'no-keep-subs' not in compat_opts: |
cffab0ee | 522 | opts.writesubtitles = True |
523 | # --all-sub automatically sets --write-sub if --write-auto-sub is not given | |
524 | # this was the old behaviour if only --all-sub was given. | |
525 | if opts.allsubtitles and not opts.writeautomaticsub: | |
526 | opts.writesubtitles = True | |
7a340e0d | 527 | # ModifyChapters must run before FFmpegMetadataPP |
2d9ec704 | 528 | remove_chapters_patterns, remove_ranges = [], [] |
7a340e0d | 529 | for regex in opts.remove_chapters: |
2d9ec704 | 530 | if regex.startswith('*'): |
531 | dur = list(map(parse_duration, regex[1:].split('-'))) | |
532 | if len(dur) == 2 and all(t is not None for t in dur): | |
533 | remove_ranges.append(tuple(dur)) | |
534 | continue | |
b69fd25c | 535 | parser.error(f'invalid --remove-chapters time range {regex!r}. Must be of the form *start-end') |
7a340e0d NA |
536 | try: |
537 | remove_chapters_patterns.append(re.compile(regex)) | |
538 | except re.error as err: | |
539 | parser.error(f'invalid --remove-chapters regex {regex!r} - {err}') | |
540 | if opts.remove_chapters or sponsorblock_query: | |
541 | postprocessors.append({ | |
542 | 'key': 'ModifyChapters', | |
543 | 'remove_chapters_patterns': remove_chapters_patterns, | |
544 | 'remove_sponsor_segments': opts.sponsorblock_remove, | |
2d9ec704 | 545 | 'remove_ranges': remove_ranges, |
7a340e0d NA |
546 | 'sponsorblock_chapter_title': opts.sponsorblock_chapter_title, |
547 | 'force_keyframes': opts.force_keyframes_at_cuts | |
548 | }) | |
549 | # FFmpegMetadataPP should be run after FFmpegVideoConvertorPP and | |
550 | # FFmpegExtractAudioPP as containers before conversion may not support | |
551 | # metadata (3gp, webm, etc.) | |
552 | # By default ffmpeg preserves metadata applicable for both | |
553 | # source and target containers. From this point the container won't change, | |
554 | # so metadata can be added here. | |
dac5df5a | 555 | if opts.addmetadata or opts.addchapters or opts.embed_infojson: |
556 | if opts.embed_infojson is None: | |
557 | opts.embed_infojson = 'if_exists' | |
7a340e0d NA |
558 | postprocessors.append({ |
559 | 'key': 'FFmpegMetadata', | |
560 | 'add_chapters': opts.addchapters, | |
561 | 'add_metadata': opts.addmetadata, | |
dac5df5a | 562 | 'add_infojson': opts.embed_infojson, |
7a340e0d | 563 | }) |
ee8dd27a | 564 | # Deprecated |
f4e4be19 | 565 | # This should be above EmbedThumbnail since sponskrub removes the thumbnail attachment |
566 | # but must be below EmbedSubtitle and FFmpegMetadata | |
567 | # See https://github.com/yt-dlp/yt-dlp/issues/204 , https://github.com/faissaloo/SponSkrub/issues/29 | |
a9e7f546 | 568 | # If opts.sponskrub is None, sponskrub is used, but it silently fails if the executable can't be found |
569 | if opts.sponskrub is not False: | |
570 | postprocessors.append({ | |
571 | 'key': 'SponSkrub', | |
572 | 'path': opts.sponskrub_path, | |
573 | 'args': opts.sponskrub_args, | |
574 | 'cut': opts.sponskrub_cut, | |
575 | 'force': opts.sponskrub_force, | |
576 | 'ignoreerror': opts.sponskrub is None, | |
ee8dd27a | 577 | '_from_cli': True, |
a9e7f546 | 578 | }) |
f4e4be19 | 579 | if opts.embedthumbnail: |
f4e4be19 | 580 | postprocessors.append({ |
581 | 'key': 'EmbedThumbnail', | |
56d868db | 582 | # already_have_thumbnail = True prevents the file from being deleted after embedding |
acc0d6a4 | 583 | 'already_have_thumbnail': opts.writethumbnail |
f4e4be19 | 584 | }) |
acc0d6a4 | 585 | if not opts.writethumbnail: |
f4e4be19 | 586 | opts.writethumbnail = True |
80c03fa9 | 587 | opts.outtmpl['pl_thumbnail'] = '' |
72755351 | 588 | if opts.split_chapters: |
7a340e0d NA |
589 | postprocessors.append({ |
590 | 'key': 'FFmpegSplitChapters', | |
591 | 'force_keyframes': opts.force_keyframes_at_cuts, | |
592 | }) | |
72755351 | 593 | # XAttrMetadataPP should be run after post-processors that may change file contents |
594 | if opts.xattrs: | |
595 | postprocessors.append({'key': 'XAttrMetadata'}) | |
ad3dc496 | 596 | # Exec must be the last PP |
4f026faf PH |
597 | if opts.exec_cmd: |
598 | postprocessors.append({ | |
ad3dc496 | 599 | 'key': 'Exec', |
4f026faf | 600 | 'exec_cmd': opts.exec_cmd, |
56d868db | 601 | # Run this only after the files have been moved to their final locations |
602 | 'when': 'after_move' | |
4f026faf | 603 | }) |
1b77b347 | 604 | |
06869367 | 605 | def report_args_compat(arg, name): |
0d1bb027 | 606 | warnings.append('%s given without specifying name. The arguments will be given to all %s' % (arg, name)) |
607 | ||
b8f6bbe6 | 608 | if 'default' in opts.external_downloader_args: |
0d1bb027 | 609 | report_args_compat('--downloader-args', 'external downloaders') |
b8f6bbe6 | 610 | |
45016689 | 611 | if 'default-compat' in opts.postprocessor_args and 'default' not in opts.postprocessor_args: |
06869367 | 612 | report_args_compat('--post-processor-args', 'post-processors') |
45016689 | 613 | opts.postprocessor_args.setdefault('sponskrub', []) |
614 | opts.postprocessor_args['default'] = opts.postprocessor_args['default-compat'] | |
1b77b347 | 615 | |
ee8dd27a | 616 | def report_deprecation(val, old, new=None): |
617 | if not val: | |
618 | return | |
619 | deprecation_warnings.append( | |
620 | f'{old} is deprecated and may be removed in a future version. Use {new} instead' if new | |
621 | else f'{old} is deprecated and may not work as expected') | |
622 | ||
623 | report_deprecation(opts.sponskrub, '--sponskrub', '--sponsorblock-mark or --sponsorblock-remove') | |
624 | report_deprecation(not opts.prefer_ffmpeg, '--prefer-avconv', 'ffmpeg') | |
625 | report_deprecation(opts.include_ads, '--include-ads') | |
626 | # report_deprecation(opts.call_home, '--call-home') # We may re-implement this in future | |
627 | # report_deprecation(opts.writeannotations, '--write-annotations') # It's just that no website has it | |
628 | ||
df692c5a | 629 | final_ext = ( |
81a23040 | 630 | opts.recodevideo if opts.recodevideo in FFmpegVideoConvertorPP.SUPPORTED_EXTS |
631 | else opts.remuxvideo if opts.remuxvideo in FFmpegVideoRemuxerPP.SUPPORTED_EXTS | |
632 | else opts.audioformat if (opts.extractaudio and opts.audioformat != 'best') | |
633 | else None) | |
f6d7624f | 634 | |
347de493 PH |
635 | match_filter = ( |
636 | None if opts.match_filter is None | |
637 | else match_filter_func(opts.match_filter)) | |
4f026faf | 638 | |
bdde425c | 639 | ydl_opts = { |
59ae15a5 | 640 | 'usenetrc': opts.usenetrc, |
0001fcb5 | 641 | 'netrc_location': opts.netrc_location, |
59ae15a5 PH |
642 | 'username': opts.username, |
643 | 'password': opts.password, | |
83317f69 | 644 | 'twofactor': opts.twofactor, |
c6c19746 | 645 | 'videopassword': opts.videopassword, |
797c636b | 646 | 'ap_mso': opts.ap_mso, |
1b6712ab RA |
647 | 'ap_username': opts.ap_username, |
648 | 'ap_password': opts.ap_password, | |
c0bdf32a | 649 | 'quiet': (opts.quiet or any_getting or any_printing), |
ad8915b7 | 650 | 'no_warnings': opts.no_warnings, |
59ae15a5 PH |
651 | 'forceurl': opts.geturl, |
652 | 'forcetitle': opts.gettitle, | |
1a2adf3f | 653 | 'forceid': opts.getid, |
59ae15a5 PH |
654 | 'forcethumbnail': opts.getthumbnail, |
655 | 'forcedescription': opts.getdescription, | |
525ef922 | 656 | 'forceduration': opts.getduration, |
59ae15a5 PH |
657 | 'forcefilename': opts.getfilename, |
658 | 'forceformat': opts.getformat, | |
d2a1fad9 | 659 | 'forceprint': opts.forceprint, |
c0bdf32a | 660 | 'forcejson': opts.dumpjson or opts.print_json, |
63e0be34 | 661 | 'dump_single_json': opts.dump_single_json, |
2d30509f | 662 | 'force_write_download_archive': opts.force_write_download_archive, |
b7b04c78 | 663 | 'simulate': (any_getting or None) if opts.simulate is None else opts.simulate, |
1bdeb7be | 664 | 'skip_download': opts.skip_download, |
59ae15a5 | 665 | 'format': opts.format, |
63ad4d43 | 666 | 'allow_unplayable_formats': opts.allow_unplayable_formats, |
b7da73eb | 667 | 'ignore_no_formats_error': opts.ignore_no_formats_error, |
eb8a4433 | 668 | 'format_sort': opts.format_sort, |
669 | 'format_sort_force': opts.format_sort_force, | |
909d24dd | 670 | 'allow_multiple_video_streams': opts.allow_multiple_video_streams, |
671 | 'allow_multiple_audio_streams': opts.allow_multiple_audio_streams, | |
e8e73840 | 672 | 'check_formats': opts.check_formats, |
59ae15a5 | 673 | 'listformats': opts.listformats, |
76d321f6 | 674 | 'listformats_table': opts.listformats_table, |
486fb179 | 675 | 'outtmpl': opts.outtmpl, |
a820dc72 | 676 | 'outtmpl_na_placeholder': opts.outtmpl_na_placeholder, |
0202b52a | 677 | 'paths': opts.paths, |
213c31ae | 678 | 'autonumber_size': opts.autonumber_size, |
acbb2374 | 679 | 'autonumber_start': opts.autonumber_start, |
59ae15a5 | 680 | 'restrictfilenames': opts.restrictfilenames, |
c2934512 | 681 | 'windowsfilenames': opts.windowsfilenames, |
59ae15a5 | 682 | 'ignoreerrors': opts.ignoreerrors, |
d22dec74 | 683 | 'force_generic_extractor': opts.force_generic_extractor, |
59ae15a5 | 684 | 'ratelimit': opts.ratelimit, |
51d9739f | 685 | 'throttledratelimit': opts.throttledratelimit, |
0c3d0f51 | 686 | 'overwrites': opts.overwrites, |
52bb437e | 687 | 'retries': opts.retries, |
205a0654 | 688 | 'file_access_retries': opts.file_access_retries, |
52bb437e | 689 | 'fragment_retries': opts.fragment_retries, |
62bff2c1 | 690 | 'extractor_retries': opts.extractor_retries, |
9603b660 | 691 | 'skip_unavailable_fragments': opts.skip_unavailable_fragments, |
0eee52f3 | 692 | 'keep_fragments': opts.keep_fragments, |
4cf1e5d2 | 693 | 'concurrent_fragment_downloads': opts.concurrent_fragment_downloads, |
59ae15a5 PH |
694 | 'buffersize': opts.buffersize, |
695 | 'noresizebuffer': opts.noresizebuffer, | |
ba515388 | 696 | 'http_chunk_size': opts.http_chunk_size, |
59ae15a5 | 697 | 'continuedl': opts.continue_dl, |
819e0531 | 698 | 'noprogress': opts.quiet if opts.noprogress is None else opts.noprogress, |
5717d91a | 699 | 'progress_with_newline': opts.progress_with_newline, |
819e0531 | 700 | 'progress_template': opts.progress_template, |
59ae15a5 PH |
701 | 'playliststart': opts.playliststart, |
702 | 'playlistend': opts.playlistend, | |
ff815fe6 | 703 | 'playlistreverse': opts.playlist_reverse, |
75822ca7 | 704 | 'playlistrandom': opts.playlist_random, |
47192f92 | 705 | 'noplaylist': opts.noplaylist, |
de6000d9 | 706 | 'logtostderr': outtmpl_default == '-', |
59ae15a5 PH |
707 | 'consoletitle': opts.consoletitle, |
708 | 'nopart': opts.nopart, | |
709 | 'updatetime': opts.updatetime, | |
710 | 'writedescription': opts.writedescription, | |
1fb07d10 | 711 | 'writeannotations': opts.writeannotations, |
f0884c8b | 712 | 'writeinfojson': opts.writeinfojson, |
1ea24129 | 713 | 'allow_playlist_files': opts.allow_playlist_files, |
75d43ca0 | 714 | 'clean_infojson': opts.clean_infojson, |
06167fbb | 715 | 'getcomments': opts.getcomments, |
acc0d6a4 | 716 | 'writethumbnail': opts.writethumbnail is True, |
717 | 'write_all_thumbnails': opts.writethumbnail == 'all', | |
732044af | 718 | 'writelink': opts.writelink, |
719 | 'writeurllink': opts.writeurllink, | |
720 | 'writewebloclink': opts.writewebloclink, | |
721 | 'writedesktoplink': opts.writedesktoplink, | |
59ae15a5 | 722 | 'writesubtitles': opts.writesubtitles, |
b004821f | 723 | 'writeautomaticsub': opts.writeautomaticsub, |
ae608b80 | 724 | 'allsubtitles': opts.allsubtitles, |
2a4093ea | 725 | 'listsubtitles': opts.listsubtitles, |
9e62bc44 | 726 | 'subtitlesformat': opts.subtitlesformat, |
d6e203b3 | 727 | 'subtitleslangs': opts.subtitleslangs, |
8271226a PH |
728 | 'matchtitle': decodeOption(opts.matchtitle), |
729 | 'rejecttitle': decodeOption(opts.rejecttitle), | |
59ae15a5 PH |
730 | 'max_downloads': opts.max_downloads, |
731 | 'prefer_free_formats': opts.prefer_free_formats, | |
bdc3fd2f | 732 | 'trim_file_name': opts.trim_file_name, |
59ae15a5 | 733 | 'verbose': opts.verbose, |
855703e5 | 734 | 'dump_intermediate_pages': opts.dump_intermediate_pages, |
d41e6efc | 735 | 'write_pages': opts.write_pages, |
8d5d3a5d | 736 | 'test': opts.test, |
7851b379 | 737 | 'keepvideo': opts.keepvideo, |
9e982f9e | 738 | 'min_filesize': opts.min_filesize, |
bd558525 | 739 | 'max_filesize': opts.max_filesize, |
5fe18bdb PH |
740 | 'min_views': opts.min_views, |
741 | 'max_views': opts.max_views, | |
11d9224e | 742 | 'daterange': date, |
7f747732 | 743 | 'cachedir': opts.cachedir, |
f8061589 | 744 | 'youtube_print_sig_code': opts.youtube_print_sig_code, |
8dbe9899 | 745 | 'age_limit': opts.age_limit, |
17093b83 | 746 | 'download_archive': download_archive_fn, |
ea6e0c2b | 747 | 'break_on_existing': opts.break_on_existing, |
8b0d7497 | 748 | 'break_on_reject': opts.break_on_reject, |
b222c271 | 749 | 'break_per_url': opts.break_per_url, |
26e2805c | 750 | 'skip_playlist_after_errors': opts.skip_playlist_after_errors, |
dca08720 | 751 | 'cookiefile': opts.cookiefile, |
982ee69a | 752 | 'cookiesfrombrowser': opts.cookiesfrombrowser, |
dca08720 | 753 | 'nocheckcertificate': opts.no_check_certificate, |
7e8c0af0 | 754 | 'prefer_insecure': opts.prefer_insecure, |
c2e52508 | 755 | 'proxy': opts.proxy, |
6ad14cab | 756 | 'socket_timeout': opts.socket_timeout, |
0783b09b | 757 | 'bidi_workaround': opts.bidi_workaround, |
a0ddb8a2 | 758 | 'debug_printtraffic': opts.debug_printtraffic, |
76b1bd67 | 759 | 'prefer_ffmpeg': opts.prefer_ffmpeg, |
7b0817e8 | 760 | 'include_ads': opts.include_ads, |
04b4d394 | 761 | 'default_search': opts.default_search, |
78895bd3 | 762 | 'dynamic_mpd': opts.dynamic_mpd, |
5d3a0e79 | 763 | 'extractor_args': opts.extractor_args, |
4919603f | 764 | 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest, |
78895bd3 | 765 | 'youtube_include_hls_manifest': opts.youtube_include_hls_manifest, |
62fec3b2 | 766 | 'encoding': opts.encoding, |
057a5206 | 767 | 'extract_flat': opts.extract_flat, |
adbc4ec4 | 768 | 'live_from_start': opts.live_from_start, |
f2ebc5c7 | 769 | 'wait_for_video': opts.wait_for_video, |
d77ab8e2 | 770 | 'mark_watched': opts.mark_watched, |
34c781a2 | 771 | 'merge_output_format': opts.merge_output_format, |
df692c5a | 772 | 'final_ext': final_ext, |
4f026faf | 773 | 'postprocessors': postprocessors, |
6271f1ca | 774 | 'fixup': opts.fixup, |
be4a824d | 775 | 'source_address': opts.source_address, |
58b1f00d | 776 | 'call_home': opts.call_home, |
1cf376f5 | 777 | 'sleep_interval_requests': opts.sleep_interval_requests, |
5f0d813d | 778 | 'sleep_interval': opts.sleep_interval, |
065bc354 | 779 | 'max_sleep_interval': opts.max_sleep_interval, |
0c9df79e | 780 | 'sleep_interval_subtitles': opts.sleep_interval_subtitles, |
222516d9 | 781 | 'external_downloader': opts.external_downloader, |
cfb56d1a | 782 | 'list_thumbnails': opts.list_thumbnails, |
c14e88f0 | 783 | 'playlist_items': opts.playlist_items, |
881e6a1f | 784 | 'xattr_set_filesize': opts.xattr_set_filesize, |
347de493 | 785 | 'match_filter': match_filter, |
7e5db8c9 | 786 | 'no_color': opts.no_color, |
73fac4e9 | 787 | 'ffmpeg_location': opts.ffmpeg_location, |
85729c51 | 788 | 'hls_prefer_native': opts.hls_prefer_native, |
7d106a65 | 789 | 'hls_use_mpegts': opts.hls_use_mpegts, |
310c2ed2 | 790 | 'hls_split_discontinuity': opts.hls_split_discontinuity, |
46ee996e | 791 | 'external_downloader_args': opts.external_downloader_args, |
45016689 | 792 | 'postprocessor_args': opts.postprocessor_args, |
91410c9b | 793 | 'cn_verification_proxy': opts.cn_verification_proxy, |
38cce791 | 794 | 'geo_verification_proxy': opts.geo_verification_proxy, |
0a840f58 S |
795 | 'geo_bypass': opts.geo_bypass, |
796 | 'geo_bypass_country': opts.geo_bypass_country, | |
5f95927a | 797 | 'geo_bypass_ip_block': opts.geo_bypass_ip_block, |
49a57e70 | 798 | '_warnings': warnings, |
ee8dd27a | 799 | '_deprecation_warnings': deprecation_warnings, |
53ed7066 | 800 | 'compat_opts': compat_opts, |
bdde425c | 801 | } |
59ae15a5 | 802 | |
bdde425c | 803 | with YoutubeDL(ydl_opts) as ydl: |
f304da8a | 804 | actual_use = all_urls or opts.load_info_filename |
bdde425c | 805 | |
052421ff PH |
806 | # Remove cache dir |
807 | if opts.rm_cachedir: | |
a0e07d31 | 808 | ydl.cache.remove() |
052421ff | 809 | |
e5813e53 | 810 | # Update version |
811 | if opts.update_self: | |
812 | # If updater returns True, exit. Required for windows | |
c19bc311 | 813 | if run_update(ydl): |
e5813e53 | 814 | if actual_use: |
6b027907 | 815 | sys.exit('ERROR: The program must exit for the update to complete') |
e5813e53 | 816 | sys.exit() |
817 | ||
bdde425c | 818 | # Maybe do nothing |
e5813e53 | 819 | if not actual_use: |
7d4111ed | 820 | if opts.update_self or opts.rm_cachedir: |
bdde425c | 821 | sys.exit() |
59ae15a5 | 822 | |
7d4111ed | 823 | ydl.warn_if_short_id(sys.argv[1:] if argv is None else argv) |
adc0ae3c PH |
824 | parser.error( |
825 | 'You must provide at least one URL.\n' | |
7a5c1cfe | 826 | 'Type yt-dlp --help to see a list of all options.') |
7d4111ed | 827 | |
bdde425c | 828 | try: |
1dcc4c0c | 829 | if opts.load_info_filename is not None: |
590bc6f6 | 830 | retcode = ydl.download_with_info_file(expand_path(opts.load_info_filename)) |
1dcc4c0c JMF |
831 | else: |
832 | retcode = ydl.download(all_urls) | |
f304da8a | 833 | except DownloadCancelled: |
8b0d7497 | 834 | ydl.to_screen('Aborting remaining downloads') |
bdde425c | 835 | retcode = 101 |
59ae15a5 | 836 | |
59ae15a5 | 837 | sys.exit(retcode) |
235b3ba4 | 838 | |
a27b9e8b | 839 | |
b8ad4f02 | 840 | def main(argv=None): |
59ae15a5 | 841 | try: |
b8ad4f02 | 842 | _real_main(argv) |
59ae15a5 PH |
843 | except DownloadError: |
844 | sys.exit(1) | |
aa9369a2 | 845 | except SameFileError as e: |
846 | sys.exit(f'ERROR: {e}') | |
59ae15a5 | 847 | except KeyboardInterrupt: |
a4bc4336 | 848 | sys.exit('\nERROR: Interrupted by user') |
aa9369a2 | 849 | except BrokenPipeError as e: |
cc3fa8d3 | 850 | # https://docs.python.org/3/library/signal.html#note-on-sigpipe |
851 | devnull = os.open(os.devnull, os.O_WRONLY) | |
852 | os.dup2(devnull, sys.stdout.fileno()) | |
aa9369a2 | 853 | sys.exit(f'\nERROR: {e}') |
2bad0e5d | 854 | |
582be358 | 855 | |
2bad0e5d | 856 | __all__ = ['main', 'YoutubeDL', 'gen_extractors', 'list_extractors'] |