[util] Move compatibility functions out of util
[youtube-dl] / youtube_dl / __init__.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 __license__ = 'Public Domain'
5
6 import codecs
7 import io
8 import os
9 import random
10 import sys
11
12
13 from .options import (
14     parseOpts,
15 )
16 from .compat import (
17     compat_expanduser,
18     compat_getpass,
19     compat_print,
20 )
21 from .utils import (
22     DateRange,
23     DEFAULT_OUTTMPL,
24     decodeOption,
25     DownloadError,
26     MaxDownloadsReached,
27     preferredencoding,
28     read_batch_urls,
29     SameFileError,
30     setproctitle,
31     std_headers,
32     write_string,
33 )
34 from .update import update_self
35 from .downloader import (
36     FileDownloader,
37 )
38 from .extractor import gen_extractors
39 from .YoutubeDL import YoutubeDL
40 from .postprocessor import (
41     AtomicParsleyPP,
42     FFmpegAudioFixPP,
43     FFmpegMetadataPP,
44     FFmpegVideoConvertor,
45     FFmpegExtractAudioPP,
46     FFmpegEmbedSubtitlePP,
47     XAttrMetadataPP,
48     ExecAfterDownloadPP,
49 )
50
51
52 def _real_main(argv=None):
53     # Compatibility fixes for Windows
54     if sys.platform == 'win32':
55         # https://github.com/rg3/youtube-dl/issues/820
56         codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
57
58     setproctitle(u'youtube-dl')
59
60     parser, opts, args = parseOpts(argv)
61
62     # Set user agent
63     if opts.user_agent is not None:
64         std_headers['User-Agent'] = opts.user_agent
65
66     # Set referer
67     if opts.referer is not None:
68         std_headers['Referer'] = opts.referer
69
70     # Custom HTTP headers
71     if opts.headers is not None:
72         for h in opts.headers:
73             if h.find(':', 1) < 0:
74                 parser.error(u'wrong header formatting, it should be key:value, not "%s"'%h)
75             key, value = h.split(':', 2)
76             if opts.verbose:
77                 write_string(u'[debug] Adding header from command line option %s:%s\n'%(key, value))
78             std_headers[key] = value
79
80     # Dump user agent
81     if opts.dump_user_agent:
82         compat_print(std_headers['User-Agent'])
83         sys.exit(0)
84
85     # Batch file verification
86     batch_urls = []
87     if opts.batchfile is not None:
88         try:
89             if opts.batchfile == '-':
90                 batchfd = sys.stdin
91             else:
92                 batchfd = io.open(opts.batchfile, 'r', encoding='utf-8', errors='ignore')
93             batch_urls = read_batch_urls(batchfd)
94             if opts.verbose:
95                 write_string(u'[debug] Batch file urls: ' + repr(batch_urls) + u'\n')
96         except IOError:
97             sys.exit(u'ERROR: batch file could not be read')
98     all_urls = batch_urls + args
99     all_urls = [url.strip() for url in all_urls]
100     _enc = preferredencoding()
101     all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls]
102
103     extractors = gen_extractors()
104
105     if opts.list_extractors:
106         for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
107             compat_print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
108             matchedUrls = [url for url in all_urls if ie.suitable(url)]
109             for mu in matchedUrls:
110                 compat_print(u'  ' + mu)
111         sys.exit(0)
112     if opts.list_extractor_descriptions:
113         for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
114             if not ie._WORKING:
115                 continue
116             desc = getattr(ie, 'IE_DESC', ie.IE_NAME)
117             if desc is False:
118                 continue
119             if hasattr(ie, 'SEARCH_KEY'):
120                 _SEARCHES = (u'cute kittens', u'slithering pythons', u'falling cat', u'angry poodle', u'purple fish', u'running tortoise', u'sleeping bunny')
121                 _COUNTS = (u'', u'5', u'10', u'all')
122                 desc += u' (Example: "%s%s:%s" )' % (ie.SEARCH_KEY, random.choice(_COUNTS), random.choice(_SEARCHES))
123             compat_print(desc)
124         sys.exit(0)
125
126
127     # Conflicting, missing and erroneous options
128     if opts.usenetrc and (opts.username is not None or opts.password is not None):
129         parser.error(u'using .netrc conflicts with giving username/password')
130     if opts.password is not None and opts.username is None:
131         parser.error(u'account username missing\n')
132     if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
133         parser.error(u'using output template conflicts with using title, video ID or auto number')
134     if opts.usetitle and opts.useid:
135         parser.error(u'using title conflicts with using video ID')
136     if opts.username is not None and opts.password is None:
137         opts.password = compat_getpass(u'Type account password and press [Return]: ')
138     if opts.ratelimit is not None:
139         numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
140         if numeric_limit is None:
141             parser.error(u'invalid rate limit specified')
142         opts.ratelimit = numeric_limit
143     if opts.min_filesize is not None:
144         numeric_limit = FileDownloader.parse_bytes(opts.min_filesize)
145         if numeric_limit is None:
146             parser.error(u'invalid min_filesize specified')
147         opts.min_filesize = numeric_limit
148     if opts.max_filesize is not None:
149         numeric_limit = FileDownloader.parse_bytes(opts.max_filesize)
150         if numeric_limit is None:
151             parser.error(u'invalid max_filesize specified')
152         opts.max_filesize = numeric_limit
153     if opts.retries is not None:
154         try:
155             opts.retries = int(opts.retries)
156         except (TypeError, ValueError):
157             parser.error(u'invalid retry count specified')
158     if opts.buffersize is not None:
159         numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
160         if numeric_buffersize is None:
161             parser.error(u'invalid buffer size specified')
162         opts.buffersize = numeric_buffersize
163     if opts.playliststart <= 0:
164         raise ValueError(u'Playlist start must be positive')
165     if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart:
166         raise ValueError(u'Playlist end must be greater than playlist start')
167     if opts.extractaudio:
168         if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
169             parser.error(u'invalid audio format specified')
170     if opts.audioquality:
171         opts.audioquality = opts.audioquality.strip('k').strip('K')
172         if not opts.audioquality.isdigit():
173             parser.error(u'invalid audio quality specified')
174     if opts.recodevideo is not None:
175         if opts.recodevideo not in ['mp4', 'flv', 'webm', 'ogg', 'mkv']:
176             parser.error(u'invalid video recode format specified')
177     if opts.date is not None:
178         date = DateRange.day(opts.date)
179     else:
180         date = DateRange(opts.dateafter, opts.datebefore)
181
182     # Do not download videos when there are audio-only formats
183     if opts.extractaudio and not opts.keepvideo and opts.format is None:
184         opts.format = 'bestaudio/best'
185
186     # --all-sub automatically sets --write-sub if --write-auto-sub is not given
187     # this was the old behaviour if only --all-sub was given.
188     if opts.allsubtitles and (opts.writeautomaticsub == False):
189         opts.writesubtitles = True
190
191     if sys.version_info < (3,):
192         # In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
193         if opts.outtmpl is not None:
194             opts.outtmpl = opts.outtmpl.decode(preferredencoding())
195     outtmpl =((opts.outtmpl is not None and opts.outtmpl)
196             or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
197             or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
198             or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
199             or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
200             or (opts.useid and u'%(id)s.%(ext)s')
201             or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
202             or DEFAULT_OUTTMPL)
203     if not os.path.splitext(outtmpl)[1] and opts.extractaudio:
204         parser.error(u'Cannot download a video and extract audio into the same'
205                      u' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
206                      u' template'.format(outtmpl))
207
208     any_printing = opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_json
209     download_archive_fn = compat_expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive
210
211     ydl_opts = {
212         'usenetrc': opts.usenetrc,
213         'username': opts.username,
214         'password': opts.password,
215         'twofactor': opts.twofactor,
216         'videopassword': opts.videopassword,
217         'quiet': (opts.quiet or any_printing),
218         'no_warnings': opts.no_warnings,
219         'forceurl': opts.geturl,
220         'forcetitle': opts.gettitle,
221         'forceid': opts.getid,
222         'forcethumbnail': opts.getthumbnail,
223         'forcedescription': opts.getdescription,
224         'forceduration': opts.getduration,
225         'forcefilename': opts.getfilename,
226         'forceformat': opts.getformat,
227         'forcejson': opts.dumpjson,
228         'dump_single_json': opts.dump_single_json,
229         'simulate': opts.simulate or any_printing,
230         'skip_download': opts.skip_download,
231         'format': opts.format,
232         'format_limit': opts.format_limit,
233         'listformats': opts.listformats,
234         'outtmpl': outtmpl,
235         'autonumber_size': opts.autonumber_size,
236         'restrictfilenames': opts.restrictfilenames,
237         'ignoreerrors': opts.ignoreerrors,
238         'ratelimit': opts.ratelimit,
239         'nooverwrites': opts.nooverwrites,
240         'retries': opts.retries,
241         'buffersize': opts.buffersize,
242         'noresizebuffer': opts.noresizebuffer,
243         'continuedl': opts.continue_dl,
244         'noprogress': opts.noprogress,
245         'progress_with_newline': opts.progress_with_newline,
246         'playliststart': opts.playliststart,
247         'playlistend': opts.playlistend,
248         'noplaylist': opts.noplaylist,
249         'logtostderr': opts.outtmpl == '-',
250         'consoletitle': opts.consoletitle,
251         'nopart': opts.nopart,
252         'updatetime': opts.updatetime,
253         'writedescription': opts.writedescription,
254         'writeannotations': opts.writeannotations,
255         'writeinfojson': opts.writeinfojson,
256         'writethumbnail': opts.writethumbnail,
257         'writesubtitles': opts.writesubtitles,
258         'writeautomaticsub': opts.writeautomaticsub,
259         'allsubtitles': opts.allsubtitles,
260         'listsubtitles': opts.listsubtitles,
261         'subtitlesformat': opts.subtitlesformat,
262         'subtitleslangs': opts.subtitleslangs,
263         'matchtitle': decodeOption(opts.matchtitle),
264         'rejecttitle': decodeOption(opts.rejecttitle),
265         'max_downloads': opts.max_downloads,
266         'prefer_free_formats': opts.prefer_free_formats,
267         'verbose': opts.verbose,
268         'dump_intermediate_pages': opts.dump_intermediate_pages,
269         'write_pages': opts.write_pages,
270         'test': opts.test,
271         'keepvideo': opts.keepvideo,
272         'min_filesize': opts.min_filesize,
273         'max_filesize': opts.max_filesize,
274         'min_views': opts.min_views,
275         'max_views': opts.max_views,
276         'daterange': date,
277         'cachedir': opts.cachedir,
278         'youtube_print_sig_code': opts.youtube_print_sig_code,
279         'age_limit': opts.age_limit,
280         'download_archive': download_archive_fn,
281         'cookiefile': opts.cookiefile,
282         'nocheckcertificate': opts.no_check_certificate,
283         'prefer_insecure': opts.prefer_insecure,
284         'proxy': opts.proxy,
285         'socket_timeout': opts.socket_timeout,
286         'bidi_workaround': opts.bidi_workaround,
287         'debug_printtraffic': opts.debug_printtraffic,
288         'prefer_ffmpeg': opts.prefer_ffmpeg,
289         'include_ads': opts.include_ads,
290         'default_search': opts.default_search,
291         'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
292         'encoding': opts.encoding,
293         'exec_cmd': opts.exec_cmd,
294         'extract_flat': opts.extract_flat,
295     }
296
297     with YoutubeDL(ydl_opts) as ydl:
298         # PostProcessors
299         # Add the metadata pp first, the other pps will copy it
300         if opts.addmetadata:
301             ydl.add_post_processor(FFmpegMetadataPP())
302         if opts.extractaudio:
303             ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))
304         if opts.recodevideo:
305             ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))
306         if opts.embedsubtitles:
307             ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))
308         if opts.xattrs:
309             ydl.add_post_processor(XAttrMetadataPP())
310         if opts.embedthumbnail:
311             if not opts.addmetadata:
312                 ydl.add_post_processor(FFmpegAudioFixPP())
313             ydl.add_post_processor(AtomicParsleyPP())
314
315
316         # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.
317         # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.
318         if opts.exec_cmd:
319             ydl.add_post_processor(ExecAfterDownloadPP(
320                 verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))
321
322         # Update version
323         if opts.update_self:
324             update_self(ydl.to_screen, opts.verbose)
325
326         # Remove cache dir
327         if opts.rm_cachedir:
328             ydl.cache.remove()
329
330         # Maybe do nothing
331         if (len(all_urls) < 1) and (opts.load_info_filename is None):
332             if not (opts.update_self or opts.rm_cachedir):
333                 parser.error(u'you must provide at least one URL')
334             else:
335                 sys.exit()
336
337         try:
338             if opts.load_info_filename is not None:
339                 retcode = ydl.download_with_info_file(opts.load_info_filename)
340             else:
341                 retcode = ydl.download(all_urls)
342         except MaxDownloadsReached:
343             ydl.to_screen(u'--max-download limit reached, aborting.')
344             retcode = 101
345
346     sys.exit(retcode)
347
348
349 def main(argv=None):
350     try:
351         _real_main(argv)
352     except DownloadError:
353         sys.exit(1)
354     except SameFileError:
355         sys.exit(u'ERROR: fixed output name but more than one file to download')
356     except KeyboardInterrupt:
357         sys.exit(u'\nERROR: Interrupted by user')