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