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