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