| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Helpers for interacting with youtube-dl""" |
| 10 |
|
| 11 |
import shlex |
| 12 |
import itertools |
| 13 |
from . import text, util, exception |
| 14 |
|
| 15 |
|
| 16 |
def import_module(module_name): |
| 17 |
if module_name is None: |
| 18 |
try: |
| 19 |
return __import__("yt_dlp") |
| 20 |
except (ImportError, SyntaxError): |
| 21 |
return __import__("youtube_dl") |
| 22 |
return util.import_file(module_name) |
| 23 |
|
| 24 |
|
| 25 |
def construct_YoutubeDL(module, obj, user_opts, system_opts=None): |
| 26 |
opts = argv = None |
| 27 |
config = obj.config |
| 28 |
|
| 29 |
if not config("deprecations"): |
| 30 |
module.YoutubeDL.deprecated_feature = util.false |
| 31 |
module.YoutubeDL.deprecation_warning = util.false |
| 32 |
|
| 33 |
if cfg := config("config-file"): |
| 34 |
with open(util.expand_path(cfg), encoding="utf-8") as fp: |
| 35 |
contents = fp.read() |
| 36 |
argv = shlex.split(contents, comments=True) |
| 37 |
|
| 38 |
if cmd := config("cmdline-args"): |
| 39 |
if isinstance(cmd, str): |
| 40 |
cmd = shlex.split(cmd) |
| 41 |
argv = (argv + cmd) if argv else cmd |
| 42 |
|
| 43 |
try: |
| 44 |
opts = parse_command_line(module, argv) if argv else user_opts |
| 45 |
except SystemExit: |
| 46 |
raise exception.AbortExtraction("Invalid command-line option") |
| 47 |
|
| 48 |
if opts.get("format") is None: |
| 49 |
opts["format"] = config("format") |
| 50 |
if opts.get("nopart") is None: |
| 51 |
opts["nopart"] = not config("part", True) |
| 52 |
if opts.get("updatetime") is None: |
| 53 |
opts["updatetime"] = config("mtime", True) |
| 54 |
if opts.get("min_filesize") is None: |
| 55 |
opts["min_filesize"] = text.parse_bytes(config("filesize-min"), None) |
| 56 |
if opts.get("max_filesize") is None: |
| 57 |
opts["max_filesize"] = text.parse_bytes(config("filesize-max"), None) |
| 58 |
if opts.get("overwrites") is None and not config("skip", True): |
| 59 |
opts["overwrites"] = True |
| 60 |
if opts.get("ratelimit") is None: |
| 61 |
if rate := config("rate"): |
| 62 |
func = util.build_selection_func(rate, 0, text.parse_bytes) |
| 63 |
if hasattr(func, "args"): |
| 64 |
opts["__gdl_ratelimit_func"] = func |
| 65 |
else: |
| 66 |
opts["ratelimit"] = func() or None |
| 67 |
else: |
| 68 |
opts["ratelimit"] = None |
| 69 |
|
| 70 |
if raw_opts := config("raw-options"): |
| 71 |
opts.update(raw_opts) |
| 72 |
if config("logging", True): |
| 73 |
opts["logger"] = obj.log |
| 74 |
if system_opts: |
| 75 |
opts.update(system_opts) |
| 76 |
|
| 77 |
opts["__gdl_initialize"] = True |
| 78 |
return module.YoutubeDL(opts) |
| 79 |
|
| 80 |
|
| 81 |
def parse_command_line(module, argv): |
| 82 |
parser, opts, args = module.parseOpts(argv) |
| 83 |
|
| 84 |
ytdlp = hasattr(module, "cookies") |
| 85 |
try: |
| 86 |
std_headers = module.utils.networking.std_headers |
| 87 |
except AttributeError: |
| 88 |
std_headers = module.std_headers |
| 89 |
|
| 90 |
try: |
| 91 |
parse_bytes = module.parse_bytes |
| 92 |
except AttributeError: |
| 93 |
parse_bytes = module.FileDownloader.parse_bytes |
| 94 |
|
| 95 |
|
| 96 |
if opts.user_agent is not None: |
| 97 |
std_headers["User-Agent"] = opts.user_agent |
| 98 |
if opts.referer is not None: |
| 99 |
std_headers["Referer"] = opts.referer |
| 100 |
if opts.headers: |
| 101 |
if isinstance(opts.headers, dict): |
| 102 |
std_headers.update(opts.headers) |
| 103 |
else: |
| 104 |
for h in opts.headers: |
| 105 |
key, _, value = h.partition(":") |
| 106 |
std_headers[key] = value |
| 107 |
|
| 108 |
if getattr(opts, "impersonate", None) is not None: |
| 109 |
opts.impersonate = module.ImpersonateTarget.from_str( |
| 110 |
opts.impersonate.lower()) |
| 111 |
if opts.ratelimit is not None: |
| 112 |
opts.ratelimit = parse_bytes(opts.ratelimit) |
| 113 |
if getattr(opts, "throttledratelimit", None) is not None: |
| 114 |
opts.throttledratelimit = parse_bytes(opts.throttledratelimit) |
| 115 |
if opts.min_filesize is not None: |
| 116 |
opts.min_filesize = parse_bytes(opts.min_filesize) |
| 117 |
if opts.max_filesize is not None: |
| 118 |
opts.max_filesize = parse_bytes(opts.max_filesize) |
| 119 |
if opts.max_sleep_interval is None: |
| 120 |
opts.max_sleep_interval = opts.sleep_interval |
| 121 |
if getattr(opts, "overwrites", None): |
| 122 |
opts.continue_dl = False |
| 123 |
if opts.retries is not None: |
| 124 |
opts.retries = parse_retries(opts.retries) |
| 125 |
if getattr(opts, "file_access_retries", None) is not None: |
| 126 |
opts.file_access_retries = parse_retries(opts.file_access_retries) |
| 127 |
if opts.fragment_retries is not None: |
| 128 |
opts.fragment_retries = parse_retries(opts.fragment_retries) |
| 129 |
if getattr(opts, "extractor_retries", None) is not None: |
| 130 |
opts.extractor_retries = parse_retries(opts.extractor_retries) |
| 131 |
if opts.buffersize is not None: |
| 132 |
opts.buffersize = parse_bytes(opts.buffersize) |
| 133 |
if opts.http_chunk_size is not None: |
| 134 |
opts.http_chunk_size = parse_bytes(opts.http_chunk_size) |
| 135 |
if opts.extractaudio: |
| 136 |
opts.audioformat = opts.audioformat.lower() |
| 137 |
if opts.audioquality: |
| 138 |
opts.audioquality = opts.audioquality.strip("kK") |
| 139 |
if opts.recodevideo is not None: |
| 140 |
opts.recodevideo = opts.recodevideo.replace(" ", "") |
| 141 |
if getattr(opts, "remuxvideo", None) is not None: |
| 142 |
opts.remuxvideo = opts.remuxvideo.replace(" ", "") |
| 143 |
if getattr(opts, "wait_for_video", None) is not None: |
| 144 |
min_wait, _, max_wait = opts.wait_for_video.partition("-") |
| 145 |
opts.wait_for_video = (module.parse_duration(min_wait), |
| 146 |
module.parse_duration(max_wait)) |
| 147 |
|
| 148 |
if opts.date is not None: |
| 149 |
date = module.DateRange.day(opts.date) |
| 150 |
else: |
| 151 |
date = module.DateRange(opts.dateafter, opts.datebefore) |
| 152 |
|
| 153 |
decodeOption = getattr(module, "decodeOption", util.identity) |
| 154 |
compat_opts = getattr(opts, "compat_opts", ()) |
| 155 |
|
| 156 |
def _unused_compat_opt(name): |
| 157 |
if name not in compat_opts: |
| 158 |
return False |
| 159 |
compat_opts.discard(name) |
| 160 |
compat_opts.update([f"*{name}"]) |
| 161 |
return True |
| 162 |
|
| 163 |
def set_default_compat( |
| 164 |
compat_name, opt_name, default=True, remove_compat=True): |
| 165 |
attr = getattr(opts, opt_name, None) |
| 166 |
if compat_name in compat_opts: |
| 167 |
if attr is None: |
| 168 |
setattr(opts, opt_name, not default) |
| 169 |
return True |
| 170 |
else: |
| 171 |
if remove_compat: |
| 172 |
_unused_compat_opt(compat_name) |
| 173 |
return False |
| 174 |
elif attr is None: |
| 175 |
setattr(opts, opt_name, default) |
| 176 |
return None |
| 177 |
|
| 178 |
set_default_compat("abort-on-error", "ignoreerrors", "only_download") |
| 179 |
set_default_compat("no-playlist-metafiles", "allow_playlist_files") |
| 180 |
set_default_compat("no-clean-infojson", "clean_infojson") |
| 181 |
if "format-sort" in compat_opts: |
| 182 |
opts.format_sort.extend(module.InfoExtractor.FormatSort.ytdl_default) |
| 183 |
_video_multistreams_set = set_default_compat( |
| 184 |
"multistreams", "allow_multiple_video_streams", |
| 185 |
False, remove_compat=False) |
| 186 |
_audio_multistreams_set = set_default_compat( |
| 187 |
"multistreams", "allow_multiple_audio_streams", |
| 188 |
False, remove_compat=False) |
| 189 |
if _video_multistreams_set is False and _audio_multistreams_set is False: |
| 190 |
_unused_compat_opt("multistreams") |
| 191 |
|
| 192 |
if isinstance(opts.outtmpl, dict): |
| 193 |
outtmpl = opts.outtmpl |
| 194 |
outtmpl_default = outtmpl.get("default") |
| 195 |
else: |
| 196 |
opts.outtmpl = outtmpl = outtmpl_default = "" |
| 197 |
|
| 198 |
if "filename" in compat_opts: |
| 199 |
if outtmpl_default is None: |
| 200 |
outtmpl_default = outtmpl["default"] = "%(title)s-%(id)s.%(ext)s" |
| 201 |
else: |
| 202 |
_unused_compat_opt("filename") |
| 203 |
|
| 204 |
if opts.extractaudio and not opts.keepvideo and opts.format is None: |
| 205 |
opts.format = "bestaudio/best" |
| 206 |
|
| 207 |
if ytdlp: |
| 208 |
def metadataparser_actions(f): |
| 209 |
if isinstance(f, str): |
| 210 |
yield module.MetadataFromFieldPP.to_action(f) |
| 211 |
else: |
| 212 |
REPLACE = module.MetadataParserPP.Actions.REPLACE |
| 213 |
args = f[1:] |
| 214 |
for x in f[0].split(","): |
| 215 |
action = [REPLACE, x] |
| 216 |
action += args |
| 217 |
yield action |
| 218 |
|
| 219 |
parse_metadata = getattr(opts, "parse_metadata", None) |
| 220 |
if isinstance(parse_metadata, dict): |
| 221 |
if opts.metafromtitle is not None: |
| 222 |
if "pre_process" not in parse_metadata: |
| 223 |
parse_metadata["pre_process"] = [] |
| 224 |
parse_metadata["pre_process"].append( |
| 225 |
f"title:{opts.metafromtitle}") |
| 226 |
opts.parse_metadata = { |
| 227 |
k: list(itertools.chain.from_iterable(map( |
| 228 |
metadataparser_actions, v))) |
| 229 |
for k, v in parse_metadata.items() |
| 230 |
} |
| 231 |
else: |
| 232 |
if parse_metadata is None: |
| 233 |
parse_metadata = [] |
| 234 |
if opts.metafromtitle is not None: |
| 235 |
parse_metadata.append(f"title:{opts.metafromtitle}") |
| 236 |
opts.parse_metadata = list(itertools.chain.from_iterable(map( |
| 237 |
metadataparser_actions, parse_metadata))) |
| 238 |
|
| 239 |
opts.metafromtitle = None |
| 240 |
else: |
| 241 |
opts.parse_metadata = () |
| 242 |
|
| 243 |
download_archive_fn = module.expand_path(opts.download_archive) \ |
| 244 |
if opts.download_archive is not None else opts.download_archive |
| 245 |
|
| 246 |
if getattr(opts, "getcomments", None): |
| 247 |
opts.writeinfojson = True |
| 248 |
|
| 249 |
if getattr(opts, "no_sponsorblock", None): |
| 250 |
opts.sponsorblock_mark = set() |
| 251 |
opts.sponsorblock_remove = set() |
| 252 |
else: |
| 253 |
opts.sponsorblock_mark = \ |
| 254 |
getattr(opts, "sponsorblock_mark", None) or set() |
| 255 |
opts.sponsorblock_remove = \ |
| 256 |
getattr(opts, "sponsorblock_remove", None) or set() |
| 257 |
opts.remove_chapters = getattr(opts, "remove_chapters", None) or () |
| 258 |
|
| 259 |
try: |
| 260 |
postprocessors = list(module.get_postprocessors(opts)) |
| 261 |
except AttributeError: |
| 262 |
postprocessors = legacy_postprocessors( |
| 263 |
opts, module, ytdlp, compat_opts) |
| 264 |
|
| 265 |
match_filter = ( |
| 266 |
None if opts.match_filter is None |
| 267 |
else module.match_filter_func(opts.match_filter)) |
| 268 |
|
| 269 |
if cookiesfrombrowser := getattr(opts, "cookiesfrombrowser", None): |
| 270 |
pattern = text.re(r"""(?x) |
| 271 |
(?P<name>[^+:]+) |
| 272 |
(?:\s*\+\s*(?P<keyring>[^:]+))? |
| 273 |
(?:\s*:\s*(?!:)(?P<profile>.+?))? |
| 274 |
(?:\s*::\s*(?P<container>.+))?""") |
| 275 |
if match := pattern.fullmatch(cookiesfrombrowser): |
| 276 |
browser, keyring, profile, container = match.groups() |
| 277 |
if keyring is not None: |
| 278 |
keyring = keyring.upper() |
| 279 |
cookiesfrombrowser = (browser.lower(), profile, keyring, container) |
| 280 |
else: |
| 281 |
cookiesfrombrowser = None |
| 282 |
|
| 283 |
return { |
| 284 |
"usenetrc": opts.usenetrc, |
| 285 |
"netrc_location": getattr(opts, "netrc_location", None), |
| 286 |
"username": opts.username, |
| 287 |
"password": opts.password, |
| 288 |
"twofactor": opts.twofactor, |
| 289 |
"videopassword": opts.videopassword, |
| 290 |
"ap_mso": opts.ap_mso, |
| 291 |
"ap_username": opts.ap_username, |
| 292 |
"ap_password": opts.ap_password, |
| 293 |
"quiet": opts.quiet, |
| 294 |
"no_warnings": opts.no_warnings, |
| 295 |
"forceurl": opts.geturl, |
| 296 |
"forcetitle": opts.gettitle, |
| 297 |
"forceid": opts.getid, |
| 298 |
"forcethumbnail": opts.getthumbnail, |
| 299 |
"forcedescription": opts.getdescription, |
| 300 |
"forceduration": opts.getduration, |
| 301 |
"forcefilename": opts.getfilename, |
| 302 |
"forceformat": opts.getformat, |
| 303 |
"forceprint": getattr(opts, "forceprint", None) or (), |
| 304 |
"force_write_download_archive": getattr( |
| 305 |
opts, "force_write_download_archive", None), |
| 306 |
"simulate": opts.simulate, |
| 307 |
"skip_download": opts.skip_download, |
| 308 |
"format": opts.format, |
| 309 |
"allow_unplayable_formats": getattr( |
| 310 |
opts, "allow_unplayable_formats", None), |
| 311 |
"ignore_no_formats_error": getattr( |
| 312 |
opts, "ignore_no_formats_error", None), |
| 313 |
"format_sort": getattr( |
| 314 |
opts, "format_sort", None), |
| 315 |
"format_sort_force": getattr( |
| 316 |
opts, "format_sort_force", None), |
| 317 |
"allow_multiple_video_streams": opts.allow_multiple_video_streams, |
| 318 |
"allow_multiple_audio_streams": opts.allow_multiple_audio_streams, |
| 319 |
"check_formats": getattr( |
| 320 |
opts, "check_formats", None), |
| 321 |
"outtmpl": opts.outtmpl, |
| 322 |
"outtmpl_na_placeholder": opts.outtmpl_na_placeholder, |
| 323 |
"paths": getattr(opts, "paths", None), |
| 324 |
"autonumber_size": opts.autonumber_size, |
| 325 |
"autonumber_start": opts.autonumber_start, |
| 326 |
"restrictfilenames": opts.restrictfilenames, |
| 327 |
"windowsfilenames": getattr(opts, "windowsfilenames", None), |
| 328 |
"ignoreerrors": opts.ignoreerrors, |
| 329 |
"force_generic_extractor": opts.force_generic_extractor, |
| 330 |
"ratelimit": opts.ratelimit, |
| 331 |
"throttledratelimit": getattr(opts, "throttledratelimit", None), |
| 332 |
"overwrites": getattr(opts, "overwrites", None), |
| 333 |
"retries": opts.retries, |
| 334 |
"file_access_retries": getattr(opts, "file_access_retries", None), |
| 335 |
"fragment_retries": opts.fragment_retries, |
| 336 |
"extractor_retries": getattr(opts, "extractor_retries", None), |
| 337 |
"skip_unavailable_fragments": opts.skip_unavailable_fragments, |
| 338 |
"keep_fragments": opts.keep_fragments, |
| 339 |
"concurrent_fragment_downloads": getattr( |
| 340 |
opts, "concurrent_fragment_downloads", None), |
| 341 |
"buffersize": opts.buffersize, |
| 342 |
"noresizebuffer": opts.noresizebuffer, |
| 343 |
"http_chunk_size": opts.http_chunk_size, |
| 344 |
"continuedl": opts.continue_dl, |
| 345 |
"noprogress": True if opts.noprogress is None else opts.noprogress, |
| 346 |
"playliststart": opts.playliststart, |
| 347 |
"playlistend": opts.playlistend, |
| 348 |
"playlistreverse": opts.playlist_reverse, |
| 349 |
"playlistrandom": opts.playlist_random, |
| 350 |
"noplaylist": opts.noplaylist, |
| 351 |
"logtostderr": outtmpl_default == "-", |
| 352 |
"consoletitle": opts.consoletitle, |
| 353 |
"nopart": opts.nopart, |
| 354 |
"updatetime": opts.updatetime, |
| 355 |
"writedescription": opts.writedescription, |
| 356 |
"writeannotations": getattr(opts, "writeannotations", None), |
| 357 |
"writeinfojson": opts.writeinfojson, |
| 358 |
"allow_playlist_files": opts.allow_playlist_files, |
| 359 |
"clean_infojson": opts.clean_infojson, |
| 360 |
"getcomments": getattr(opts, "getcomments", None), |
| 361 |
"writethumbnail": opts.writethumbnail is True, |
| 362 |
"write_all_thumbnails": getattr(opts, "write_all_thumbnails", None) or |
| 363 |
opts.writethumbnail == "all", |
| 364 |
"writelink": getattr(opts, "writelink", None), |
| 365 |
"writeurllink": getattr(opts, "writeurllink", None), |
| 366 |
"writewebloclink": getattr(opts, "writewebloclink", None), |
| 367 |
"writedesktoplink": getattr(opts, "writedesktoplink", None), |
| 368 |
"writesubtitles": opts.writesubtitles, |
| 369 |
"writeautomaticsub": opts.writeautomaticsub, |
| 370 |
"allsubtitles": opts.allsubtitles, |
| 371 |
"subtitlesformat": opts.subtitlesformat, |
| 372 |
"subtitleslangs": opts.subtitleslangs, |
| 373 |
"matchtitle": decodeOption(opts.matchtitle), |
| 374 |
"rejecttitle": decodeOption(opts.rejecttitle), |
| 375 |
"max_downloads": opts.max_downloads, |
| 376 |
"prefer_free_formats": opts.prefer_free_formats, |
| 377 |
"trim_file_name": getattr(opts, "trim_file_name", None), |
| 378 |
"verbose": opts.verbose, |
| 379 |
"dump_intermediate_pages": opts.dump_intermediate_pages, |
| 380 |
"write_pages": opts.write_pages, |
| 381 |
"test": opts.test, |
| 382 |
"keepvideo": opts.keepvideo, |
| 383 |
"min_filesize": opts.min_filesize, |
| 384 |
"max_filesize": opts.max_filesize, |
| 385 |
"min_views": opts.min_views, |
| 386 |
"max_views": opts.max_views, |
| 387 |
"daterange": date, |
| 388 |
"cachedir": opts.cachedir, |
| 389 |
"youtube_print_sig_code": getattr( |
| 390 |
opts, "youtube_print_sig_code", None), |
| 391 |
"age_limit": opts.age_limit, |
| 392 |
"download_archive": download_archive_fn, |
| 393 |
"break_on_existing": getattr(opts, "break_on_existing", None), |
| 394 |
"break_on_reject": getattr(opts, "break_on_reject", None), |
| 395 |
"break_per_url": getattr(opts, "break_per_url", None), |
| 396 |
"skip_playlist_after_errors": getattr( |
| 397 |
opts, "skip_playlist_after_errors", None), |
| 398 |
"cookiefile": opts.cookiefile, |
| 399 |
"cookiesfrombrowser": cookiesfrombrowser, |
| 400 |
"nocheckcertificate": opts.no_check_certificate, |
| 401 |
"prefer_insecure": opts.prefer_insecure, |
| 402 |
"proxy": opts.proxy, |
| 403 |
"socket_timeout": opts.socket_timeout, |
| 404 |
"bidi_workaround": opts.bidi_workaround, |
| 405 |
"debug_printtraffic": opts.debug_printtraffic, |
| 406 |
"prefer_ffmpeg": getattr(opts, "prefer_ffmpeg", None), |
| 407 |
"include_ads": getattr(opts, "include_ads", None), |
| 408 |
"default_search": opts.default_search, |
| 409 |
"dynamic_mpd": getattr(opts, "dynamic_mpd", None), |
| 410 |
"extractor_args": getattr(opts, "extractor_args", None), |
| 411 |
"youtube_include_dash_manifest": getattr( |
| 412 |
opts, "youtube_include_dash_manifest", None), |
| 413 |
"youtube_include_hls_manifest": getattr( |
| 414 |
opts, "youtube_include_hls_manifest", None), |
| 415 |
"encoding": opts.encoding, |
| 416 |
"extract_flat": opts.extract_flat, |
| 417 |
"live_from_start": getattr(opts, "live_from_start", None), |
| 418 |
"wait_for_video": getattr(opts, "wait_for_video", None), |
| 419 |
"mark_watched": opts.mark_watched, |
| 420 |
"merge_output_format": opts.merge_output_format, |
| 421 |
"postprocessors": postprocessors, |
| 422 |
"fixup": opts.fixup, |
| 423 |
"source_address": opts.source_address, |
| 424 |
"sleep_interval_requests": getattr( |
| 425 |
opts, "sleep_interval_requests", None), |
| 426 |
"sleep_interval": opts.sleep_interval, |
| 427 |
"max_sleep_interval": opts.max_sleep_interval, |
| 428 |
"sleep_interval_subtitles": getattr( |
| 429 |
opts, "sleep_interval_subtitles", None), |
| 430 |
"external_downloader": opts.external_downloader, |
| 431 |
"playlist_items": opts.playlist_items, |
| 432 |
"xattr_set_filesize": getattr(opts, "xattr_set_filesize", None), |
| 433 |
"match_filter": match_filter, |
| 434 |
"no_color": getattr(opts, "no_color", None), |
| 435 |
"ffmpeg_location": opts.ffmpeg_location, |
| 436 |
"hls_prefer_native": opts.hls_prefer_native, |
| 437 |
"hls_use_mpegts": opts.hls_use_mpegts, |
| 438 |
"hls_split_discontinuity": getattr( |
| 439 |
opts, "hls_split_discontinuity", None), |
| 440 |
"external_downloader_args": opts.external_downloader_args, |
| 441 |
"postprocessor_args": opts.postprocessor_args, |
| 442 |
"cn_verification_proxy": getattr(opts, "cn_verification_proxy", None), |
| 443 |
"geo_verification_proxy": opts.geo_verification_proxy, |
| 444 |
"geo_bypass": getattr( |
| 445 |
opts, "geo_bypass", "default"), |
| 446 |
"geo_bypass_country": getattr( |
| 447 |
opts, "geo_bypass_country", None), |
| 448 |
"geo_bypass_ip_block": getattr( |
| 449 |
opts, "geo_bypass_ip_block", None), |
| 450 |
"compat_opts": compat_opts, |
| 451 |
"impersonate": getattr(opts, "impersonate", None), |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
def parse_retries(retries, name=""): |
| 456 |
if retries in ("inf", "infinite"): |
| 457 |
return float("inf") |
| 458 |
return int(retries) |
| 459 |
|
| 460 |
|
| 461 |
def legacy_postprocessors(opts, module, ytdlp, compat_opts): |
| 462 |
postprocessors = [] |
| 463 |
|
| 464 |
sponsorblock_query = opts.sponsorblock_mark | opts.sponsorblock_remove |
| 465 |
if opts.metafromtitle: |
| 466 |
postprocessors.append({ |
| 467 |
"key": "MetadataFromTitle", |
| 468 |
"titleformat": opts.metafromtitle, |
| 469 |
}) |
| 470 |
if getattr(opts, "add_postprocessors", None): |
| 471 |
postprocessors += list(opts.add_postprocessors) |
| 472 |
if sponsorblock_query: |
| 473 |
postprocessors.append({ |
| 474 |
"key": "SponsorBlock", |
| 475 |
"categories": sponsorblock_query, |
| 476 |
"api": opts.sponsorblock_api, |
| 477 |
"when": "pre_process", |
| 478 |
}) |
| 479 |
if opts.parse_metadata: |
| 480 |
postprocessors.append({ |
| 481 |
"key": "MetadataParser", |
| 482 |
"actions": opts.parse_metadata, |
| 483 |
"when": "pre_process", |
| 484 |
}) |
| 485 |
if opts.convertsubtitles: |
| 486 |
pp = {"key": "FFmpegSubtitlesConvertor", |
| 487 |
"format": opts.convertsubtitles} |
| 488 |
if ytdlp: |
| 489 |
pp["when"] = "before_dl" |
| 490 |
postprocessors.append(pp) |
| 491 |
if getattr(opts, "convertthumbnails", None): |
| 492 |
postprocessors.append({ |
| 493 |
"key": "FFmpegThumbnailsConvertor", |
| 494 |
"format": opts.convertthumbnails, |
| 495 |
"when": "before_dl", |
| 496 |
}) |
| 497 |
if getattr(opts, "exec_before_dl_cmd", None): |
| 498 |
postprocessors.append({ |
| 499 |
"key": "Exec", |
| 500 |
"exec_cmd": opts.exec_before_dl_cmd, |
| 501 |
"when": "before_dl", |
| 502 |
}) |
| 503 |
if opts.extractaudio: |
| 504 |
postprocessors.append({ |
| 505 |
"key": "FFmpegExtractAudio", |
| 506 |
"preferredcodec": opts.audioformat, |
| 507 |
"preferredquality": opts.audioquality, |
| 508 |
"nopostoverwrites": opts.nopostoverwrites, |
| 509 |
}) |
| 510 |
if getattr(opts, "remuxvideo", None): |
| 511 |
postprocessors.append({ |
| 512 |
"key": "FFmpegVideoRemuxer", |
| 513 |
"preferedformat": opts.remuxvideo, |
| 514 |
}) |
| 515 |
if opts.recodevideo: |
| 516 |
postprocessors.append({ |
| 517 |
"key": "FFmpegVideoConvertor", |
| 518 |
"preferedformat": opts.recodevideo, |
| 519 |
}) |
| 520 |
if opts.embedsubtitles: |
| 521 |
pp = {"key": "FFmpegEmbedSubtitle"} |
| 522 |
if ytdlp: |
| 523 |
pp["already_have_subtitle"] = ( |
| 524 |
opts.writesubtitles and "no-keep-subs" not in compat_opts) |
| 525 |
postprocessors.append(pp) |
| 526 |
if not opts.writeautomaticsub and "no-keep-subs" not in compat_opts: |
| 527 |
opts.writesubtitles = True |
| 528 |
if opts.allsubtitles and not opts.writeautomaticsub: |
| 529 |
opts.writesubtitles = True |
| 530 |
remove_chapters_patterns, remove_ranges = [], [] |
| 531 |
for regex in opts.remove_chapters: |
| 532 |
if regex.startswith("*"): |
| 533 |
dur = list(map(module.parse_duration, regex[1:].split("-"))) |
| 534 |
if len(dur) == 2 and all(t is not None for t in dur): |
| 535 |
remove_ranges.append(tuple(dur)) |
| 536 |
continue |
| 537 |
remove_chapters_patterns.append(text.re(regex)) |
| 538 |
if opts.remove_chapters or sponsorblock_query: |
| 539 |
postprocessors.append({ |
| 540 |
"key": "ModifyChapters", |
| 541 |
"remove_chapters_patterns": remove_chapters_patterns, |
| 542 |
"remove_sponsor_segments": opts.sponsorblock_remove, |
| 543 |
"remove_ranges": remove_ranges, |
| 544 |
"sponsorblock_chapter_title": opts.sponsorblock_chapter_title, |
| 545 |
"force_keyframes": opts.force_keyframes_at_cuts, |
| 546 |
}) |
| 547 |
addchapters = getattr(opts, "addchapters", None) |
| 548 |
embed_infojson = getattr(opts, "embed_infojson", None) |
| 549 |
if opts.addmetadata or addchapters or embed_infojson: |
| 550 |
pp = {"key": "FFmpegMetadata"} |
| 551 |
if ytdlp: |
| 552 |
if embed_infojson is None: |
| 553 |
embed_infojson = "if_exists" |
| 554 |
pp["add_metadata"] = opts.addmetadata |
| 555 |
pp["add_chapters"] = addchapters |
| 556 |
pp["add_infojson"] = embed_infojson |
| 557 |
|
| 558 |
postprocessors.append(pp) |
| 559 |
if getattr(opts, "sponskrub", False) is not False: |
| 560 |
postprocessors.append({ |
| 561 |
"key": "SponSkrub", |
| 562 |
"path": opts.sponskrub_path, |
| 563 |
"args": opts.sponskrub_args, |
| 564 |
"cut": opts.sponskrub_cut, |
| 565 |
"force": opts.sponskrub_force, |
| 566 |
"ignoreerror": opts.sponskrub is None, |
| 567 |
"_from_cli": True, |
| 568 |
}) |
| 569 |
if opts.embedthumbnail: |
| 570 |
already_have_thumbnail = (opts.writethumbnail or |
| 571 |
getattr(opts, "write_all_thumbnails", False)) |
| 572 |
postprocessors.append({ |
| 573 |
"key": "EmbedThumbnail", |
| 574 |
"already_have_thumbnail": already_have_thumbnail, |
| 575 |
}) |
| 576 |
if not already_have_thumbnail: |
| 577 |
opts.writethumbnail = True |
| 578 |
if isinstance(opts.outtmpl, dict): |
| 579 |
opts.outtmpl["pl_thumbnail"] = "" |
| 580 |
if getattr(opts, "split_chapters", None): |
| 581 |
postprocessors.append({ |
| 582 |
"key": "FFmpegSplitChapters", |
| 583 |
"force_keyframes": opts.force_keyframes_at_cuts, |
| 584 |
}) |
| 585 |
if opts.xattrs: |
| 586 |
postprocessors.append({"key": "XAttrMetadata"}) |
| 587 |
if opts.exec_cmd: |
| 588 |
postprocessors.append({ |
| 589 |
"key": "Exec", |
| 590 |
"exec_cmd": opts.exec_cmd, |
| 591 |
"when": "after_move", |
| 592 |
}) |
| 593 |
|
| 594 |
return postprocessors |