]> jfr.im git - yt-dlp.git/blame - youtube_dl/YoutubeDL.py
Fix W504 and disable W503 (closes #20863)
[yt-dlp.git] / youtube_dl / YoutubeDL.py
CommitLineData
8222d8de 1#!/usr/bin/env python
dcdb292f 2# coding: utf-8
8222d8de 3
6febd1c1 4from __future__ import absolute_import, unicode_literals
8222d8de 5
26e63931 6import collections
31bd3925 7import contextlib
317f7ab6 8import copy
9d2ecdbc 9import datetime
c1c9a79c 10import errno
31bd3925 11import fileinput
8222d8de 12import io
b82f815f 13import itertools
8694c600 14import json
62fec3b2 15import locale
083c9df9 16import operator
8222d8de 17import os
dca08720 18import platform
8222d8de
JMF
19import re
20import shutil
dca08720 21import subprocess
8222d8de
JMF
22import socket
23import sys
24import time
67134eab 25import tokenize
8222d8de 26import traceback
75822ca7 27import random
8222d8de 28
961ea474
S
29from string import ascii_letters
30
8c25f81b 31from .compat import (
82d8a8b6 32 compat_basestring,
dca08720 33 compat_cookiejar,
003c69a8 34 compat_get_terminal_size,
ce02ed60 35 compat_http_client,
4f026faf 36 compat_kwargs,
d0d9ade4 37 compat_numeric_types,
e9c0cdd3 38 compat_os_name,
ce02ed60 39 compat_str,
67134eab 40 compat_tokenize_tokenize,
ce02ed60
PH
41 compat_urllib_error,
42 compat_urllib_request,
8b172c2e 43 compat_urllib_request_DataHandler,
8c25f81b
PH
44)
45from .utils import (
eedb7ba5
S
46 age_restricted,
47 args_to_str,
ce02ed60
PH
48 ContentTooShortError,
49 date_from_str,
50 DateRange,
acd69589 51 DEFAULT_OUTTMPL,
ce02ed60 52 determine_ext,
b5559424 53 determine_protocol,
ce02ed60 54 DownloadError,
c0384f22 55 encode_compat_str,
ce02ed60 56 encodeFilename,
9b9c5355 57 error_to_compat_str,
590bc6f6 58 expand_path,
ce02ed60 59 ExtractorError,
02dbf93f 60 format_bytes,
525ef922 61 formatSeconds,
773f291d 62 GeoRestrictedError,
c9969434 63 int_or_none,
773f291d 64 ISO3166Utils,
ce02ed60 65 locked_file,
dca08720 66 make_HTTPS_handler,
ce02ed60 67 MaxDownloadsReached,
cd6fc19e 68 orderedSet,
b7ab0590 69 PagedList,
083c9df9 70 parse_filesize,
91410c9b 71 PerRequestProxyHandler,
dca08720 72 platform_name,
eedb7ba5 73 PostProcessingError,
ce02ed60 74 preferredencoding,
eedb7ba5 75 prepend_extension,
51fb4995 76 register_socks_protocols,
cfb56d1a 77 render_table,
eedb7ba5 78 replace_extension,
ce02ed60
PH
79 SameFileError,
80 sanitize_filename,
1bb5c511 81 sanitize_path,
dcf77cf1 82 sanitize_url,
67dda517 83 sanitized_Request,
e5660ee6 84 std_headers,
1211bb6d 85 str_or_none,
ce02ed60 86 subtitles_filename,
ce02ed60 87 UnavailableVideoError,
29eb5174 88 url_basename,
58b1f00d 89 version_tuple,
ce02ed60
PH
90 write_json_file,
91 write_string,
1bab3437 92 YoutubeDLCookieJar,
6a3f4c3f 93 YoutubeDLCookieProcessor,
dca08720 94 YoutubeDLHandler,
ce02ed60 95)
a0e07d31 96from .cache import Cache
e0986e31 97from .extractor import get_info_extractor, gen_extractor_classes, _LAZY_LOADER
4c54b89e 98from .extractor.openload import PhantomJSwrapper
3bc2ddcc 99from .downloader import get_suitable_downloader
4c83c967 100from .downloader.rtmp import rtmpdump_version
4f026faf 101from .postprocessor import (
f17f8651 102 FFmpegFixupM3u8PP,
62cd676c 103 FFmpegFixupM4aPP,
6271f1ca 104 FFmpegFixupStretchedPP,
4f026faf
PH
105 FFmpegMergerPP,
106 FFmpegPostProcessor,
107 get_postprocessor,
108)
dca08720 109from .version import __version__
8222d8de 110
e9c0cdd3
YCH
111if compat_os_name == 'nt':
112 import ctypes
113
8222d8de
JMF
114
115class YoutubeDL(object):
116 """YoutubeDL class.
117
118 YoutubeDL objects are the ones responsible of downloading the
119 actual video file and writing it to disk if the user has requested
120 it, among some other tasks. In most cases there should be one per
121 program. As, given a video URL, the downloader doesn't know how to
122 extract all the needed information, task that InfoExtractors do, it
123 has to pass the URL to one of them.
124
125 For this, YoutubeDL objects have a method that allows
126 InfoExtractors to be registered in a given order. When it is passed
127 a URL, the YoutubeDL object handles it to the first InfoExtractor it
128 finds that reports being able to handle it. The InfoExtractor extracts
129 all the information about the video or videos the URL refers to, and
130 YoutubeDL process the extracted information, possibly using a File
131 Downloader to download the video.
132
133 YoutubeDL objects accept a lot of parameters. In order not to saturate
134 the object constructor with arguments, it receives a dictionary of
135 options instead. These options are available through the params
136 attribute for the InfoExtractors to use. The YoutubeDL also
137 registers itself as the downloader in charge for the InfoExtractors
138 that are added to it, so this is a "mutual registration".
139
140 Available options:
141
142 username: Username for authentication purposes.
143 password: Password for authentication purposes.
180940e0 144 videopassword: Password for accessing a video.
1da50aa3
S
145 ap_mso: Adobe Pass multiple-system operator identifier.
146 ap_username: Multiple-system operator account username.
147 ap_password: Multiple-system operator account password.
8222d8de
JMF
148 usenetrc: Use netrc for authentication instead.
149 verbose: Print additional info to stdout.
150 quiet: Do not print messages to stdout.
ad8915b7 151 no_warnings: Do not print out anything for warnings.
8222d8de
JMF
152 forceurl: Force printing final URL.
153 forcetitle: Force printing title.
154 forceid: Force printing ID.
155 forcethumbnail: Force printing thumbnail URL.
156 forcedescription: Force printing description.
157 forcefilename: Force printing final filename.
525ef922 158 forceduration: Force printing duration.
8694c600 159 forcejson: Force printing info_dict as JSON.
63e0be34
PH
160 dump_single_json: Force printing the info_dict of the whole playlist
161 (or video) as a single JSON line.
8222d8de 162 simulate: Do not download the video files.
d8600787 163 format: Video format code. See options.py for more information.
8222d8de
JMF
164 outtmpl: Template for output names.
165 restrictfilenames: Do not allow "&" and spaces in file names
166 ignoreerrors: Do not stop on download errors.
d22dec74 167 force_generic_extractor: Force downloader to use the generic extractor
8222d8de
JMF
168 nooverwrites: Prevent overwriting files.
169 playliststart: Playlist item to start at.
170 playlistend: Playlist item to end at.
c14e88f0 171 playlist_items: Specific indices of playlist to download.
ff815fe6 172 playlistreverse: Download playlist items in reverse order.
75822ca7 173 playlistrandom: Download playlist items in random order.
8222d8de
JMF
174 matchtitle: Download only matching titles.
175 rejecttitle: Reject downloads for matching titles.
8bf9319e 176 logger: Log messages to a logging.Logger instance.
8222d8de
JMF
177 logtostderr: Log messages to stderr instead of stdout.
178 writedescription: Write the video description to a .description file
179 writeinfojson: Write the video description to a .info.json file
1fb07d10 180 writeannotations: Write the video annotations to a .annotations.xml file
8222d8de 181 writethumbnail: Write the thumbnail image to a file
ec82d85a 182 write_all_thumbnails: Write all thumbnail formats to files
8222d8de 183 writesubtitles: Write the video subtitles to a file
741dd8ea 184 writeautomaticsub: Write the automatically generated subtitles to a file
8222d8de 185 allsubtitles: Downloads all the subtitles of the video
0b7f3118 186 (requires writesubtitles or writeautomaticsub)
8222d8de 187 listsubtitles: Lists all available subtitles for the video
a504ced0 188 subtitlesformat: The format code for subtitles
aa6a10c4 189 subtitleslangs: List of languages of the subtitles to download
8222d8de
JMF
190 keepvideo: Keep the video file after post-processing
191 daterange: A DateRange object, download only if the upload_date is in the range.
192 skip_download: Skip the actual download of the video file
c35f9e72 193 cachedir: Location of the cache files in the filesystem.
a0e07d31 194 False to disable filesystem cache.
47192f92 195 noplaylist: Download single video instead of a playlist if in doubt.
8dbe9899
PH
196 age_limit: An integer representing the user's age in years.
197 Unsuitable videos for the given age are skipped.
5fe18bdb
PH
198 min_views: An integer representing the minimum view count the video
199 must have in order to not be skipped.
200 Videos without view count information are always
201 downloaded. None for no limit.
202 max_views: An integer representing the maximum view count.
203 Videos that are more popular than that are not
204 downloaded.
205 Videos without view count information are always
206 downloaded. None for no limit.
207 download_archive: File name of a file where all downloads are recorded.
c1c9a79c
PH
208 Videos already present in the file are not downloaded
209 again.
dca08720 210 cookiefile: File name where cookies should be read from and dumped to.
a1ee09e8 211 nocheckcertificate:Do not verify SSL certificates
7e8c0af0
PH
212 prefer_insecure: Use HTTP instead of HTTPS to retrieve information.
213 At the moment, this is only supported by YouTube.
a1ee09e8 214 proxy: URL of the proxy server to use
38cce791 215 geo_verification_proxy: URL of the proxy to use for IP address verification
504f20dd 216 on geo-restricted sites.
e344693b 217 socket_timeout: Time to wait for unresponsive hosts, in seconds
0783b09b
PH
218 bidi_workaround: Work around buggy terminals without bidirectional text
219 support, using fridibi
a0ddb8a2 220 debug_printtraffic:Print out sent and received HTTP traffic
7b0817e8 221 include_ads: Download ads as well
04b4d394
PH
222 default_search: Prepend this string if an input url is not valid.
223 'auto' for elaborate guessing
62fec3b2 224 encoding: Use this encoding instead of the system-specified.
e8ee972c 225 extract_flat: Do not resolve URLs, return the immediate result.
057a5206
PH
226 Pass in 'in_playlist' to only show this behavior for
227 playlist items.
4f026faf 228 postprocessors: A list of dictionaries, each with an entry
71b640cc
PH
229 * key: The name of the postprocessor. See
230 youtube_dl/postprocessor/__init__.py for a list.
4f026faf
PH
231 as well as any further keyword arguments for the
232 postprocessor.
71b640cc
PH
233 progress_hooks: A list of functions that get called on download
234 progress, with a dictionary with the entries
5cda4eda 235 * status: One of "downloading", "error", or "finished".
ee69b99a 236 Check this first and ignore unknown values.
71b640cc 237
5cda4eda 238 If status is one of "downloading", or "finished", the
ee69b99a
PH
239 following properties may also be present:
240 * filename: The final filename (always present)
5cda4eda 241 * tmpfilename: The filename we're currently writing to
71b640cc
PH
242 * downloaded_bytes: Bytes on disk
243 * total_bytes: Size of the whole file, None if unknown
5cda4eda
PH
244 * total_bytes_estimate: Guess of the eventual file size,
245 None if unavailable.
246 * elapsed: The number of seconds since download started.
71b640cc
PH
247 * eta: The estimated time in seconds, None if unknown
248 * speed: The download speed in bytes/second, None if
249 unknown
5cda4eda
PH
250 * fragment_index: The counter of the currently
251 downloaded video fragment.
252 * fragment_count: The number of fragments (= individual
253 files that will be merged)
71b640cc
PH
254
255 Progress hooks are guaranteed to be called at least once
256 (with status "finished") if the download is successful.
45598f15 257 merge_output_format: Extension to use when merging formats.
6271f1ca
PH
258 fixup: Automatically correct known faults of the file.
259 One of:
260 - "never": do nothing
261 - "warn": only emit a warning
262 - "detect_or_warn": check whether we can do anything
62cd676c 263 about it, warn otherwise (default)
504f20dd 264 source_address: Client-side IP address to bind to.
6ec6cb4e 265 call_home: Boolean, true iff we are allowed to contact the
8bfa7545 266 youtube-dl servers for debugging.
7aa589a5
S
267 sleep_interval: Number of seconds to sleep before each download when
268 used alone or a lower bound of a range for randomized
269 sleep before each download (minimum possible number
270 of seconds to sleep) when used along with
271 max_sleep_interval.
272 max_sleep_interval:Upper bound of a range for randomized sleep before each
273 download (maximum possible number of seconds to sleep).
274 Must only be used along with sleep_interval.
275 Actual sleep time will be a random float from range
276 [sleep_interval; max_sleep_interval].
cfb56d1a
PH
277 listformats: Print an overview of available video formats and exit.
278 list_thumbnails: Print a table of all thumbnails and exit.
347de493
PH
279 match_filter: A function that gets called with the info_dict of
280 every video.
281 If it returns a message, the video is ignored.
282 If it returns None, the video is downloaded.
283 match_filter_func in utils.py is one example for this.
7e5db8c9 284 no_color: Do not emit color codes in output.
0a840f58 285 geo_bypass: Bypass geographic restriction via faking X-Forwarded-For
504f20dd 286 HTTP header
0a840f58 287 geo_bypass_country:
773f291d
S
288 Two-letter ISO 3166-2 country code that will be used for
289 explicit geographic restriction bypassing via faking
504f20dd 290 X-Forwarded-For HTTP header
5f95927a
S
291 geo_bypass_ip_block:
292 IP range in CIDR notation that will be used similarly to
504f20dd 293 geo_bypass_country
71b640cc 294
85729c51
PH
295 The following options determine which downloader is picked:
296 external_downloader: Executable of the external downloader to call.
297 None or unset for standard (built-in) downloader.
bf09af3a
S
298 hls_prefer_native: Use the native HLS downloader instead of ffmpeg/avconv
299 if True, otherwise use ffmpeg/avconv if False, otherwise
300 use downloader suggested by extractor if None.
fe7e0c98 301
8222d8de 302 The following parameters are not used by YoutubeDL itself, they are used by
c75f0b36 303 the downloader (see youtube_dl/downloader/common.py):
8222d8de 304 nopart, updatetime, buffersize, ratelimit, min_filesize, max_filesize, test,
881e6a1f 305 noresizebuffer, retries, continuedl, noprogress, consoletitle,
b54d4a5c
S
306 xattr_set_filesize, external_downloader_args, hls_use_mpegts,
307 http_chunk_size.
76b1bd67
JMF
308
309 The following options are used by the post processors:
d4a24f40
S
310 prefer_ffmpeg: If False, use avconv instead of ffmpeg if both are available,
311 otherwise prefer ffmpeg.
c0b7d117
S
312 ffmpeg_location: Location of the ffmpeg/avconv binary; either the path
313 to the binary or its containing directory.
f72b0a60
S
314 postprocessor_args: A list of additional command-line arguments for the
315 postprocessor.
3836b02c 316
3600fd59
S
317 The following options are used by the Youtube extractor:
318 youtube_include_dash_manifest: If True (default), DASH manifests and related
319 data will be downloaded and processed by extractor.
320 You can reduce network I/O by disabling it if you don't
321 care about DASH.
8222d8de
JMF
322 """
323
c9969434
S
324 _NUMERIC_FIELDS = set((
325 'width', 'height', 'tbr', 'abr', 'asr', 'vbr', 'fps', 'filesize', 'filesize_approx',
326 'timestamp', 'upload_year', 'upload_month', 'upload_day',
327 'duration', 'view_count', 'like_count', 'dislike_count', 'repost_count',
328 'average_rating', 'comment_count', 'age_limit',
329 'start_time', 'end_time',
330 'chapter_number', 'season_number', 'episode_number',
331 'track_number', 'disc_number', 'release_year',
332 'playlist_index',
333 ))
334
8222d8de
JMF
335 params = None
336 _ies = []
337 _pps = []
338 _download_retcode = None
339 _num_downloads = None
340 _screen_file = None
341
3511266b 342 def __init__(self, params=None, auto_init=True):
8222d8de 343 """Create a FileDownloader object with the given options."""
e9f9a10f
JMF
344 if params is None:
345 params = {}
8222d8de 346 self._ies = []
56c73665 347 self._ies_instances = {}
8222d8de 348 self._pps = []
933605d7 349 self._progress_hooks = []
8222d8de
JMF
350 self._download_retcode = 0
351 self._num_downloads = 0
352 self._screen_file = [sys.stdout, sys.stderr][params.get('logtostderr', False)]
0783b09b 353 self._err_file = sys.stderr
4abf617b
S
354 self.params = {
355 # Default parameters
356 'nocheckcertificate': False,
357 }
358 self.params.update(params)
a0e07d31 359 self.cache = Cache(self)
34308b30 360
be5df5ee
S
361 def check_deprecated(param, option, suggestion):
362 if self.params.get(param) is not None:
363 self.report_warning(
364 '%s is deprecated. Use %s instead.' % (option, suggestion))
365 return True
366 return False
367
368 if check_deprecated('cn_verification_proxy', '--cn-verification-proxy', '--geo-verification-proxy'):
38cce791
YCH
369 if self.params.get('geo_verification_proxy') is None:
370 self.params['geo_verification_proxy'] = self.params['cn_verification_proxy']
371
be5df5ee
S
372 check_deprecated('autonumber_size', '--autonumber-size', 'output template with %(autonumber)0Nd, where N in the number of digits')
373 check_deprecated('autonumber', '--auto-number', '-o "%(autonumber)s-%(title)s.%(ext)s"')
374 check_deprecated('usetitle', '--title', '-o "%(title)s-%(id)s.%(ext)s"')
375
0783b09b 376 if params.get('bidi_workaround', False):
1c088fa8
PH
377 try:
378 import pty
379 master, slave = pty.openpty()
003c69a8 380 width = compat_get_terminal_size().columns
1c088fa8
PH
381 if width is None:
382 width_args = []
383 else:
384 width_args = ['-w', str(width)]
5d681e96 385 sp_kwargs = dict(
1c088fa8
PH
386 stdin=subprocess.PIPE,
387 stdout=slave,
388 stderr=self._err_file)
5d681e96
PH
389 try:
390 self._output_process = subprocess.Popen(
391 ['bidiv'] + width_args, **sp_kwargs
392 )
393 except OSError:
5d681e96
PH
394 self._output_process = subprocess.Popen(
395 ['fribidi', '-c', 'UTF-8'] + width_args, **sp_kwargs)
396 self._output_channel = os.fdopen(master, 'rb')
1c088fa8 397 except OSError as ose:
66e7ace1 398 if ose.errno == errno.ENOENT:
6febd1c1 399 self.report_warning('Could not find fribidi executable, ignoring --bidi-workaround . Make sure that fribidi is an executable file in one of the directories in your $PATH.')
1c088fa8
PH
400 else:
401 raise
0783b09b 402
3089bc74
S
403 if (sys.platform != 'win32'
404 and sys.getfilesystemencoding() in ['ascii', 'ANSI_X3.4-1968']
405 and not params.get('restrictfilenames', False)):
e9137224 406 # Unicode filesystem API will throw errors (#1474, #13027)
34308b30 407 self.report_warning(
6febd1c1 408 'Assuming --restrict-filenames since file system encoding '
1b725173 409 'cannot encode all characters. '
6febd1c1 410 'Set the LC_ALL environment variable to fix this.')
4a98cdbf 411 self.params['restrictfilenames'] = True
34308b30 412
486dd09e
PH
413 if isinstance(params.get('outtmpl'), bytes):
414 self.report_warning(
415 'Parameter outtmpl is bytes, but should be a unicode string. '
416 'Put from __future__ import unicode_literals at the top of your code file or consider switching to Python 3.x.')
417
dca08720
PH
418 self._setup_opener()
419
3511266b
PH
420 if auto_init:
421 self.print_debug_header()
422 self.add_default_info_extractors()
423
4f026faf
PH
424 for pp_def_raw in self.params.get('postprocessors', []):
425 pp_class = get_postprocessor(pp_def_raw['key'])
426 pp_def = dict(pp_def_raw)
427 del pp_def['key']
428 pp = pp_class(self, **compat_kwargs(pp_def))
429 self.add_post_processor(pp)
430
71b640cc
PH
431 for ph in self.params.get('progress_hooks', []):
432 self.add_progress_hook(ph)
433
51fb4995
YCH
434 register_socks_protocols()
435
7d4111ed
PH
436 def warn_if_short_id(self, argv):
437 # short YouTube ID starting with dash?
438 idxs = [
439 i for i, a in enumerate(argv)
440 if re.match(r'^-[0-9A-Za-z_-]{10}$', a)]
441 if idxs:
442 correct_argv = (
3089bc74
S
443 ['youtube-dl']
444 + [a for i, a in enumerate(argv) if i not in idxs]
445 + ['--'] + [argv[i] for i in idxs]
7d4111ed
PH
446 )
447 self.report_warning(
448 'Long argument string detected. '
449 'Use -- to separate parameters and URLs, like this:\n%s\n' %
450 args_to_str(correct_argv))
451
8222d8de
JMF
452 def add_info_extractor(self, ie):
453 """Add an InfoExtractor object to the end of the list."""
454 self._ies.append(ie)
e52d7f85
JMF
455 if not isinstance(ie, type):
456 self._ies_instances[ie.ie_key()] = ie
457 ie.set_downloader(self)
8222d8de 458
56c73665
JMF
459 def get_info_extractor(self, ie_key):
460 """
461 Get an instance of an IE with name ie_key, it will try to get one from
462 the _ies list, if there's no instance it will create a new one and add
463 it to the extractor list.
464 """
465 ie = self._ies_instances.get(ie_key)
466 if ie is None:
467 ie = get_info_extractor(ie_key)()
468 self.add_info_extractor(ie)
469 return ie
470
023fa8c4
JMF
471 def add_default_info_extractors(self):
472 """
473 Add the InfoExtractors returned by gen_extractors to the end of the list
474 """
e52d7f85 475 for ie in gen_extractor_classes():
023fa8c4
JMF
476 self.add_info_extractor(ie)
477
8222d8de
JMF
478 def add_post_processor(self, pp):
479 """Add a PostProcessor object to the end of the chain."""
480 self._pps.append(pp)
481 pp.set_downloader(self)
482
933605d7
JMF
483 def add_progress_hook(self, ph):
484 """Add the progress hook (currently only for the file downloader)"""
485 self._progress_hooks.append(ph)
8ab470f1 486
1c088fa8 487 def _bidi_workaround(self, message):
5d681e96 488 if not hasattr(self, '_output_channel'):
1c088fa8
PH
489 return message
490
5d681e96 491 assert hasattr(self, '_output_process')
11b85ce6 492 assert isinstance(message, compat_str)
6febd1c1
PH
493 line_count = message.count('\n') + 1
494 self._output_process.stdin.write((message + '\n').encode('utf-8'))
5d681e96 495 self._output_process.stdin.flush()
6febd1c1 496 res = ''.join(self._output_channel.readline().decode('utf-8')
9e1a5b84 497 for _ in range(line_count))
6febd1c1 498 return res[:-len('\n')]
1c088fa8 499
8222d8de 500 def to_screen(self, message, skip_eol=False):
0783b09b
PH
501 """Print message to stdout if not in quiet mode."""
502 return self.to_stdout(message, skip_eol, check_quiet=True)
503
734f90bb 504 def _write_string(self, s, out=None):
b58ddb32 505 write_string(s, out=out, encoding=self.params.get('encoding'))
734f90bb 506
0783b09b 507 def to_stdout(self, message, skip_eol=False, check_quiet=False):
8222d8de 508 """Print message to stdout if not in quiet mode."""
8bf9319e 509 if self.params.get('logger'):
43afe285 510 self.params['logger'].debug(message)
0783b09b 511 elif not check_quiet or not self.params.get('quiet', False):
1c088fa8 512 message = self._bidi_workaround(message)
6febd1c1 513 terminator = ['\n', ''][skip_eol]
8222d8de 514 output = message + terminator
1c088fa8 515
734f90bb 516 self._write_string(output, self._screen_file)
8222d8de
JMF
517
518 def to_stderr(self, message):
519 """Print message to stderr."""
11b85ce6 520 assert isinstance(message, compat_str)
8bf9319e 521 if self.params.get('logger'):
43afe285
IB
522 self.params['logger'].error(message)
523 else:
1c088fa8 524 message = self._bidi_workaround(message)
6febd1c1 525 output = message + '\n'
734f90bb 526 self._write_string(output, self._err_file)
8222d8de 527
1e5b9a95
PH
528 def to_console_title(self, message):
529 if not self.params.get('consoletitle', False):
530 return
4bede0d8
C
531 if compat_os_name == 'nt':
532 if ctypes.windll.kernel32.GetConsoleWindow():
533 # c_wchar_p() might not be necessary if `message` is
534 # already of type unicode()
535 ctypes.windll.kernel32.SetConsoleTitleW(ctypes.c_wchar_p(message))
1e5b9a95 536 elif 'TERM' in os.environ:
734f90bb 537 self._write_string('\033]0;%s\007' % message, self._screen_file)
1e5b9a95 538
bdde425c
PH
539 def save_console_title(self):
540 if not self.params.get('consoletitle', False):
541 return
94c3442e
S
542 if self.params.get('simulate', False):
543 return
4bede0d8 544 if compat_os_name != 'nt' and 'TERM' in os.environ:
efd6c574 545 # Save the title on stack
734f90bb 546 self._write_string('\033[22;0t', self._screen_file)
bdde425c
PH
547
548 def restore_console_title(self):
549 if not self.params.get('consoletitle', False):
550 return
94c3442e
S
551 if self.params.get('simulate', False):
552 return
4bede0d8 553 if compat_os_name != 'nt' and 'TERM' in os.environ:
efd6c574 554 # Restore the title from stack
734f90bb 555 self._write_string('\033[23;0t', self._screen_file)
bdde425c
PH
556
557 def __enter__(self):
558 self.save_console_title()
559 return self
560
561 def __exit__(self, *args):
562 self.restore_console_title()
f89197d7 563
dca08720 564 if self.params.get('cookiefile') is not None:
1bab3437 565 self.cookiejar.save(ignore_discard=True, ignore_expires=True)
bdde425c 566
8222d8de
JMF
567 def trouble(self, message=None, tb=None):
568 """Determine action to take when a download problem appears.
569
570 Depending on if the downloader has been configured to ignore
571 download errors or not, this method may throw an exception or
572 not when errors are found, after printing the message.
573
574 tb, if given, is additional traceback information.
575 """
576 if message is not None:
577 self.to_stderr(message)
578 if self.params.get('verbose'):
579 if tb is None:
580 if sys.exc_info()[0]: # if .trouble has been called from an except block
6febd1c1 581 tb = ''
8222d8de 582 if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
6febd1c1 583 tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info))
c0384f22 584 tb += encode_compat_str(traceback.format_exc())
8222d8de
JMF
585 else:
586 tb_data = traceback.format_list(traceback.extract_stack())
6febd1c1 587 tb = ''.join(tb_data)
8222d8de
JMF
588 self.to_stderr(tb)
589 if not self.params.get('ignoreerrors', False):
590 if sys.exc_info()[0] and hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
591 exc_info = sys.exc_info()[1].exc_info
592 else:
593 exc_info = sys.exc_info()
594 raise DownloadError(message, exc_info)
595 self._download_retcode = 1
596
597 def report_warning(self, message):
598 '''
599 Print the message to stderr, it will be prefixed with 'WARNING:'
600 If stderr is a tty file the 'WARNING:' will be colored
601 '''
6d07ce01
JMF
602 if self.params.get('logger') is not None:
603 self.params['logger'].warning(message)
8222d8de 604 else:
ad8915b7
PH
605 if self.params.get('no_warnings'):
606 return
e9c0cdd3 607 if not self.params.get('no_color') and self._err_file.isatty() and compat_os_name != 'nt':
6d07ce01
JMF
608 _msg_header = '\033[0;33mWARNING:\033[0m'
609 else:
610 _msg_header = 'WARNING:'
611 warning_message = '%s %s' % (_msg_header, message)
612 self.to_stderr(warning_message)
8222d8de
JMF
613
614 def report_error(self, message, tb=None):
615 '''
616 Do the same as trouble, but prefixes the message with 'ERROR:', colored
617 in red if stderr is a tty file.
618 '''
e9c0cdd3 619 if not self.params.get('no_color') and self._err_file.isatty() and compat_os_name != 'nt':
6febd1c1 620 _msg_header = '\033[0;31mERROR:\033[0m'
8222d8de 621 else:
6febd1c1
PH
622 _msg_header = 'ERROR:'
623 error_message = '%s %s' % (_msg_header, message)
8222d8de
JMF
624 self.trouble(error_message, tb)
625
8222d8de
JMF
626 def report_file_already_downloaded(self, file_name):
627 """Report file has already been fully downloaded."""
628 try:
6febd1c1 629 self.to_screen('[download] %s has already been downloaded' % file_name)
ce02ed60 630 except UnicodeEncodeError:
6febd1c1 631 self.to_screen('[download] The file has already been downloaded')
8222d8de 632
8222d8de
JMF
633 def prepare_filename(self, info_dict):
634 """Generate the output filename."""
635 try:
636 template_dict = dict(info_dict)
637
638 template_dict['epoch'] = int(time.time())
639 autonumber_size = self.params.get('autonumber_size')
640 if autonumber_size is None:
641 autonumber_size = 5
89db639d 642 template_dict['autonumber'] = self.params.get('autonumber_start', 1) - 1 + self._num_downloads
17b75c0d
PH
643 if template_dict.get('resolution') is None:
644 if template_dict.get('width') and template_dict.get('height'):
645 template_dict['resolution'] = '%dx%d' % (template_dict['width'], template_dict['height'])
646 elif template_dict.get('height'):
805ef3c6 647 template_dict['resolution'] = '%sp' % template_dict['height']
17b75c0d 648 elif template_dict.get('width'):
51ce9117 649 template_dict['resolution'] = '%dx?' % template_dict['width']
8222d8de 650
586a91b6 651 sanitize = lambda k, v: sanitize_filename(
45598aab 652 compat_str(v),
1bb5c511 653 restricted=self.params.get('restrictfilenames'),
40df485f 654 is_id=(k == 'id' or k.endswith('_id')))
d0d9ade4 655 template_dict = dict((k, v if isinstance(v, compat_numeric_types) else sanitize(k, v))
45598aab 656 for k, v in template_dict.items()
f0e14fdd 657 if v is not None and not isinstance(v, (list, tuple, dict)))
6febd1c1 658 template_dict = collections.defaultdict(lambda: 'NA', template_dict)
8222d8de 659
b3613d36 660 outtmpl = self.params.get('outtmpl', DEFAULT_OUTTMPL)
d0d9ade4 661
89db639d
S
662 # For fields playlist_index and autonumber convert all occurrences
663 # of %(field)s to %(field)0Nd for backward compatibility
664 field_size_compat_map = {
665 'playlist_index': len(str(template_dict['n_entries'])),
666 'autonumber': autonumber_size,
667 }
668 FIELD_SIZE_COMPAT_RE = r'(?<!%)%\((?P<field>autonumber|playlist_index)\)s'
669 mobj = re.search(FIELD_SIZE_COMPAT_RE, outtmpl)
670 if mobj:
671 outtmpl = re.sub(
672 FIELD_SIZE_COMPAT_RE,
673 r'%%(\1)0%dd' % field_size_compat_map[mobj.group('field')],
674 outtmpl)
675
d0d9ade4
S
676 # Missing numeric fields used together with integer presentation types
677 # in format specification will break the argument substitution since
678 # string 'NA' is returned for missing fields. We will patch output
679 # template for missing fields to meet string presentation type.
c9969434 680 for numeric_field in self._NUMERIC_FIELDS:
d0d9ade4
S
681 if numeric_field not in template_dict:
682 # As of [1] format syntax is:
683 # %[mapping_key][conversion_flags][minimum_width][.precision][length_modifier]type
684 # 1. https://docs.python.org/2/library/stdtypes.html#string-formatting
685 FORMAT_RE = r'''(?x)
686 (?<!%)
687 %
688 \({0}\) # mapping key
689 (?:[#0\-+ ]+)? # conversion flags (optional)
690 (?:\d+)? # minimum field width (optional)
691 (?:\.\d+)? # precision (optional)
692 [hlL]? # length modifier (optional)
693 [diouxXeEfFgGcrs%] # conversion type
694 '''
695 outtmpl = re.sub(
696 FORMAT_RE.format(numeric_field),
697 r'%({0})s'.format(numeric_field), outtmpl)
698
15da37c7
S
699 # expand_path translates '%%' into '%' and '$$' into '$'
700 # correspondingly that is not what we want since we need to keep
701 # '%%' intact for template dict substitution step. Working around
702 # with boundary-alike separator hack.
961ea474 703 sep = ''.join([random.choice(ascii_letters) for _ in range(32)])
15da37c7
S
704 outtmpl = outtmpl.replace('%%', '%{0}%'.format(sep)).replace('$$', '${0}$'.format(sep))
705
706 # outtmpl should be expand_path'ed before template dict substitution
707 # because meta fields may contain env variables we don't want to
708 # be expanded. For example, for outtmpl "%(title)s.%(ext)s" and
709 # title "Hello $PATH", we don't want `$PATH` to be expanded.
710 filename = expand_path(outtmpl).replace(sep, '') % template_dict
711
3a0d2f52
S
712 # Temporary fix for #4787
713 # 'Treat' all problem characters by passing filename through preferredencoding
714 # to workaround encoding issues with subprocess on python2 @ Windows
715 if sys.version_info < (3, 0) and sys.platform == 'win32':
716 filename = encodeFilename(filename, True).decode(preferredencoding())
b3613d36 717 return sanitize_path(filename)
8222d8de 718 except ValueError as err:
6febd1c1 719 self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')')
8222d8de
JMF
720 return None
721
442c37b7 722 def _match_entry(self, info_dict, incomplete):
6ec6cb4e 723 """ Returns None iff the file should be downloaded """
8222d8de 724
6febd1c1 725 video_title = info_dict.get('title', info_dict.get('id', 'video'))
7012b23c
PH
726 if 'title' in info_dict:
727 # This can happen when we're just evaluating the playlist
728 title = info_dict['title']
729 matchtitle = self.params.get('matchtitle', False)
730 if matchtitle:
731 if not re.search(matchtitle, title, re.IGNORECASE):
6febd1c1 732 return '"' + title + '" title did not match pattern "' + matchtitle + '"'
7012b23c
PH
733 rejecttitle = self.params.get('rejecttitle', False)
734 if rejecttitle:
735 if re.search(rejecttitle, title, re.IGNORECASE):
6febd1c1 736 return '"' + title + '" title matched reject pattern "' + rejecttitle + '"'
d800609c 737 date = info_dict.get('upload_date')
8222d8de
JMF
738 if date is not None:
739 dateRange = self.params.get('daterange', DateRange())
740 if date not in dateRange:
6febd1c1 741 return '%s upload date is not in range %s' % (date_from_str(date).isoformat(), dateRange)
d800609c 742 view_count = info_dict.get('view_count')
5fe18bdb
PH
743 if view_count is not None:
744 min_views = self.params.get('min_views')
745 if min_views is not None and view_count < min_views:
6febd1c1 746 return 'Skipping %s, because it has not reached minimum view count (%d/%d)' % (video_title, view_count, min_views)
5fe18bdb
PH
747 max_views = self.params.get('max_views')
748 if max_views is not None and view_count > max_views:
6febd1c1 749 return 'Skipping %s, because it has exceeded the maximum view count (%d/%d)' % (video_title, view_count, max_views)
05900629 750 if age_restricted(info_dict.get('age_limit'), self.params.get('age_limit')):
347de493 751 return 'Skipping "%s" because it is age restricted' % video_title
c1c9a79c 752 if self.in_download_archive(info_dict):
6febd1c1 753 return '%s has already been recorded in archive' % video_title
347de493 754
442c37b7
PH
755 if not incomplete:
756 match_filter = self.params.get('match_filter')
757 if match_filter is not None:
758 ret = match_filter(info_dict)
759 if ret is not None:
760 return ret
347de493 761
8222d8de 762 return None
fe7e0c98 763
b6c45014
JMF
764 @staticmethod
765 def add_extra_info(info_dict, extra_info):
766 '''Set the keys from extra_info in info dict if they are missing'''
767 for key, value in extra_info.items():
768 info_dict.setdefault(key, value)
769
7fc3fa05 770 def extract_info(self, url, download=True, ie_key=None, extra_info={},
61aa5ba3 771 process=True, force_generic_extractor=False):
8222d8de
JMF
772 '''
773 Returns a list with a dictionary for each video we find.
774 If 'download', also downloads the videos.
775 extra_info is a dict containing the extra values to add to each result
613b2d9d 776 '''
fe7e0c98 777
61aa5ba3 778 if not ie_key and force_generic_extractor:
d22dec74
S
779 ie_key = 'Generic'
780
8222d8de 781 if ie_key:
56c73665 782 ies = [self.get_info_extractor(ie_key)]
8222d8de
JMF
783 else:
784 ies = self._ies
785
786 for ie in ies:
787 if not ie.suitable(url):
788 continue
789
e52d7f85 790 ie = self.get_info_extractor(ie.ie_key())
8222d8de 791 if not ie.working():
6febd1c1
PH
792 self.report_warning('The program functionality for this site has been marked as broken, '
793 'and will probably not work.')
8222d8de
JMF
794
795 try:
796 ie_result = ie.extract(url)
5f6a1245 797 if ie_result is None: # Finished already (backwards compatibility; listformats and friends should be moved here)
8222d8de
JMF
798 break
799 if isinstance(ie_result, list):
800 # Backwards compatibility: old IE result format
8222d8de
JMF
801 ie_result = {
802 '_type': 'compat_list',
803 'entries': ie_result,
804 }
ea38e55f 805 self.add_default_extra_info(ie_result, ie, url)
7fc3fa05
PH
806 if process:
807 return self.process_ie_result(ie_result, download, extra_info)
808 else:
809 return ie_result
773f291d
S
810 except GeoRestrictedError as e:
811 msg = e.msg
812 if e.countries:
813 msg += '\nThis video is available in %s.' % ', '.join(
814 map(ISO3166Utils.short2full, e.countries))
815 msg += '\nYou might want to use a VPN or a proxy server (with --proxy) to workaround.'
816 self.report_error(msg)
817 break
fb043a6e 818 except ExtractorError as e: # An error we somewhat expected
2c74e6fa 819 self.report_error(compat_str(e), e.format_traceback())
8222d8de 820 break
d3e5bbf4
PH
821 except MaxDownloadsReached:
822 raise
8222d8de
JMF
823 except Exception as e:
824 if self.params.get('ignoreerrors', False):
9b9c5355 825 self.report_error(error_to_compat_str(e), tb=encode_compat_str(traceback.format_exc()))
8222d8de
JMF
826 break
827 else:
828 raise
829 else:
1a489545 830 self.report_error('no suitable InfoExtractor for URL %s' % url)
fe7e0c98 831
ea38e55f
PH
832 def add_default_extra_info(self, ie_result, ie, url):
833 self.add_extra_info(ie_result, {
834 'extractor': ie.IE_NAME,
835 'webpage_url': url,
836 'webpage_url_basename': url_basename(url),
837 'extractor_key': ie.ie_key(),
838 })
839
8222d8de
JMF
840 def process_ie_result(self, ie_result, download=True, extra_info={}):
841 """
842 Take the result of the ie(may be modified) and resolve all unresolved
843 references (URLs, playlist items).
844
845 It will also download the videos if 'download'.
846 Returns the resolved ie_result.
847 """
e8ee972c
PH
848 result_type = ie_result.get('_type', 'video')
849
057a5206 850 if result_type in ('url', 'url_transparent'):
134c6ea8 851 ie_result['url'] = sanitize_url(ie_result['url'])
057a5206 852 extract_flat = self.params.get('extract_flat', False)
3089bc74
S
853 if ((extract_flat == 'in_playlist' and 'playlist' in extra_info)
854 or extract_flat is True):
057a5206
PH
855 if self.params.get('forcejson', False):
856 self.to_stdout(json.dumps(ie_result))
e8ee972c
PH
857 return ie_result
858
8222d8de 859 if result_type == 'video':
b6c45014 860 self.add_extra_info(ie_result, extra_info)
feee2ecf 861 return self.process_video_result(ie_result, download=download)
8222d8de
JMF
862 elif result_type == 'url':
863 # We have to add extra_info to the results because it may be
864 # contained in a playlist
865 return self.extract_info(ie_result['url'],
866 download,
867 ie_key=ie_result.get('ie_key'),
868 extra_info=extra_info)
7fc3fa05
PH
869 elif result_type == 'url_transparent':
870 # Use the information from the embedding page
871 info = self.extract_info(
872 ie_result['url'], ie_key=ie_result.get('ie_key'),
873 extra_info=extra_info, download=False, process=False)
874
1640eb09
S
875 # extract_info may return None when ignoreerrors is enabled and
876 # extraction failed with an error, don't crash and return early
877 # in this case
878 if not info:
879 return info
880
412c617d
PH
881 force_properties = dict(
882 (k, v) for k, v in ie_result.items() if v is not None)
0396806f 883 for f in ('_type', 'url', 'id', 'extractor', 'extractor_key', 'ie_key'):
412c617d
PH
884 if f in force_properties:
885 del force_properties[f]
886 new_result = info.copy()
887 new_result.update(force_properties)
7fc3fa05 888
0563f7ac
S
889 # Extracted info may not be a video result (i.e.
890 # info.get('_type', 'video') != video) but rather an url or
891 # url_transparent. In such cases outer metadata (from ie_result)
892 # should be propagated to inner one (info). For this to happen
893 # _type of info should be overridden with url_transparent. This
067aa17e 894 # fixes issue from https://github.com/ytdl-org/youtube-dl/pull/11163.
0563f7ac
S
895 if new_result.get('_type') == 'url':
896 new_result['_type'] = 'url_transparent'
7fc3fa05
PH
897
898 return self.process_ie_result(
899 new_result, download=download, extra_info=extra_info)
40fcba5e 900 elif result_type in ('playlist', 'multi_video'):
8222d8de 901 # We process each entry in the playlist
d800609c 902 playlist = ie_result.get('title') or ie_result.get('id')
6febd1c1 903 self.to_screen('[download] Downloading playlist: %s' % playlist)
8222d8de
JMF
904
905 playlist_results = []
906
8222d8de 907 playliststart = self.params.get('playliststart', 1) - 1
d800609c 908 playlistend = self.params.get('playlistend')
a19fd00c 909 # For backwards compatibility, interpret -1 as whole list
8222d8de 910 if playlistend == -1:
a19fd00c 911 playlistend = None
8222d8de 912
d800609c 913 playlistitems_str = self.params.get('playlist_items')
c14e88f0
PH
914 playlistitems = None
915 if playlistitems_str is not None:
916 def iter_playlistitems(format):
917 for string_segment in format.split(','):
918 if '-' in string_segment:
919 start, end = string_segment.split('-')
920 for item in range(int(start), int(end) + 1):
921 yield int(item)
922 else:
923 yield int(string_segment)
cd6fc19e 924 playlistitems = orderedSet(iter_playlistitems(playlistitems_str))
c14e88f0 925
b82f815f 926 ie_entries = ie_result['entries']
7e85e872
S
927
928 def make_playlistitems_entries(list_ie_entries):
929 num_entries = len(list_ie_entries)
930 return [
931 list_ie_entries[i - 1] for i in playlistitems
932 if -num_entries <= i - 1 < num_entries]
933
934 def report_download(num_entries):
935 self.to_screen(
936 '[%s] playlist %s: Downloading %d videos' %
937 (ie_result['extractor'], playlist, num_entries))
938
b82f815f
PH
939 if isinstance(ie_entries, list):
940 n_all_entries = len(ie_entries)
c14e88f0 941 if playlistitems:
7e85e872 942 entries = make_playlistitems_entries(ie_entries)
c14e88f0
PH
943 else:
944 entries = ie_entries[playliststart:playlistend]
b7ab0590
PH
945 n_entries = len(entries)
946 self.to_screen(
611c1dd9 947 '[%s] playlist %s: Collected %d video ids (downloading %d of them)' %
b7ab0590 948 (ie_result['extractor'], playlist, n_all_entries, n_entries))
b82f815f 949 elif isinstance(ie_entries, PagedList):
c14e88f0
PH
950 if playlistitems:
951 entries = []
952 for item in playlistitems:
953 entries.extend(ie_entries.getslice(
954 item - 1, item
955 ))
956 else:
957 entries = ie_entries.getslice(
958 playliststart, playlistend)
b7ab0590 959 n_entries = len(entries)
7e85e872 960 report_download(n_entries)
b82f815f 961 else: # iterable
c14e88f0 962 if playlistitems:
5871ebac
S
963 entries = make_playlistitems_entries(list(itertools.islice(
964 ie_entries, 0, max(playlistitems))))
c14e88f0
PH
965 else:
966 entries = list(itertools.islice(
967 ie_entries, playliststart, playlistend))
b82f815f 968 n_entries = len(entries)
7e85e872 969 report_download(n_entries)
8222d8de 970
ff815fe6
MS
971 if self.params.get('playlistreverse', False):
972 entries = entries[::-1]
973
75822ca7
TC
974 if self.params.get('playlistrandom', False):
975 random.shuffle(entries)
976
0016b84e
S
977 x_forwarded_for = ie_result.get('__x_forwarded_for_ip')
978
fe7e0c98 979 for i, entry in enumerate(entries, 1):
734ea11e 980 self.to_screen('[download] Downloading video %s of %s' % (i, n_entries))
0016b84e
S
981 # This __x_forwarded_for_ip thing is a bit ugly but requires
982 # minimal changes
983 if x_forwarded_for:
984 entry['__x_forwarded_for_ip'] = x_forwarded_for
8222d8de 985 extra = {
c6b4132a 986 'n_entries': n_entries,
fe7e0c98 987 'playlist': playlist,
a1cf99d0
PH
988 'playlist_id': ie_result.get('id'),
989 'playlist_title': ie_result.get('title'),
3961c6cb
S
990 'playlist_uploader': ie_result.get('uploader'),
991 'playlist_uploader_id': ie_result.get('uploader_id'),
fe7e0c98 992 'playlist_index': i + playliststart,
b6c45014 993 'extractor': ie_result['extractor'],
9103bbc5 994 'webpage_url': ie_result['webpage_url'],
29eb5174 995 'webpage_url_basename': url_basename(ie_result['webpage_url']),
be97abc2 996 'extractor_key': ie_result['extractor_key'],
fe7e0c98 997 }
7012b23c 998
442c37b7 999 reason = self._match_entry(entry, incomplete=True)
7012b23c 1000 if reason is not None:
6febd1c1 1001 self.to_screen('[download] ' + reason)
7012b23c
PH
1002 continue
1003
8222d8de
JMF
1004 entry_result = self.process_ie_result(entry,
1005 download=download,
1006 extra_info=extra)
1007 playlist_results.append(entry_result)
1008 ie_result['entries'] = playlist_results
371c3b79 1009 self.to_screen('[download] Finished downloading playlist: %s' % playlist)
8222d8de
JMF
1010 return ie_result
1011 elif result_type == 'compat_list':
c9bf4114
PH
1012 self.report_warning(
1013 'Extractor %s returned a compat_list result. '
1014 'It needs to be updated.' % ie_result.get('extractor'))
5f6a1245 1015
8222d8de 1016 def _fixup(r):
9e1a5b84
JW
1017 self.add_extra_info(
1018 r,
9103bbc5
JMF
1019 {
1020 'extractor': ie_result['extractor'],
1021 'webpage_url': ie_result['webpage_url'],
29eb5174 1022 'webpage_url_basename': url_basename(ie_result['webpage_url']),
be97abc2 1023 'extractor_key': ie_result['extractor_key'],
9e1a5b84
JW
1024 }
1025 )
8222d8de
JMF
1026 return r
1027 ie_result['entries'] = [
b6c45014 1028 self.process_ie_result(_fixup(r), download, extra_info)
8222d8de
JMF
1029 for r in ie_result['entries']
1030 ]
1031 return ie_result
1032 else:
1033 raise Exception('Invalid result type: %s' % result_type)
1034
67134eab
JMF
1035 def _build_format_filter(self, filter_spec):
1036 " Returns a function to filter the formats according to the filter_spec "
083c9df9
PH
1037
1038 OPERATORS = {
1039 '<': operator.lt,
1040 '<=': operator.le,
1041 '>': operator.gt,
1042 '>=': operator.ge,
1043 '=': operator.eq,
1044 '!=': operator.ne,
1045 }
67134eab 1046 operator_rex = re.compile(r'''(?x)\s*
a03a3c80 1047 (?P<key>width|height|tbr|abr|vbr|asr|filesize|filesize_approx|fps)
083c9df9
PH
1048 \s*(?P<op>%s)(?P<none_inclusive>\s*\?)?\s*
1049 (?P<value>[0-9.]+(?:[kKmMgGtTpPeEzZyY]i?[Bb]?)?)
67134eab 1050 $
083c9df9 1051 ''' % '|'.join(map(re.escape, OPERATORS.keys())))
67134eab 1052 m = operator_rex.search(filter_spec)
9ddb6925
S
1053 if m:
1054 try:
1055 comparison_value = int(m.group('value'))
1056 except ValueError:
1057 comparison_value = parse_filesize(m.group('value'))
1058 if comparison_value is None:
1059 comparison_value = parse_filesize(m.group('value') + 'B')
1060 if comparison_value is None:
1061 raise ValueError(
1062 'Invalid value %r in format specification %r' % (
67134eab 1063 m.group('value'), filter_spec))
9ddb6925
S
1064 op = OPERATORS[m.group('op')]
1065
083c9df9 1066 if not m:
9ddb6925
S
1067 STR_OPERATORS = {
1068 '=': operator.eq,
10d33b34
YCH
1069 '^=': lambda attr, value: attr.startswith(value),
1070 '$=': lambda attr, value: attr.endswith(value),
1071 '*=': lambda attr, value: value in attr,
9ddb6925 1072 }
67134eab 1073 str_operator_rex = re.compile(r'''(?x)
d5aacf9a 1074 \s*(?P<key>ext|acodec|vcodec|container|protocol|format_id)
2cc779f4 1075 \s*(?P<negation>!\s*)?(?P<op>%s)(?P<none_inclusive>\s*\?)?
b0df5223 1076 \s*(?P<value>[a-zA-Z0-9._-]+)
67134eab 1077 \s*$
9ddb6925 1078 ''' % '|'.join(map(re.escape, STR_OPERATORS.keys())))
67134eab 1079 m = str_operator_rex.search(filter_spec)
9ddb6925
S
1080 if m:
1081 comparison_value = m.group('value')
2cc779f4
S
1082 str_op = STR_OPERATORS[m.group('op')]
1083 if m.group('negation'):
e118a879 1084 op = lambda attr, value: not str_op(attr, value)
2cc779f4
S
1085 else:
1086 op = str_op
083c9df9 1087
9ddb6925 1088 if not m:
67134eab 1089 raise ValueError('Invalid filter specification %r' % filter_spec)
083c9df9
PH
1090
1091 def _filter(f):
1092 actual_value = f.get(m.group('key'))
1093 if actual_value is None:
1094 return m.group('none_inclusive')
1095 return op(actual_value, comparison_value)
67134eab
JMF
1096 return _filter
1097
0017d9ad 1098 def _default_format_spec(self, info_dict, download=True):
0017d9ad 1099
af0f7428
S
1100 def can_merge():
1101 merger = FFmpegMergerPP(self)
1102 return merger.available and merger.can_merge()
1103
1104 def prefer_best():
0017d9ad 1105 if self.params.get('simulate', False):
af0f7428 1106 return False
0017d9ad 1107 if not download:
0017d9ad 1108 return False
af0f7428
S
1109 if self.params.get('outtmpl', DEFAULT_OUTTMPL) == '-':
1110 return True
0017d9ad 1111 if info_dict.get('is_live'):
af0f7428
S
1112 return True
1113 if not can_merge():
1114 return True
1115 return False
1116
1117 req_format_list = ['bestvideo+bestaudio', 'best']
1118 if prefer_best():
1119 req_format_list.reverse()
0017d9ad
S
1120 return '/'.join(req_format_list)
1121
67134eab
JMF
1122 def build_format_selector(self, format_spec):
1123 def syntax_error(note, start):
1124 message = (
1125 'Invalid format specification: '
1126 '{0}\n\t{1}\n\t{2}^'.format(note, format_spec, ' ' * start[1]))
1127 return SyntaxError(message)
1128
1129 PICKFIRST = 'PICKFIRST'
1130 MERGE = 'MERGE'
1131 SINGLE = 'SINGLE'
0130afb7 1132 GROUP = 'GROUP'
67134eab
JMF
1133 FormatSelector = collections.namedtuple('FormatSelector', ['type', 'selector', 'filters'])
1134
1135 def _parse_filter(tokens):
1136 filter_parts = []
1137 for type, string, start, _, _ in tokens:
1138 if type == tokenize.OP and string == ']':
1139 return ''.join(filter_parts)
1140 else:
1141 filter_parts.append(string)
1142
232541df 1143 def _remove_unused_ops(tokens):
17cc1534 1144 # Remove operators that we don't use and join them with the surrounding strings
232541df
JMF
1145 # for example: 'mp4' '-' 'baseline' '-' '16x9' is converted to 'mp4-baseline-16x9'
1146 ALLOWED_OPS = ('/', '+', ',', '(', ')')
1147 last_string, last_start, last_end, last_line = None, None, None, None
1148 for type, string, start, end, line in tokens:
1149 if type == tokenize.OP and string == '[':
1150 if last_string:
1151 yield tokenize.NAME, last_string, last_start, last_end, last_line
1152 last_string = None
1153 yield type, string, start, end, line
1154 # everything inside brackets will be handled by _parse_filter
1155 for type, string, start, end, line in tokens:
1156 yield type, string, start, end, line
1157 if type == tokenize.OP and string == ']':
1158 break
1159 elif type == tokenize.OP and string in ALLOWED_OPS:
1160 if last_string:
1161 yield tokenize.NAME, last_string, last_start, last_end, last_line
1162 last_string = None
1163 yield type, string, start, end, line
1164 elif type in [tokenize.NAME, tokenize.NUMBER, tokenize.OP]:
1165 if not last_string:
1166 last_string = string
1167 last_start = start
1168 last_end = end
1169 else:
1170 last_string += string
1171 if last_string:
1172 yield tokenize.NAME, last_string, last_start, last_end, last_line
1173
cf2ac6df 1174 def _parse_format_selection(tokens, inside_merge=False, inside_choice=False, inside_group=False):
67134eab
JMF
1175 selectors = []
1176 current_selector = None
1177 for type, string, start, _, _ in tokens:
1178 # ENCODING is only defined in python 3.x
1179 if type == getattr(tokenize, 'ENCODING', None):
1180 continue
1181 elif type in [tokenize.NAME, tokenize.NUMBER]:
1182 current_selector = FormatSelector(SINGLE, string, [])
1183 elif type == tokenize.OP:
cf2ac6df
JMF
1184 if string == ')':
1185 if not inside_group:
1186 # ')' will be handled by the parentheses group
1187 tokens.restore_last_token()
67134eab 1188 break
cf2ac6df 1189 elif inside_merge and string in ['/', ',']:
0130afb7
JMF
1190 tokens.restore_last_token()
1191 break
cf2ac6df
JMF
1192 elif inside_choice and string == ',':
1193 tokens.restore_last_token()
1194 break
1195 elif string == ',':
0a31a350
JMF
1196 if not current_selector:
1197 raise syntax_error('"," must follow a format selector', start)
67134eab
JMF
1198 selectors.append(current_selector)
1199 current_selector = None
1200 elif string == '/':
d96d604e
JMF
1201 if not current_selector:
1202 raise syntax_error('"/" must follow a format selector', start)
67134eab 1203 first_choice = current_selector
cf2ac6df 1204 second_choice = _parse_format_selection(tokens, inside_choice=True)
f5f4a27a 1205 current_selector = FormatSelector(PICKFIRST, (first_choice, second_choice), [])
67134eab
JMF
1206 elif string == '[':
1207 if not current_selector:
1208 current_selector = FormatSelector(SINGLE, 'best', [])
1209 format_filter = _parse_filter(tokens)
1210 current_selector.filters.append(format_filter)
0130afb7
JMF
1211 elif string == '(':
1212 if current_selector:
1213 raise syntax_error('Unexpected "("', start)
cf2ac6df
JMF
1214 group = _parse_format_selection(tokens, inside_group=True)
1215 current_selector = FormatSelector(GROUP, group, [])
67134eab
JMF
1216 elif string == '+':
1217 video_selector = current_selector
cf2ac6df 1218 audio_selector = _parse_format_selection(tokens, inside_merge=True)
0a31a350
JMF
1219 if not video_selector or not audio_selector:
1220 raise syntax_error('"+" must be between two format selectors', start)
cf2ac6df 1221 current_selector = FormatSelector(MERGE, (video_selector, audio_selector), [])
67134eab
JMF
1222 else:
1223 raise syntax_error('Operator not recognized: "{0}"'.format(string), start)
1224 elif type == tokenize.ENDMARKER:
1225 break
1226 if current_selector:
1227 selectors.append(current_selector)
1228 return selectors
1229
1230 def _build_selector_function(selector):
1231 if isinstance(selector, list):
1232 fs = [_build_selector_function(s) for s in selector]
1233
317f7ab6 1234 def selector_function(ctx):
67134eab 1235 for f in fs:
317f7ab6 1236 for format in f(ctx):
67134eab
JMF
1237 yield format
1238 return selector_function
0130afb7
JMF
1239 elif selector.type == GROUP:
1240 selector_function = _build_selector_function(selector.selector)
67134eab
JMF
1241 elif selector.type == PICKFIRST:
1242 fs = [_build_selector_function(s) for s in selector.selector]
1243
317f7ab6 1244 def selector_function(ctx):
67134eab 1245 for f in fs:
317f7ab6 1246 picked_formats = list(f(ctx))
67134eab
JMF
1247 if picked_formats:
1248 return picked_formats
1249 return []
1250 elif selector.type == SINGLE:
1251 format_spec = selector.selector
1252
317f7ab6
S
1253 def selector_function(ctx):
1254 formats = list(ctx['formats'])
bb8e5536
JMF
1255 if not formats:
1256 return
5acfa126
JMF
1257 if format_spec == 'all':
1258 for f in formats:
1259 yield f
1260 elif format_spec in ['best', 'worst', None]:
67134eab
JMF
1261 format_idx = 0 if format_spec == 'worst' else -1
1262 audiovideo_formats = [
1263 f for f in formats
1264 if f.get('vcodec') != 'none' and f.get('acodec') != 'none']
1265 if audiovideo_formats:
1266 yield audiovideo_formats[format_idx]
317f7ab6
S
1267 # for extractors with incomplete formats (audio only (soundcloud)
1268 # or video only (imgur)) we will fallback to best/worst
1269 # {video,audio}-only format
1270 elif ctx['incomplete_formats']:
67134eab
JMF
1271 yield formats[format_idx]
1272 elif format_spec == 'bestaudio':
1273 audio_formats = [
1274 f for f in formats
1275 if f.get('vcodec') == 'none']
1276 if audio_formats:
1277 yield audio_formats[-1]
1278 elif format_spec == 'worstaudio':
1279 audio_formats = [
1280 f for f in formats
1281 if f.get('vcodec') == 'none']
1282 if audio_formats:
1283 yield audio_formats[0]
1284 elif format_spec == 'bestvideo':
1285 video_formats = [
1286 f for f in formats
1287 if f.get('acodec') == 'none']
1288 if video_formats:
1289 yield video_formats[-1]
1290 elif format_spec == 'worstvideo':
1291 video_formats = [
1292 f for f in formats
1293 if f.get('acodec') == 'none']
1294 if video_formats:
1295 yield video_formats[0]
1296 else:
1297 extensions = ['mp4', 'flv', 'webm', '3gp', 'm4a', 'mp3', 'ogg', 'aac', 'wav']
1298 if format_spec in extensions:
1299 filter_f = lambda f: f['ext'] == format_spec
1300 else:
1301 filter_f = lambda f: f['format_id'] == format_spec
1302 matches = list(filter(filter_f, formats))
1303 if matches:
1304 yield matches[-1]
1305 elif selector.type == MERGE:
1306 def _merge(formats_info):
1307 format_1, format_2 = [f['format_id'] for f in formats_info]
1308 # The first format must contain the video and the
1309 # second the audio
1310 if formats_info[0].get('vcodec') == 'none':
1311 self.report_error('The first format must '
1312 'contain the video, try using '
1313 '"-f %s+%s"' % (format_2, format_1))
1314 return
3d24bbfb
S
1315 # Formats must be opposite (video+audio)
1316 if formats_info[0].get('acodec') == 'none' and formats_info[1].get('acodec') == 'none':
1317 self.report_error(
1318 'Both formats %s and %s are video-only, you must specify "-f video+audio"'
1319 % (format_1, format_2))
1320 return
67134eab
JMF
1321 output_ext = (
1322 formats_info[0]['ext']
1323 if self.params.get('merge_output_format') is None
1324 else self.params['merge_output_format'])
1325 return {
1326 'requested_formats': formats_info,
1327 'format': '%s+%s' % (formats_info[0].get('format'),
1328 formats_info[1].get('format')),
1329 'format_id': '%s+%s' % (formats_info[0].get('format_id'),
1330 formats_info[1].get('format_id')),
1331 'width': formats_info[0].get('width'),
1332 'height': formats_info[0].get('height'),
1333 'resolution': formats_info[0].get('resolution'),
1334 'fps': formats_info[0].get('fps'),
1335 'vcodec': formats_info[0].get('vcodec'),
1336 'vbr': formats_info[0].get('vbr'),
1337 'stretched_ratio': formats_info[0].get('stretched_ratio'),
1338 'acodec': formats_info[1].get('acodec'),
1339 'abr': formats_info[1].get('abr'),
1340 'ext': output_ext,
1341 }
1342 video_selector, audio_selector = map(_build_selector_function, selector.selector)
083c9df9 1343
317f7ab6
S
1344 def selector_function(ctx):
1345 for pair in itertools.product(
1346 video_selector(copy.deepcopy(ctx)), audio_selector(copy.deepcopy(ctx))):
67134eab 1347 yield _merge(pair)
083c9df9 1348
67134eab 1349 filters = [self._build_format_filter(f) for f in selector.filters]
083c9df9 1350
317f7ab6
S
1351 def final_selector(ctx):
1352 ctx_copy = copy.deepcopy(ctx)
67134eab 1353 for _filter in filters:
317f7ab6
S
1354 ctx_copy['formats'] = list(filter(_filter, ctx_copy['formats']))
1355 return selector_function(ctx_copy)
67134eab 1356 return final_selector
083c9df9 1357
67134eab 1358 stream = io.BytesIO(format_spec.encode('utf-8'))
0130afb7 1359 try:
232541df 1360 tokens = list(_remove_unused_ops(compat_tokenize_tokenize(stream.readline)))
0130afb7
JMF
1361 except tokenize.TokenError:
1362 raise syntax_error('Missing closing/opening brackets or parenthesis', (0, len(format_spec)))
1363
1364 class TokenIterator(object):
1365 def __init__(self, tokens):
1366 self.tokens = tokens
1367 self.counter = 0
1368
1369 def __iter__(self):
1370 return self
1371
1372 def __next__(self):
1373 if self.counter >= len(self.tokens):
1374 raise StopIteration()
1375 value = self.tokens[self.counter]
1376 self.counter += 1
1377 return value
1378
1379 next = __next__
1380
1381 def restore_last_token(self):
1382 self.counter -= 1
1383
1384 parsed_selector = _parse_format_selection(iter(TokenIterator(tokens)))
67134eab 1385 return _build_selector_function(parsed_selector)
a9c58ad9 1386
e5660ee6
JMF
1387 def _calc_headers(self, info_dict):
1388 res = std_headers.copy()
1389
1390 add_headers = info_dict.get('http_headers')
1391 if add_headers:
1392 res.update(add_headers)
1393
1394 cookies = self._calc_cookies(info_dict)
1395 if cookies:
1396 res['Cookie'] = cookies
1397
0016b84e
S
1398 if 'X-Forwarded-For' not in res:
1399 x_forwarded_for_ip = info_dict.get('__x_forwarded_for_ip')
1400 if x_forwarded_for_ip:
1401 res['X-Forwarded-For'] = x_forwarded_for_ip
1402
e5660ee6
JMF
1403 return res
1404
1405 def _calc_cookies(self, info_dict):
5c2266df 1406 pr = sanitized_Request(info_dict['url'])
e5660ee6 1407 self.cookiejar.add_cookie_header(pr)
662435f7 1408 return pr.get_header('Cookie')
e5660ee6 1409
dd82ffea
JMF
1410 def process_video_result(self, info_dict, download=True):
1411 assert info_dict.get('_type', 'video') == 'video'
1412
bec1fad2
PH
1413 if 'id' not in info_dict:
1414 raise ExtractorError('Missing "id" field in extractor result')
1415 if 'title' not in info_dict:
1416 raise ExtractorError('Missing "title" field in extractor result')
1417
c9969434
S
1418 def report_force_conversion(field, field_not, conversion):
1419 self.report_warning(
1420 '"%s" field is not %s - forcing %s conversion, there is an error in extractor'
1421 % (field, field_not, conversion))
1422
1423 def sanitize_string_field(info, string_field):
1424 field = info.get(string_field)
1425 if field is None or isinstance(field, compat_str):
1426 return
1427 report_force_conversion(string_field, 'a string', 'string')
1428 info[string_field] = compat_str(field)
1429
1430 def sanitize_numeric_fields(info):
1431 for numeric_field in self._NUMERIC_FIELDS:
1432 field = info.get(numeric_field)
1433 if field is None or isinstance(field, compat_numeric_types):
1434 continue
1435 report_force_conversion(numeric_field, 'numeric', 'int')
1436 info[numeric_field] = int_or_none(field)
1437
1438 sanitize_string_field(info_dict, 'id')
1439 sanitize_numeric_fields(info_dict)
be6217b2 1440
dd82ffea
JMF
1441 if 'playlist' not in info_dict:
1442 # It isn't part of a playlist
1443 info_dict['playlist'] = None
1444 info_dict['playlist_index'] = None
1445
d5519808 1446 thumbnails = info_dict.get('thumbnails')
cfb56d1a
PH
1447 if thumbnails is None:
1448 thumbnail = info_dict.get('thumbnail')
1449 if thumbnail:
a7a14d95 1450 info_dict['thumbnails'] = thumbnails = [{'url': thumbnail}]
d5519808 1451 if thumbnails:
be6d7229 1452 thumbnails.sort(key=lambda t: (
d37708fc
RA
1453 t.get('preference') if t.get('preference') is not None else -1,
1454 t.get('width') if t.get('width') is not None else -1,
1455 t.get('height') if t.get('height') is not None else -1,
1456 t.get('id') if t.get('id') is not None else '', t.get('url')))
f6c24009 1457 for i, t in enumerate(thumbnails):
dcf77cf1 1458 t['url'] = sanitize_url(t['url'])
9603e8a7 1459 if t.get('width') and t.get('height'):
d5519808 1460 t['resolution'] = '%dx%d' % (t['width'], t['height'])
f6c24009
PH
1461 if t.get('id') is None:
1462 t['id'] = '%d' % i
d5519808 1463
b7b72db9 1464 if self.params.get('list_thumbnails'):
1465 self.list_thumbnails(info_dict)
1466 return
1467
536a55da
S
1468 thumbnail = info_dict.get('thumbnail')
1469 if thumbnail:
1470 info_dict['thumbnail'] = sanitize_url(thumbnail)
1471 elif thumbnails:
d5519808
PH
1472 info_dict['thumbnail'] = thumbnails[-1]['url']
1473
c9ae7b95 1474 if 'display_id' not in info_dict and 'id' in info_dict:
0afef30b
PH
1475 info_dict['display_id'] = info_dict['id']
1476
955c4514 1477 if info_dict.get('upload_date') is None and info_dict.get('timestamp') is not None:
a55e36f4
S
1478 # Working around out-of-range timestamp values (e.g. negative ones on Windows,
1479 # see http://bugs.python.org/issue1646728)
1480 try:
1481 upload_date = datetime.datetime.utcfromtimestamp(info_dict['timestamp'])
1482 info_dict['upload_date'] = upload_date.strftime('%Y%m%d')
1483 except (ValueError, OverflowError, OSError):
1484 pass
9d2ecdbc 1485
33d2fc2f
S
1486 # Auto generate title fields corresponding to the *_number fields when missing
1487 # in order to always have clean titles. This is very common for TV series.
1488 for field in ('chapter', 'season', 'episode'):
1489 if info_dict.get('%s_number' % field) is not None and not info_dict.get(field):
1490 info_dict[field] = '%s %d' % (field.capitalize(), info_dict['%s_number' % field])
1491
05108a49
S
1492 for cc_kind in ('subtitles', 'automatic_captions'):
1493 cc = info_dict.get(cc_kind)
1494 if cc:
1495 for _, subtitle in cc.items():
1496 for subtitle_format in subtitle:
1497 if subtitle_format.get('url'):
1498 subtitle_format['url'] = sanitize_url(subtitle_format['url'])
1499 if subtitle_format.get('ext') is None:
1500 subtitle_format['ext'] = determine_ext(subtitle_format['url']).lower()
1501
1502 automatic_captions = info_dict.get('automatic_captions')
4bba3716 1503 subtitles = info_dict.get('subtitles')
4bba3716 1504
a504ced0 1505 if self.params.get('listsubtitles', False):
360e1ca5 1506 if 'automatic_captions' in info_dict:
05108a49
S
1507 self.list_subtitles(
1508 info_dict['id'], automatic_captions, 'automatic captions')
4bba3716 1509 self.list_subtitles(info_dict['id'], subtitles, 'subtitles')
a504ced0 1510 return
05108a49 1511
360e1ca5 1512 info_dict['requested_subtitles'] = self.process_subtitles(
05108a49 1513 info_dict['id'], subtitles, automatic_captions)
a504ced0 1514
dd82ffea
JMF
1515 # We now pick which formats have to be downloaded
1516 if info_dict.get('formats') is None:
1517 # There's only one format available
1518 formats = [info_dict]
1519 else:
1520 formats = info_dict['formats']
1521
db95dc13
PH
1522 if not formats:
1523 raise ExtractorError('No video formats found!')
1524
73af5cc8
S
1525 def is_wellformed(f):
1526 url = f.get('url')
a5ac0c47 1527 if not url:
73af5cc8
S
1528 self.report_warning(
1529 '"url" field is missing or empty - skipping format, '
1530 'there is an error in extractor')
a5ac0c47
S
1531 return False
1532 if isinstance(url, bytes):
1533 sanitize_string_field(f, 'url')
1534 return True
73af5cc8
S
1535
1536 # Filter out malformed formats for better extraction robustness
1537 formats = list(filter(is_wellformed, formats))
1538
181c7053
S
1539 formats_dict = {}
1540
dd82ffea 1541 # We check that all the formats have the format and format_id fields
db95dc13 1542 for i, format in enumerate(formats):
c9969434
S
1543 sanitize_string_field(format, 'format_id')
1544 sanitize_numeric_fields(format)
dcf77cf1 1545 format['url'] = sanitize_url(format['url'])
e74e3b63 1546 if not format.get('format_id'):
8016c922 1547 format['format_id'] = compat_str(i)
e2effb08
S
1548 else:
1549 # Sanitize format_id from characters used in format selector expression
ec85ded8 1550 format['format_id'] = re.sub(r'[\s,/+\[\]()]', '_', format['format_id'])
181c7053
S
1551 format_id = format['format_id']
1552 if format_id not in formats_dict:
1553 formats_dict[format_id] = []
1554 formats_dict[format_id].append(format)
1555
1556 # Make sure all formats have unique format_id
1557 for format_id, ambiguous_formats in formats_dict.items():
1558 if len(ambiguous_formats) > 1:
1559 for i, format in enumerate(ambiguous_formats):
1560 format['format_id'] = '%s-%d' % (format_id, i)
1561
1562 for i, format in enumerate(formats):
8c51aa65 1563 if format.get('format') is None:
6febd1c1 1564 format['format'] = '{id} - {res}{note}'.format(
8c51aa65
JMF
1565 id=format['format_id'],
1566 res=self.format_resolution(format),
6febd1c1 1567 note=' ({0})'.format(format['format_note']) if format.get('format_note') is not None else '',
8c51aa65 1568 )
c1002e96 1569 # Automatically determine file extension if missing
5b1d8575 1570 if format.get('ext') is None:
cce929ea 1571 format['ext'] = determine_ext(format['url']).lower()
b5559424
S
1572 # Automatically determine protocol if missing (useful for format
1573 # selection purposes)
6f0be937 1574 if format.get('protocol') is None:
b5559424 1575 format['protocol'] = determine_protocol(format)
e5660ee6
JMF
1576 # Add HTTP headers, so that external programs can use them from the
1577 # json output
1578 full_format_info = info_dict.copy()
1579 full_format_info.update(format)
1580 format['http_headers'] = self._calc_headers(full_format_info)
0016b84e
S
1581 # Remove private housekeeping stuff
1582 if '__x_forwarded_for_ip' in info_dict:
1583 del info_dict['__x_forwarded_for_ip']
dd82ffea 1584
4bcc7bd1 1585 # TODO Central sorting goes here
99e206d5 1586
f89197d7 1587 if formats[0] is not info_dict:
b3d9ef88
JMF
1588 # only set the 'formats' fields if the original info_dict list them
1589 # otherwise we end up with a circular reference, the first (and unique)
f89197d7 1590 # element in the 'formats' field in info_dict is info_dict itself,
dfb1b146 1591 # which can't be exported to json
b3d9ef88 1592 info_dict['formats'] = formats
cfb56d1a 1593 if self.params.get('listformats'):
bfaae0a7 1594 self.list_formats(info_dict)
1595 return
1596
de3ef3ed 1597 req_format = self.params.get('format')
a9c58ad9 1598 if req_format is None:
0017d9ad
S
1599 req_format = self._default_format_spec(info_dict, download=download)
1600 if self.params.get('verbose'):
1601 self.to_stdout('[debug] Default format spec: %s' % req_format)
1602
5acfa126 1603 format_selector = self.build_format_selector(req_format)
317f7ab6
S
1604
1605 # While in format selection we may need to have an access to the original
1606 # format set in order to calculate some metrics or do some processing.
1607 # For now we need to be able to guess whether original formats provided
1608 # by extractor are incomplete or not (i.e. whether extractor provides only
1609 # video-only or audio-only formats) for proper formats selection for
1610 # extractors with such incomplete formats (see
067aa17e 1611 # https://github.com/ytdl-org/youtube-dl/pull/5556).
317f7ab6
S
1612 # Since formats may be filtered during format selection and may not match
1613 # the original formats the results may be incorrect. Thus original formats
1614 # or pre-calculated metrics should be passed to format selection routines
1615 # as well.
1616 # We will pass a context object containing all necessary additional data
1617 # instead of just formats.
1618 # This fixes incorrect format selection issue (see
067aa17e 1619 # https://github.com/ytdl-org/youtube-dl/issues/10083).
2e221ca3 1620 incomplete_formats = (
317f7ab6 1621 # All formats are video-only or
3089bc74 1622 all(f.get('vcodec') != 'none' and f.get('acodec') == 'none' for f in formats)
317f7ab6 1623 # all formats are audio-only
3089bc74 1624 or all(f.get('vcodec') == 'none' and f.get('acodec') != 'none' for f in formats))
317f7ab6
S
1625
1626 ctx = {
1627 'formats': formats,
1628 'incomplete_formats': incomplete_formats,
1629 }
1630
1631 formats_to_download = list(format_selector(ctx))
dd82ffea 1632 if not formats_to_download:
6febd1c1 1633 raise ExtractorError('requested format not available',
78a3a9f8 1634 expected=True)
dd82ffea
JMF
1635
1636 if download:
1637 if len(formats_to_download) > 1:
6febd1c1 1638 self.to_screen('[info] %s: downloading video in %s formats' % (info_dict['id'], len(formats_to_download)))
dd82ffea
JMF
1639 for format in formats_to_download:
1640 new_info = dict(info_dict)
1641 new_info.update(format)
1642 self.process_info(new_info)
1643 # We update the info dict with the best quality format (backwards compatibility)
1644 info_dict.update(formats_to_download[-1])
1645 return info_dict
1646
98c70d6f 1647 def process_subtitles(self, video_id, normal_subtitles, automatic_captions):
a504ced0 1648 """Select the requested subtitles and their format"""
98c70d6f
JMF
1649 available_subs = {}
1650 if normal_subtitles and self.params.get('writesubtitles'):
1651 available_subs.update(normal_subtitles)
1652 if automatic_captions and self.params.get('writeautomaticsub'):
1653 for lang, cap_info in automatic_captions.items():
360e1ca5
JMF
1654 if lang not in available_subs:
1655 available_subs[lang] = cap_info
1656
4d171848
JMF
1657 if (not self.params.get('writesubtitles') and not
1658 self.params.get('writeautomaticsub') or not
1659 available_subs):
1660 return None
a504ced0
JMF
1661
1662 if self.params.get('allsubtitles', False):
1663 requested_langs = available_subs.keys()
1664 else:
1665 if self.params.get('subtitleslangs', False):
1666 requested_langs = self.params.get('subtitleslangs')
1667 elif 'en' in available_subs:
1668 requested_langs = ['en']
1669 else:
1670 requested_langs = [list(available_subs.keys())[0]]
1671
1672 formats_query = self.params.get('subtitlesformat', 'best')
1673 formats_preference = formats_query.split('/') if formats_query else []
1674 subs = {}
1675 for lang in requested_langs:
1676 formats = available_subs.get(lang)
1677 if formats is None:
1678 self.report_warning('%s subtitles not available for %s' % (lang, video_id))
1679 continue
a504ced0
JMF
1680 for ext in formats_preference:
1681 if ext == 'best':
1682 f = formats[-1]
1683 break
1684 matches = list(filter(lambda f: f['ext'] == ext, formats))
1685 if matches:
1686 f = matches[-1]
1687 break
1688 else:
1689 f = formats[-1]
1690 self.report_warning(
1691 'No subtitle format found matching "%s" for language %s, '
1692 'using %s' % (formats_query, lang, f['ext']))
1693 subs[lang] = f
1694 return subs
1695
8222d8de
JMF
1696 def process_info(self, info_dict):
1697 """Process a single resolved IE result."""
1698
1699 assert info_dict.get('_type', 'video') == 'video'
fd288278
PH
1700
1701 max_downloads = self.params.get('max_downloads')
1702 if max_downloads is not None:
1703 if self._num_downloads >= int(max_downloads):
1704 raise MaxDownloadsReached()
8222d8de
JMF
1705
1706 info_dict['fulltitle'] = info_dict['title']
1707 if len(info_dict['title']) > 200:
6febd1c1 1708 info_dict['title'] = info_dict['title'][:197] + '...'
8222d8de 1709
11b85ce6 1710 if 'format' not in info_dict:
8222d8de
JMF
1711 info_dict['format'] = info_dict['ext']
1712
442c37b7 1713 reason = self._match_entry(info_dict, incomplete=False)
8222d8de 1714 if reason is not None:
6febd1c1 1715 self.to_screen('[download] ' + reason)
8222d8de
JMF
1716 return
1717
fd288278 1718 self._num_downloads += 1
8222d8de 1719
e72c7e41 1720 info_dict['_filename'] = filename = self.prepare_filename(info_dict)
8222d8de
JMF
1721
1722 # Forced printings
1723 if self.params.get('forcetitle', False):
0783b09b 1724 self.to_stdout(info_dict['fulltitle'])
8222d8de 1725 if self.params.get('forceid', False):
0783b09b 1726 self.to_stdout(info_dict['id'])
8222d8de 1727 if self.params.get('forceurl', False):
16ae61f6 1728 if info_dict.get('requested_formats') is not None:
1729 for f in info_dict['requested_formats']:
1730 self.to_stdout(f['url'] + f.get('play_path', ''))
1731 else:
1732 # For RTMP URLs, also include the playpath
1733 self.to_stdout(info_dict['url'] + info_dict.get('play_path', ''))
216d71d0 1734 if self.params.get('forcethumbnail', False) and info_dict.get('thumbnail') is not None:
0783b09b 1735 self.to_stdout(info_dict['thumbnail'])
216d71d0 1736 if self.params.get('forcedescription', False) and info_dict.get('description') is not None:
0783b09b 1737 self.to_stdout(info_dict['description'])
8222d8de 1738 if self.params.get('forcefilename', False) and filename is not None:
0783b09b 1739 self.to_stdout(filename)
525ef922
PH
1740 if self.params.get('forceduration', False) and info_dict.get('duration') is not None:
1741 self.to_stdout(formatSeconds(info_dict['duration']))
8222d8de 1742 if self.params.get('forceformat', False):
0783b09b 1743 self.to_stdout(info_dict['format'])
9d153818 1744 if self.params.get('forcejson', False):
0783b09b 1745 self.to_stdout(json.dumps(info_dict))
8222d8de
JMF
1746
1747 # Do nothing else if in simulate mode
1748 if self.params.get('simulate', False):
1749 return
1750
1751 if filename is None:
1752 return
1753
c5c9bf0c
S
1754 def ensure_dir_exists(path):
1755 try:
1756 dn = os.path.dirname(path)
1757 if dn and not os.path.exists(dn):
1758 os.makedirs(dn)
1759 return True
1760 except (OSError, IOError) as err:
1761 self.report_error('unable to create directory ' + error_to_compat_str(err))
1762 return False
1763
1764 if not ensure_dir_exists(sanitize_path(encodeFilename(filename))):
8222d8de
JMF
1765 return
1766
1767 if self.params.get('writedescription', False):
2699da80 1768 descfn = replace_extension(filename, 'description', info_dict.get('ext'))
7b6fefc9 1769 if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(descfn)):
6febd1c1 1770 self.to_screen('[info] Video description is already present')
f00fd51d
JMF
1771 elif info_dict.get('description') is None:
1772 self.report_warning('There\'s no description to write.')
7b6fefc9
PH
1773 else:
1774 try:
6febd1c1 1775 self.to_screen('[info] Writing video description to: ' + descfn)
7b6fefc9
PH
1776 with io.open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile:
1777 descfile.write(info_dict['description'])
7b6fefc9 1778 except (OSError, IOError):
6febd1c1 1779 self.report_error('Cannot write description file ' + descfn)
7b6fefc9 1780 return
8222d8de 1781
1fb07d10 1782 if self.params.get('writeannotations', False):
98727e12 1783 annofn = replace_extension(filename, 'annotations.xml', info_dict.get('ext'))
7b6fefc9 1784 if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(annofn)):
6febd1c1 1785 self.to_screen('[info] Video annotations are already present')
7b6fefc9
PH
1786 else:
1787 try:
6febd1c1 1788 self.to_screen('[info] Writing video annotations to: ' + annofn)
7b6fefc9
PH
1789 with io.open(encodeFilename(annofn), 'w', encoding='utf-8') as annofile:
1790 annofile.write(info_dict['annotations'])
1791 except (KeyError, TypeError):
6febd1c1 1792 self.report_warning('There are no annotations to write.')
7b6fefc9 1793 except (OSError, IOError):
6febd1c1 1794 self.report_error('Cannot write annotations file: ' + annofn)
7b6fefc9 1795 return
1fb07d10 1796
c4a91be7 1797 subtitles_are_requested = any([self.params.get('writesubtitles', False),
0b7f3118 1798 self.params.get('writeautomaticsub')])
c4a91be7 1799
c84dd8a9 1800 if subtitles_are_requested and info_dict.get('requested_subtitles'):
8222d8de
JMF
1801 # subtitles download errors are already managed as troubles in relevant IE
1802 # that way it will silently go on when used with unsupporting IE
c84dd8a9 1803 subtitles = info_dict['requested_subtitles']
0f2c0d33 1804 ie = self.get_info_extractor(info_dict['extractor_key'])
a504ced0
JMF
1805 for sub_lang, sub_info in subtitles.items():
1806 sub_format = sub_info['ext']
5ff1bc0c
RA
1807 sub_filename = subtitles_filename(filename, sub_lang, sub_format)
1808 if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(sub_filename)):
1809 self.to_screen('[info] Video subtitle %s.%s is already present' % (sub_lang, sub_format))
a504ced0 1810 else:
5ff1bc0c
RA
1811 self.to_screen('[info] Writing video subtitles to: ' + sub_filename)
1812 if sub_info.get('data') is not None:
1813 try:
1814 # Use newline='' to prevent conversion of newline characters
067aa17e 1815 # See https://github.com/ytdl-org/youtube-dl/issues/10268
5ff1bc0c
RA
1816 with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8', newline='') as subfile:
1817 subfile.write(sub_info['data'])
1818 except (OSError, IOError):
1819 self.report_error('Cannot write subtitles file ' + sub_filename)
1820 return
7b6fefc9 1821 else:
5ff1bc0c
RA
1822 try:
1823 sub_data = ie._request_webpage(
1824 sub_info['url'], info_dict['id'], note=False).read()
1825 with io.open(encodeFilename(sub_filename), 'wb') as subfile:
1826 subfile.write(sub_data)
1827 except (ExtractorError, IOError, OSError, ValueError) as err:
1828 self.report_warning('Unable to download subtitle for "%s": %s' %
1829 (sub_lang, error_to_compat_str(err)))
1830 continue
8222d8de 1831
8222d8de 1832 if self.params.get('writeinfojson', False):
b29e0000 1833 infofn = replace_extension(filename, 'info.json', info_dict.get('ext'))
7b6fefc9 1834 if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(infofn)):
6febd1c1 1835 self.to_screen('[info] Video description metadata is already present')
7b6fefc9 1836 else:
6febd1c1 1837 self.to_screen('[info] Writing video description metadata as JSON to: ' + infofn)
7b6fefc9 1838 try:
cb202fd2 1839 write_json_file(self.filter_requested_info(info_dict), infofn)
7b6fefc9 1840 except (OSError, IOError):
6febd1c1 1841 self.report_error('Cannot write metadata to JSON file ' + infofn)
7b6fefc9 1842 return
8222d8de 1843
ec82d85a 1844 self._write_thumbnails(info_dict, filename)
8222d8de
JMF
1845
1846 if not self.params.get('skip_download', False):
4340deca
P
1847 try:
1848 def dl(name, info):
a055469f 1849 fd = get_suitable_downloader(info, self.params)(self, self.params)
4340deca
P
1850 for ph in self._progress_hooks:
1851 fd.add_progress_hook(ph)
1852 if self.params.get('verbose'):
1853 self.to_stdout('[debug] Invoking downloader on %r' % info.get('url'))
1854 return fd.download(name, info)
ee69b99a 1855
4340deca
P
1856 if info_dict.get('requested_formats') is not None:
1857 downloaded = []
1858 success = True
d47aeb22 1859 merger = FFmpegMergerPP(self)
f740fae2 1860 if not merger.available:
4340deca
P
1861 postprocessors = []
1862 self.report_warning('You have requested multiple '
1863 'formats but ffmpeg or avconv are not installed.'
4a5a898a 1864 ' The formats won\'t be merged.')
6350728b 1865 else:
4340deca 1866 postprocessors = [merger]
81cd954a
S
1867
1868 def compatible_formats(formats):
1869 video, audio = formats
1870 # Check extension
5a19d231 1871 video_ext, audio_ext = video.get('ext'), audio.get('ext')
81cd954a
S
1872 if video_ext and audio_ext:
1873 COMPATIBLE_EXTS = (
b2758123 1874 ('mp3', 'mp4', 'm4a', 'm4p', 'm4b', 'm4r', 'm4v', 'ismv', 'isma'),
81cd954a
S
1875 ('webm')
1876 )
1877 for exts in COMPATIBLE_EXTS:
1878 if video_ext in exts and audio_ext in exts:
1879 return True
1880 # TODO: Check acodec/vcodec
1881 return False
1882
38c6902b
S
1883 filename_real_ext = os.path.splitext(filename)[1][1:]
1884 filename_wo_ext = (
1885 os.path.splitext(filename)[0]
1886 if filename_real_ext == info_dict['ext']
1887 else filename)
81cd954a 1888 requested_formats = info_dict['requested_formats']
c0dea0a7 1889 if self.params.get('merge_output_format') is None and not compatible_formats(requested_formats):
38c6902b 1890 info_dict['ext'] = 'mkv'
4a5a898a
S
1891 self.report_warning(
1892 'Requested formats are incompatible for merge and will be merged into mkv.')
38c6902b
S
1893 # Ensure filename always has a correct extension for successful merge
1894 filename = '%s.%s' % (filename_wo_ext, info_dict['ext'])
5b5fbc08
JMF
1895 if os.path.exists(encodeFilename(filename)):
1896 self.to_screen(
1897 '[download] %s has already been downloaded and '
1898 'merged' % filename)
1899 else:
81cd954a 1900 for f in requested_formats:
5b5fbc08
JMF
1901 new_info = dict(info_dict)
1902 new_info.update(f)
c5c9bf0c
S
1903 fname = prepend_extension(
1904 self.prepare_filename(new_info),
1905 'f%s' % f['format_id'], new_info['ext'])
1906 if not ensure_dir_exists(fname):
1907 return
5b5fbc08
JMF
1908 downloaded.append(fname)
1909 partial_success = dl(fname, new_info)
1910 success = success and partial_success
1911 info_dict['__postprocessors'] = postprocessors
1912 info_dict['__files_to_merge'] = downloaded
4340deca
P
1913 else:
1914 # Just a single file
1915 success = dl(filename, info_dict)
1916 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
7960b056 1917 self.report_error('unable to download video data: %s' % error_to_compat_str(err))
4340deca
P
1918 return
1919 except (OSError, IOError) as err:
1920 raise UnavailableVideoError(err)
1921 except (ContentTooShortError, ) as err:
1922 self.report_error('content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded))
1923 return
8222d8de 1924
e38cafe9 1925 if success and filename != '-':
6271f1ca 1926 # Fixup content
62cd676c
PH
1927 fixup_policy = self.params.get('fixup')
1928 if fixup_policy is None:
1929 fixup_policy = 'detect_or_warn'
1930
d1e4a464
S
1931 INSTALL_FFMPEG_MESSAGE = 'Install ffmpeg or avconv to fix this automatically.'
1932
6271f1ca
PH
1933 stretched_ratio = info_dict.get('stretched_ratio')
1934 if stretched_ratio is not None and stretched_ratio != 1:
6271f1ca
PH
1935 if fixup_policy == 'warn':
1936 self.report_warning('%s: Non-uniform pixel ratio (%s)' % (
1937 info_dict['id'], stretched_ratio))
1938 elif fixup_policy == 'detect_or_warn':
1939 stretched_pp = FFmpegFixupStretchedPP(self)
1940 if stretched_pp.available:
1941 info_dict.setdefault('__postprocessors', [])
1942 info_dict['__postprocessors'].append(stretched_pp)
1943 else:
1944 self.report_warning(
d1e4a464
S
1945 '%s: Non-uniform pixel ratio (%s). %s'
1946 % (info_dict['id'], stretched_ratio, INSTALL_FFMPEG_MESSAGE))
6271f1ca 1947 else:
62cd676c
PH
1948 assert fixup_policy in ('ignore', 'never')
1949
3089bc74
S
1950 if (info_dict.get('requested_formats') is None
1951 and info_dict.get('container') == 'm4a_dash'):
62cd676c 1952 if fixup_policy == 'warn':
d1e4a464
S
1953 self.report_warning(
1954 '%s: writing DASH m4a. '
1955 'Only some players support this container.'
1956 % info_dict['id'])
62cd676c
PH
1957 elif fixup_policy == 'detect_or_warn':
1958 fixup_pp = FFmpegFixupM4aPP(self)
1959 if fixup_pp.available:
1960 info_dict.setdefault('__postprocessors', [])
1961 info_dict['__postprocessors'].append(fixup_pp)
1962 else:
1963 self.report_warning(
d1e4a464
S
1964 '%s: writing DASH m4a. '
1965 'Only some players support this container. %s'
1966 % (info_dict['id'], INSTALL_FFMPEG_MESSAGE))
62cd676c
PH
1967 else:
1968 assert fixup_policy in ('ignore', 'never')
6271f1ca 1969
3089bc74
S
1970 if (info_dict.get('protocol') == 'm3u8_native'
1971 or info_dict.get('protocol') == 'm3u8'
1972 and self.params.get('hls_prefer_native')):
f17f8651 1973 if fixup_policy == 'warn':
a02682fd 1974 self.report_warning('%s: malformed AAC bitstream detected.' % (
f17f8651 1975 info_dict['id']))
1976 elif fixup_policy == 'detect_or_warn':
1977 fixup_pp = FFmpegFixupM3u8PP(self)
1978 if fixup_pp.available:
1979 info_dict.setdefault('__postprocessors', [])
1980 info_dict['__postprocessors'].append(fixup_pp)
1981 else:
1982 self.report_warning(
a02682fd 1983 '%s: malformed AAC bitstream detected. %s'
d1e4a464 1984 % (info_dict['id'], INSTALL_FFMPEG_MESSAGE))
f17f8651 1985 else:
1986 assert fixup_policy in ('ignore', 'never')
1987
8222d8de
JMF
1988 try:
1989 self.post_process(filename, info_dict)
1990 except (PostProcessingError) as err:
6febd1c1 1991 self.report_error('postprocessing: %s' % str(err))
8222d8de 1992 return
cd58dc3e 1993 self.record_download_archive(info_dict)
8222d8de
JMF
1994
1995 def download(self, url_list):
1996 """Download a given list of URLs."""
acd69589 1997 outtmpl = self.params.get('outtmpl', DEFAULT_OUTTMPL)
3089bc74
S
1998 if (len(url_list) > 1
1999 and outtmpl != '-'
2000 and '%' not in outtmpl
2001 and self.params.get('max_downloads') != 1):
acd69589 2002 raise SameFileError(outtmpl)
8222d8de
JMF
2003
2004 for url in url_list:
2005 try:
5f6a1245 2006 # It also downloads the videos
61aa5ba3
S
2007 res = self.extract_info(
2008 url, force_generic_extractor=self.params.get('force_generic_extractor', False))
8222d8de 2009 except UnavailableVideoError:
6febd1c1 2010 self.report_error('unable to download video')
8222d8de 2011 except MaxDownloadsReached:
6febd1c1 2012 self.to_screen('[info] Maximum number of downloaded files reached.')
8222d8de 2013 raise
63e0be34
PH
2014 else:
2015 if self.params.get('dump_single_json', False):
2016 self.to_stdout(json.dumps(res))
8222d8de
JMF
2017
2018 return self._download_retcode
2019
1dcc4c0c 2020 def download_with_info_file(self, info_filename):
31bd3925
JMF
2021 with contextlib.closing(fileinput.FileInput(
2022 [info_filename], mode='r',
2023 openhook=fileinput.hook_encoded('utf-8'))) as f:
2024 # FileInput doesn't have a read method, we can't call json.load
cb202fd2 2025 info = self.filter_requested_info(json.loads('\n'.join(f)))
d4943898
JMF
2026 try:
2027 self.process_ie_result(info, download=True)
2028 except DownloadError:
2029 webpage_url = info.get('webpage_url')
2030 if webpage_url is not None:
6febd1c1 2031 self.report_warning('The info failed to download, trying with "%s"' % webpage_url)
d4943898
JMF
2032 return self.download([webpage_url])
2033 else:
2034 raise
2035 return self._download_retcode
1dcc4c0c 2036
cb202fd2
S
2037 @staticmethod
2038 def filter_requested_info(info_dict):
2039 return dict(
2040 (k, v) for k, v in info_dict.items()
2041 if k not in ['requested_formats', 'requested_subtitles'])
2042
8222d8de
JMF
2043 def post_process(self, filename, ie_info):
2044 """Run all the postprocessors on the given file."""
2045 info = dict(ie_info)
2046 info['filepath'] = filename
6350728b
JMF
2047 pps_chain = []
2048 if ie_info.get('__postprocessors') is not None:
2049 pps_chain.extend(ie_info['__postprocessors'])
2050 pps_chain.extend(self._pps)
2051 for pp in pps_chain:
71646e46 2052 files_to_delete = []
8222d8de 2053 try:
592e97e8 2054 files_to_delete, info = pp.run(info)
8222d8de 2055 except PostProcessingError as e:
bbcbf4d4 2056 self.report_error(e.msg)
592e97e8
JMF
2057 if files_to_delete and not self.params.get('keepvideo', False):
2058 for old_filename in files_to_delete:
f3ff1a36 2059 self.to_screen('Deleting original file %s (pass -k to keep)' % old_filename)
592e97e8
JMF
2060 try:
2061 os.remove(encodeFilename(old_filename))
2062 except (IOError, OSError):
2063 self.report_warning('Unable to remove downloaded original file')
c1c9a79c 2064
5db07df6 2065 def _make_archive_id(self, info_dict):
e9fef7ee
S
2066 video_id = info_dict.get('id')
2067 if not video_id:
2068 return
5db07df6
PH
2069 # Future-proof against any change in case
2070 # and backwards compatibility with prior versions
e9fef7ee 2071 extractor = info_dict.get('extractor_key') or info_dict.get('ie_key') # key in a playlist
7012b23c 2072 if extractor is None:
1211bb6d
S
2073 url = str_or_none(info_dict.get('url'))
2074 if not url:
2075 return
e9fef7ee
S
2076 # Try to find matching extractor for the URL and take its ie_key
2077 for ie in self._ies:
1211bb6d 2078 if ie.suitable(url):
e9fef7ee
S
2079 extractor = ie.ie_key()
2080 break
2081 else:
2082 return
2083 return extractor.lower() + ' ' + video_id
5db07df6
PH
2084
2085 def in_download_archive(self, info_dict):
2086 fn = self.params.get('download_archive')
2087 if fn is None:
2088 return False
2089
2090 vid_id = self._make_archive_id(info_dict)
e9fef7ee 2091 if not vid_id:
7012b23c 2092 return False # Incomplete video information
5db07df6 2093
c1c9a79c
PH
2094 try:
2095 with locked_file(fn, 'r', encoding='utf-8') as archive_file:
2096 for line in archive_file:
2097 if line.strip() == vid_id:
2098 return True
2099 except IOError as ioe:
2100 if ioe.errno != errno.ENOENT:
2101 raise
2102 return False
2103
2104 def record_download_archive(self, info_dict):
2105 fn = self.params.get('download_archive')
2106 if fn is None:
2107 return
5db07df6
PH
2108 vid_id = self._make_archive_id(info_dict)
2109 assert vid_id
c1c9a79c 2110 with locked_file(fn, 'a', encoding='utf-8') as archive_file:
6febd1c1 2111 archive_file.write(vid_id + '\n')
dd82ffea 2112
8c51aa65 2113 @staticmethod
8abeeb94 2114 def format_resolution(format, default='unknown'):
fb04e403
PH
2115 if format.get('vcodec') == 'none':
2116 return 'audio only'
f49d89ee
PH
2117 if format.get('resolution') is not None:
2118 return format['resolution']
8c51aa65
JMF
2119 if format.get('height') is not None:
2120 if format.get('width') is not None:
6febd1c1 2121 res = '%sx%s' % (format['width'], format['height'])
8c51aa65 2122 else:
6febd1c1 2123 res = '%sp' % format['height']
f49d89ee 2124 elif format.get('width') is not None:
388ae76b 2125 res = '%dx?' % format['width']
8c51aa65 2126 else:
8abeeb94 2127 res = default
8c51aa65
JMF
2128 return res
2129
c57f7757
PH
2130 def _format_note(self, fdict):
2131 res = ''
2132 if fdict.get('ext') in ['f4f', 'f4m']:
2133 res += '(unsupported) '
32f90364
PH
2134 if fdict.get('language'):
2135 if res:
2136 res += ' '
9016d76f 2137 res += '[%s] ' % fdict['language']
c57f7757
PH
2138 if fdict.get('format_note') is not None:
2139 res += fdict['format_note'] + ' '
2140 if fdict.get('tbr') is not None:
2141 res += '%4dk ' % fdict['tbr']
2142 if fdict.get('container') is not None:
2143 if res:
2144 res += ', '
2145 res += '%s container' % fdict['container']
3089bc74
S
2146 if (fdict.get('vcodec') is not None
2147 and fdict.get('vcodec') != 'none'):
c57f7757
PH
2148 if res:
2149 res += ', '
2150 res += fdict['vcodec']
91c7271a 2151 if fdict.get('vbr') is not None:
c57f7757
PH
2152 res += '@'
2153 elif fdict.get('vbr') is not None and fdict.get('abr') is not None:
2154 res += 'video@'
2155 if fdict.get('vbr') is not None:
2156 res += '%4dk' % fdict['vbr']
fbb21cf5 2157 if fdict.get('fps') is not None:
5d583bdf
S
2158 if res:
2159 res += ', '
2160 res += '%sfps' % fdict['fps']
c57f7757
PH
2161 if fdict.get('acodec') is not None:
2162 if res:
2163 res += ', '
2164 if fdict['acodec'] == 'none':
2165 res += 'video only'
2166 else:
2167 res += '%-5s' % fdict['acodec']
2168 elif fdict.get('abr') is not None:
2169 if res:
2170 res += ', '
2171 res += 'audio'
2172 if fdict.get('abr') is not None:
2173 res += '@%3dk' % fdict['abr']
2174 if fdict.get('asr') is not None:
2175 res += ' (%5dHz)' % fdict['asr']
2176 if fdict.get('filesize') is not None:
2177 if res:
2178 res += ', '
2179 res += format_bytes(fdict['filesize'])
9732d77e
PH
2180 elif fdict.get('filesize_approx') is not None:
2181 if res:
2182 res += ', '
2183 res += '~' + format_bytes(fdict['filesize_approx'])
c57f7757 2184 return res
91c7271a 2185
c57f7757 2186 def list_formats(self, info_dict):
94badb25 2187 formats = info_dict.get('formats', [info_dict])
b81a359e
PH
2188 table = [
2189 [f['format_id'], f['ext'], self.format_resolution(f), self._format_note(f)]
2190 for f in formats
e65566a9 2191 if f.get('preference') is None or f['preference'] >= -1000]
94badb25 2192 if len(formats) > 1:
b81a359e 2193 table[-1][-1] += (' ' if table[-1][-1] else '') + '(best)'
57dd9a8f 2194
b81a359e 2195 header_line = ['format code', 'extension', 'resolution', 'note']
cfb56d1a 2196 self.to_screen(
b81a359e
PH
2197 '[info] Available formats for %s:\n%s' %
2198 (info_dict['id'], render_table(header_line, table)))
cfb56d1a
PH
2199
2200 def list_thumbnails(self, info_dict):
2201 thumbnails = info_dict.get('thumbnails')
2202 if not thumbnails:
b7b72db9 2203 self.to_screen('[info] No thumbnails present for %s' % info_dict['id'])
2204 return
cfb56d1a
PH
2205
2206 self.to_screen(
2207 '[info] Thumbnails for %s:' % info_dict['id'])
2208 self.to_screen(render_table(
2209 ['ID', 'width', 'height', 'URL'],
2210 [[t['id'], t.get('width', 'unknown'), t.get('height', 'unknown'), t['url']] for t in thumbnails]))
dca08720 2211
360e1ca5 2212 def list_subtitles(self, video_id, subtitles, name='subtitles'):
a504ced0 2213 if not subtitles:
360e1ca5 2214 self.to_screen('%s has no %s' % (video_id, name))
a504ced0 2215 return
a504ced0 2216 self.to_screen(
edab9dbf
JMF
2217 'Available %s for %s:' % (name, video_id))
2218 self.to_screen(render_table(
2219 ['Language', 'formats'],
2220 [[lang, ', '.join(f['ext'] for f in reversed(formats))]
2221 for lang, formats in subtitles.items()]))
a504ced0 2222
dca08720
PH
2223 def urlopen(self, req):
2224 """ Start an HTTP download """
82d8a8b6 2225 if isinstance(req, compat_basestring):
67dda517 2226 req = sanitized_Request(req)
19a41fc6 2227 return self._opener.open(req, timeout=self._socket_timeout)
dca08720
PH
2228
2229 def print_debug_header(self):
2230 if not self.params.get('verbose'):
2231 return
62fec3b2 2232
4192b51c 2233 if type('') is not compat_str:
067aa17e 2234 # Python 2.6 on SLES11 SP1 (https://github.com/ytdl-org/youtube-dl/issues/3326)
4192b51c
PH
2235 self.report_warning(
2236 'Your Python is broken! Update to a newer and supported version')
2237
c6afed48
PH
2238 stdout_encoding = getattr(
2239 sys.stdout, 'encoding', 'missing (%s)' % type(sys.stdout).__name__)
b0472057 2240 encoding_str = (
734f90bb
PH
2241 '[debug] Encodings: locale %s, fs %s, out %s, pref %s\n' % (
2242 locale.getpreferredencoding(),
2243 sys.getfilesystemencoding(),
c6afed48 2244 stdout_encoding,
b0472057 2245 self.get_encoding()))
4192b51c 2246 write_string(encoding_str, encoding=None)
734f90bb
PH
2247
2248 self._write_string('[debug] youtube-dl version ' + __version__ + '\n')
e0986e31
JMF
2249 if _LAZY_LOADER:
2250 self._write_string('[debug] Lazy loading extractors enabled' + '\n')
dca08720
PH
2251 try:
2252 sp = subprocess.Popen(
2253 ['git', 'rev-parse', '--short', 'HEAD'],
2254 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2255 cwd=os.path.dirname(os.path.abspath(__file__)))
2256 out, err = sp.communicate()
2257 out = out.decode().strip()
2258 if re.match('[0-9a-f]+', out):
734f90bb 2259 self._write_string('[debug] Git HEAD: ' + out + '\n')
70a1165b 2260 except Exception:
dca08720
PH
2261 try:
2262 sys.exc_clear()
70a1165b 2263 except Exception:
dca08720 2264 pass
b300cda4
S
2265
2266 def python_implementation():
2267 impl_name = platform.python_implementation()
2268 if impl_name == 'PyPy' and hasattr(sys, 'pypy_version_info'):
2269 return impl_name + ' version %d.%d.%d' % sys.pypy_version_info[:3]
2270 return impl_name
2271
2272 self._write_string('[debug] Python version %s (%s) - %s\n' % (
2273 platform.python_version(), python_implementation(),
2274 platform_name()))
d28b5171 2275
73fac4e9 2276 exe_versions = FFmpegPostProcessor.get_versions(self)
4c83c967 2277 exe_versions['rtmpdump'] = rtmpdump_version()
feee8d32 2278 exe_versions['phantomjs'] = PhantomJSwrapper._version()
d28b5171
PH
2279 exe_str = ', '.join(
2280 '%s %s' % (exe, v)
2281 for exe, v in sorted(exe_versions.items())
2282 if v
2283 )
2284 if not exe_str:
2285 exe_str = 'none'
2286 self._write_string('[debug] exe versions: %s\n' % exe_str)
dca08720
PH
2287
2288 proxy_map = {}
2289 for handler in self._opener.handlers:
2290 if hasattr(handler, 'proxies'):
2291 proxy_map.update(handler.proxies)
734f90bb 2292 self._write_string('[debug] Proxy map: ' + compat_str(proxy_map) + '\n')
dca08720 2293
58b1f00d
PH
2294 if self.params.get('call_home', False):
2295 ipaddr = self.urlopen('https://yt-dl.org/ip').read().decode('utf-8')
2296 self._write_string('[debug] Public IP address: %s\n' % ipaddr)
2297 latest_version = self.urlopen(
2298 'https://yt-dl.org/latest/version').read().decode('utf-8')
2299 if version_tuple(latest_version) > version_tuple(__version__):
2300 self.report_warning(
2301 'You are using an outdated version (newest version: %s)! '
2302 'See https://yt-dl.org/update if you need help updating.' %
2303 latest_version)
2304
e344693b 2305 def _setup_opener(self):
6ad14cab 2306 timeout_val = self.params.get('socket_timeout')
19a41fc6 2307 self._socket_timeout = 600 if timeout_val is None else float(timeout_val)
6ad14cab 2308
dca08720
PH
2309 opts_cookiefile = self.params.get('cookiefile')
2310 opts_proxy = self.params.get('proxy')
2311
2312 if opts_cookiefile is None:
2313 self.cookiejar = compat_cookiejar.CookieJar()
2314 else:
590bc6f6 2315 opts_cookiefile = expand_path(opts_cookiefile)
1bab3437 2316 self.cookiejar = YoutubeDLCookieJar(opts_cookiefile)
dca08720 2317 if os.access(opts_cookiefile, os.R_OK):
1d88b3e6 2318 self.cookiejar.load(ignore_discard=True, ignore_expires=True)
dca08720 2319
6a3f4c3f 2320 cookie_processor = YoutubeDLCookieProcessor(self.cookiejar)
dca08720
PH
2321 if opts_proxy is not None:
2322 if opts_proxy == '':
2323 proxies = {}
2324 else:
2325 proxies = {'http': opts_proxy, 'https': opts_proxy}
2326 else:
2327 proxies = compat_urllib_request.getproxies()
067aa17e 2328 # Set HTTPS proxy to HTTP one if given (https://github.com/ytdl-org/youtube-dl/issues/805)
dca08720
PH
2329 if 'http' in proxies and 'https' not in proxies:
2330 proxies['https'] = proxies['http']
91410c9b 2331 proxy_handler = PerRequestProxyHandler(proxies)
a0ddb8a2
PH
2332
2333 debuglevel = 1 if self.params.get('debug_printtraffic') else 0
be4a824d
PH
2334 https_handler = make_HTTPS_handler(self.params, debuglevel=debuglevel)
2335 ydlh = YoutubeDLHandler(self.params, debuglevel=debuglevel)
8b172c2e 2336 data_handler = compat_urllib_request_DataHandler()
6240b0a2
JMF
2337
2338 # When passing our own FileHandler instance, build_opener won't add the
2339 # default FileHandler and allows us to disable the file protocol, which
2340 # can be used for malicious purposes (see
067aa17e 2341 # https://github.com/ytdl-org/youtube-dl/issues/8227)
6240b0a2
JMF
2342 file_handler = compat_urllib_request.FileHandler()
2343
2344 def file_open(*args, **kwargs):
30e2f2d7 2345 raise compat_urllib_error.URLError('file:// scheme is explicitly disabled in youtube-dl for security reasons')
6240b0a2
JMF
2346 file_handler.file_open = file_open
2347
2348 opener = compat_urllib_request.build_opener(
2349 proxy_handler, https_handler, cookie_processor, ydlh, data_handler, file_handler)
2461f79d 2350
dca08720
PH
2351 # Delete the default user-agent header, which would otherwise apply in
2352 # cases where our custom HTTP handler doesn't come into play
067aa17e 2353 # (See https://github.com/ytdl-org/youtube-dl/issues/1309 for details)
dca08720
PH
2354 opener.addheaders = []
2355 self._opener = opener
62fec3b2
PH
2356
2357 def encode(self, s):
2358 if isinstance(s, bytes):
2359 return s # Already encoded
2360
2361 try:
2362 return s.encode(self.get_encoding())
2363 except UnicodeEncodeError as err:
2364 err.reason = err.reason + '. Check your system encoding configuration or use the --encoding option.'
2365 raise
2366
2367 def get_encoding(self):
2368 encoding = self.params.get('encoding')
2369 if encoding is None:
2370 encoding = preferredencoding()
2371 return encoding
ec82d85a
PH
2372
2373 def _write_thumbnails(self, info_dict, filename):
2374 if self.params.get('writethumbnail', False):
2375 thumbnails = info_dict.get('thumbnails')
2376 if thumbnails:
2377 thumbnails = [thumbnails[-1]]
2378 elif self.params.get('write_all_thumbnails', False):
2379 thumbnails = info_dict.get('thumbnails')
2380 else:
2381 return
2382
2383 if not thumbnails:
2384 # No thumbnails present, so return immediately
2385 return
2386
2387 for t in thumbnails:
2388 thumb_ext = determine_ext(t['url'], 'jpg')
2389 suffix = '_%s' % t['id'] if len(thumbnails) > 1 else ''
2390 thumb_display_id = '%s ' % t['id'] if len(thumbnails) > 1 else ''
82245a6d 2391 t['filename'] = thumb_filename = os.path.splitext(filename)[0] + suffix + '.' + thumb_ext
ec82d85a
PH
2392
2393 if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(thumb_filename)):
2394 self.to_screen('[%s] %s: Thumbnail %sis already present' %
2395 (info_dict['extractor'], info_dict['id'], thumb_display_id))
2396 else:
2397 self.to_screen('[%s] %s: Downloading thumbnail %s...' %
2398 (info_dict['extractor'], info_dict['id'], thumb_display_id))
2399 try:
2400 uf = self.urlopen(t['url'])
d3d89c32 2401 with open(encodeFilename(thumb_filename), 'wb') as thumbf:
ec82d85a
PH
2402 shutil.copyfileobj(uf, thumbf)
2403 self.to_screen('[%s] %s: Writing thumbnail %sto: %s' %
2404 (info_dict['extractor'], info_dict['id'], thumb_display_id, thumb_filename))
2405 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2406 self.report_warning('Unable to download thumbnail "%s": %s' %
9b9c5355 2407 (t['url'], error_to_compat_str(err)))