]> jfr.im git - yt-dlp.git/blame - youtube_dl/__init__.py
[downloader/common] Modernize
[yt-dlp.git] / youtube_dl / __init__.py
CommitLineData
235b3ba4
PH
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
235b3ba4 4__license__ = 'Public Domain'
235b3ba4 5
0d94f247 6import codecs
8f563f32 7import io
235b3ba4 8import os
0f818663 9import random
235b3ba4 10import sys
235b3ba4 11
c496ca96 12
2daabe49
PH
13from .options import (
14 parseOpts,
15)
8c25f81b 16from .compat import (
4644ac55 17 compat_expanduser,
e68301af 18 compat_getpass,
a4fd0415 19 compat_print,
8c25f81b
PH
20)
21from .utils import (
a4fd0415 22 DateRange,
acd69589 23 DEFAULT_OUTTMPL,
a4fd0415 24 decodeOption,
a4fd0415 25 DownloadError,
a4fd0415 26 MaxDownloadsReached,
a4fd0415 27 preferredencoding,
62e609ab 28 read_batch_urls,
a4fd0415 29 SameFileError,
e3946f98 30 setproctitle,
a4fd0415
PH
31 std_headers,
32 write_string,
a4fd0415 33)
d5ed35b6 34from .update import update_self
92a86f4c 35from .downloader import (
a4fd0415
PH
36 FileDownloader,
37)
0824c28c 38from .extractor import gen_extractors
8222d8de 39from .YoutubeDL import YoutubeDL
56327689 40from .postprocessor import (
0c14e2fb 41 AtomicParsleyPP,
149254d0 42 FFmpegAudioFixPP,
a4fd0415
PH
43 FFmpegMetadataPP,
44 FFmpegVideoConvertor,
45 FFmpegExtractAudioPP,
46 FFmpegEmbedSubtitlePP,
e63fc1be 47 XAttrMetadataPP,
a2360a4c 48 ExecAfterDownloadPP,
a4fd0415
PH
49)
50
235b3ba4 51
b8ad4f02 52def _real_main(argv=None):
0d94f247
PH
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
e3946f98
PH
58 setproctitle(u'youtube-dl')
59
b8ad4f02 60 parser, opts, args = parseOpts(argv)
59ae15a5 61
59ae15a5
PH
62 # Set user agent
63 if opts.user_agent is not None:
64 std_headers['User-Agent'] = opts.user_agent
1865ed31 65
28535652
BH
66 # Set referer
67 if opts.referer is not None:
68 std_headers['Referer'] = opts.referer
59ae15a5 69
410afb20
AA
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
59ae15a5
PH
80 # Dump user agent
81 if opts.dump_user_agent:
93eb15c5 82 compat_print(std_headers['User-Agent'])
59ae15a5
PH
83 sys.exit(0)
84
85 # Batch file verification
62e609ab 86 batch_urls = []
59ae15a5
PH
87 if opts.batchfile is not None:
88 try:
89 if opts.batchfile == '-':
90 batchfd = sys.stdin
91 else:
62e609ab
PH
92 batchfd = io.open(opts.batchfile, 'r', encoding='utf-8', errors='ignore')
93 batch_urls = read_batch_urls(batchfd)
05afc96b 94 if opts.verbose:
62e609ab 95 write_string(u'[debug] Batch file urls: ' + repr(batch_urls) + u'\n')
59ae15a5
PH
96 except IOError:
97 sys.exit(u'ERROR: batch file could not be read')
62e609ab 98 all_urls = batch_urls + args
59ae15a5 99 all_urls = [url.strip() for url in all_urls]
c774b3c6 100 _enc = preferredencoding()
41292a38 101 all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls]
59ae15a5 102
59ae15a5
PH
103 extractors = gen_extractors()
104
105 if opts.list_extractors:
7dba9cd0 106 for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
93eb15c5 107 compat_print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
1a2c3c0f 108 matchedUrls = [url for url in all_urls if ie.suitable(url)]
59ae15a5 109 for mu in matchedUrls:
93eb15c5 110 compat_print(u' ' + mu)
59ae15a5 111 sys.exit(0)
0f818663
PH
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)
15870e90
PH
117 if desc is False:
118 continue
0f818663 119 if hasattr(ie, 'SEARCH_KEY'):
53eb2176 120 _SEARCHES = (u'cute kittens', u'slithering pythons', u'falling cat', u'angry poodle', u'purple fish', u'running tortoise', u'sleeping bunny')
0f818663
PH
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
59ae15a5
PH
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:
67d28bff 131 parser.error(u'account username missing\n')
59ae15a5
PH
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:
e68301af 137 opts.password = compat_getpass(u'Type account password and press [Return]: ')
59ae15a5
PH
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
9e982f9e
JC
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
59ae15a5
PH
153 if opts.retries is not None:
154 try:
155 opts.retries = int(opts.retries)
dca08720 156 except (TypeError, ValueError):
59ae15a5
PH
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
a19fd00c
PH
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')
59ae15a5 167 if opts.extractaudio:
510e6f6d 168 if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
59ae15a5
PH
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')
7851b379 174 if opts.recodevideo is not None:
b7d73595 175 if opts.recodevideo not in ['mp4', 'flv', 'webm', 'ogg', 'mkv']:
7851b379 176 parser.error(u'invalid video recode format specified')
bd558525
JMF
177 if opts.date is not None:
178 date = DateRange.day(opts.date)
179 else:
180 date = DateRange(opts.dateafter, opts.datebefore)
59ae15a5 181
de3ef3ed
PH
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
0b7f3118
JMF
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
5cb9c312
PH
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)
0be41ec2
PH
193 if opts.outtmpl is not None:
194 opts.outtmpl = opts.outtmpl.decode(preferredencoding())
5cb9c312
PH
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')
acd69589 202 or DEFAULT_OUTTMPL)
dca02c80 203 if not os.path.splitext(outtmpl)[1] and opts.extractaudio:
b61067fa 204 parser.error(u'Cannot download a video and extract audio into the same'
dca02c80
JMF
205 u' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
206 u' template'.format(outtmpl))
29c7a63d 207
63e0be34 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
4644ac55 209 download_archive_fn = compat_expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive
525ef922 210
bdde425c 211 ydl_opts = {
59ae15a5
PH
212 'usenetrc': opts.usenetrc,
213 'username': opts.username,
214 'password': opts.password,
83317f69 215 'twofactor': opts.twofactor,
c6c19746 216 'videopassword': opts.videopassword,
525ef922 217 'quiet': (opts.quiet or any_printing),
ad8915b7 218 'no_warnings': opts.no_warnings,
59ae15a5
PH
219 'forceurl': opts.geturl,
220 'forcetitle': opts.gettitle,
1a2adf3f 221 'forceid': opts.getid,
59ae15a5
PH
222 'forcethumbnail': opts.getthumbnail,
223 'forcedescription': opts.getdescription,
525ef922 224 'forceduration': opts.getduration,
59ae15a5
PH
225 'forcefilename': opts.getfilename,
226 'forceformat': opts.getformat,
9d153818 227 'forcejson': opts.dumpjson,
63e0be34 228 'dump_single_json': opts.dump_single_json,
1bdeb7be
JMF
229 'simulate': opts.simulate or any_printing,
230 'skip_download': opts.skip_download,
59ae15a5
PH
231 'format': opts.format,
232 'format_limit': opts.format_limit,
233 'listformats': opts.listformats,
5cb9c312 234 'outtmpl': outtmpl,
213c31ae 235 'autonumber_size': opts.autonumber_size,
59ae15a5
PH
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,
5717d91a 245 'progress_with_newline': opts.progress_with_newline,
59ae15a5
PH
246 'playliststart': opts.playliststart,
247 'playlistend': opts.playlistend,
47192f92 248 'noplaylist': opts.noplaylist,
59ae15a5
PH
249 'logtostderr': opts.outtmpl == '-',
250 'consoletitle': opts.consoletitle,
251 'nopart': opts.nopart,
252 'updatetime': opts.updatetime,
253 'writedescription': opts.writedescription,
1fb07d10 254 'writeannotations': opts.writeannotations,
59ae15a5 255 'writeinfojson': opts.writeinfojson,
11d9224e 256 'writethumbnail': opts.writethumbnail,
59ae15a5 257 'writesubtitles': opts.writesubtitles,
b004821f 258 'writeautomaticsub': opts.writeautomaticsub,
ae608b80 259 'allsubtitles': opts.allsubtitles,
2a4093ea 260 'listsubtitles': opts.listsubtitles,
9e62bc44 261 'subtitlesformat': opts.subtitlesformat,
d6e203b3 262 'subtitleslangs': opts.subtitleslangs,
8271226a
PH
263 'matchtitle': decodeOption(opts.matchtitle),
264 'rejecttitle': decodeOption(opts.rejecttitle),
59ae15a5
PH
265 'max_downloads': opts.max_downloads,
266 'prefer_free_formats': opts.prefer_free_formats,
267 'verbose': opts.verbose,
855703e5 268 'dump_intermediate_pages': opts.dump_intermediate_pages,
d41e6efc 269 'write_pages': opts.write_pages,
8d5d3a5d 270 'test': opts.test,
7851b379 271 'keepvideo': opts.keepvideo,
9e982f9e 272 'min_filesize': opts.min_filesize,
bd558525 273 'max_filesize': opts.max_filesize,
5fe18bdb
PH
274 'min_views': opts.min_views,
275 'max_views': opts.max_views,
11d9224e 276 'daterange': date,
7f747732 277 'cachedir': opts.cachedir,
f8061589 278 'youtube_print_sig_code': opts.youtube_print_sig_code,
8dbe9899 279 'age_limit': opts.age_limit,
17093b83 280 'download_archive': download_archive_fn,
dca08720
PH
281 'cookiefile': opts.cookiefile,
282 'nocheckcertificate': opts.no_check_certificate,
7e8c0af0 283 'prefer_insecure': opts.prefer_insecure,
c2e52508 284 'proxy': opts.proxy,
6ad14cab 285 'socket_timeout': opts.socket_timeout,
0783b09b 286 'bidi_workaround': opts.bidi_workaround,
a0ddb8a2 287 'debug_printtraffic': opts.debug_printtraffic,
76b1bd67 288 'prefer_ffmpeg': opts.prefer_ffmpeg,
7b0817e8 289 'include_ads': opts.include_ads,
04b4d394 290 'default_search': opts.default_search,
4919603f 291 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
62fec3b2 292 'encoding': opts.encoding,
8d31fa3c 293 'exec_cmd': opts.exec_cmd,
057a5206 294 'extract_flat': opts.extract_flat,
bdde425c 295 }
59ae15a5 296
bdde425c 297 with YoutubeDL(ydl_opts) as ydl:
bdde425c
PH
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))
e63fc1be 308 if opts.xattrs:
309 ydl.add_post_processor(XAttrMetadataPP())
0c14e2fb 310 if opts.embedthumbnail:
784763c5 311 if not opts.addmetadata:
312 ydl.add_post_processor(FFmpegAudioFixPP())
0c14e2fb 313 ydl.add_post_processor(AtomicParsleyPP())
bdde425c 314
a7cacbca 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.
8d31fa3c
PH
318 if opts.exec_cmd:
319 ydl.add_post_processor(ExecAfterDownloadPP(
320 verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))
a7cacbca 321
bdde425c
PH
322 # Update version
323 if opts.update_self:
324 update_self(ydl.to_screen, opts.verbose)
325
052421ff
PH
326 # Remove cache dir
327 if opts.rm_cachedir:
a0e07d31 328 ydl.cache.remove()
052421ff 329
bdde425c 330 # Maybe do nothing
1dcc4c0c 331 if (len(all_urls) < 1) and (opts.load_info_filename is None):
052421ff 332 if not (opts.update_self or opts.rm_cachedir):
bdde425c
PH
333 parser.error(u'you must provide at least one URL')
334 else:
335 sys.exit()
59ae15a5 336
bdde425c 337 try:
1dcc4c0c
JMF
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)
bdde425c
PH
342 except MaxDownloadsReached:
343 ydl.to_screen(u'--max-download limit reached, aborting.')
344 retcode = 101
59ae15a5 345
59ae15a5 346 sys.exit(retcode)
235b3ba4 347
a27b9e8b 348
b8ad4f02 349def main(argv=None):
59ae15a5 350 try:
b8ad4f02 351 _real_main(argv)
59ae15a5
PH
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')