2 # -*- coding: utf-8 -*-
4 from __future__ import with_statement
5 from __future__ import absolute_import
8 'Ricardo Garcia Gonzalez',
16 'Philipp Hagemeister',
26 __license__ = 'Public Domain'
39 from .version import __version__
40 from .FileDownloader import *
41 from .InfoExtractors import *
42 from .PostProcessor import *
44 def updateSelf(downloader, filename):
45 """Update the program file with the latest version from the repository"""
47 # TODO: at least, check https certificates
49 from zipimport import zipimporter
51 API_URL = "https://api.github.com/repos/rg3/youtube-dl/downloads"
52 BIN_URL = "https://github.com/downloads/rg3/youtube-dl/youtube-dl"
53 EXE_URL = "https://github.com/downloads/rg3/youtube-dl/youtube-dl.exe"
55 if hasattr(sys, "frozen"): # PY2EXE
56 if not os.access(filename, os.W_OK):
57 sys.exit('ERROR: no write permissions on %s' % filename)
59 downloader.to_screen(u'Updating to latest version...')
61 urla = compat_urllib_request.urlopen(API_URL)
62 download = filter(lambda x: x["name"] == "youtube-dl.exe", json.loads(urla.read()))
64 downloader.to_screen(u'ERROR: can\'t find the current version. Please try again later.')
66 newversion = download[0]["description"].strip()
67 if newversion == __version__:
68 downloader.to_screen(u'youtube-dl is up-to-date (' + __version__ + ')')
72 exe = os.path.abspath(filename)
73 directory = os.path.dirname(exe)
74 if not os.access(directory, os.W_OK):
75 sys.exit('ERROR: no write permissions on %s' % directory)
78 urlh = compat_urllib_request.urlopen(EXE_URL)
79 newcontent = urlh.read()
81 with open(exe + '.new', 'wb') as outf:
82 outf.write(newcontent)
83 except (IOError, OSError) as err:
84 sys.exit('ERROR: unable to download latest version')
87 bat = os.path.join(directory, 'youtube-dl-updater.bat')
90 echo Updating youtube-dl...
91 ping 127.0.0.1 -n 5 -w 1000 > NUL
94 \n""" %(exe, exe, bat))
98 except (IOError, OSError) as err:
99 sys.exit('ERROR: unable to overwrite current version')
101 elif isinstance(globals().get('__loader__'), zipimporter): # UNIX ZIP
102 if not os.access(filename, os.W_OK):
103 sys.exit('ERROR: no write permissions on %s' % filename)
105 downloader.to_screen(u'Updating to latest version...')
107 urla = compat_urllib_request.urlopen(API_URL)
108 download = [x for x in json.loads(urla.read().decode('utf8')) if x["name"] == "youtube-dl"]
110 downloader.to_screen(u'ERROR: can\'t find the current version. Please try again later.')
112 newversion = download[0]["description"].strip()
113 if newversion == __version__:
114 downloader.to_screen(u'youtube-dl is up-to-date (' + __version__ + ')')
119 urlh = compat_urllib_request.urlopen(BIN_URL)
120 newcontent = urlh.read()
122 except (IOError, OSError) as err:
123 sys.exit('ERROR: unable to download latest version')
126 with open(filename, 'wb') as outf:
127 outf.write(newcontent)
128 except (IOError, OSError) as err:
129 sys.exit('ERROR: unable to overwrite current version')
132 downloader.to_screen(u'It looks like you installed youtube-dl with pip or setup.py. Please use that to update.')
135 downloader.to_screen(u'Updated youtube-dl. Restart youtube-dl to use the new version.')
138 def _readOptions(filename_bytes):
140 optionf = open(filename_bytes)
142 return [] # silently skip if file is not present
146 res += shlex.split(l, comments=True)
151 def _format_option_string(option):
152 ''' ('-o', '--option') -> -o, --format METAVAR'''
156 if option._short_opts:
157 opts.append(option._short_opts[0])
158 if option._long_opts:
159 opts.append(option._long_opts[0])
163 if option.takes_value(): opts.append(' %s' % option.metavar)
167 def _find_term_columns():
168 columns = os.environ.get('COLUMNS', None)
173 sp = subprocess.Popen(['stty', 'size'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
174 out,err = sp.communicate()
175 return int(out.split()[1])
181 max_help_position = 80
183 # No need to wrap help messages if we're on a wide console
184 columns = _find_term_columns()
185 if columns: max_width = columns
187 fmt = optparse.IndentedHelpFormatter(width=max_width, max_help_position=max_help_position)
188 fmt.format_option_strings = _format_option_string
191 'version' : __version__,
193 'usage' : '%prog [options] url [url...]',
194 'conflict_handler' : 'resolve',
197 parser = optparse.OptionParser(**kw)
200 general = optparse.OptionGroup(parser, 'General Options')
201 selection = optparse.OptionGroup(parser, 'Video Selection')
202 authentication = optparse.OptionGroup(parser, 'Authentication Options')
203 video_format = optparse.OptionGroup(parser, 'Video Format Options')
204 postproc = optparse.OptionGroup(parser, 'Post-processing Options')
205 filesystem = optparse.OptionGroup(parser, 'Filesystem Options')
206 verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')
208 general.add_option('-h', '--help',
209 action='help', help='print this help text and exit')
210 general.add_option('-v', '--version',
211 action='version', help='print program version and exit')
212 general.add_option('-U', '--update',
213 action='store_true', dest='update_self', help='update this program to latest version')
214 general.add_option('-i', '--ignore-errors',
215 action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
216 general.add_option('-r', '--rate-limit',
217 dest='ratelimit', metavar='LIMIT', help='download rate limit (e.g. 50k or 44.6m)')
218 general.add_option('-R', '--retries',
219 dest='retries', metavar='RETRIES', help='number of retries (default is %default)', default=10)
220 general.add_option('--buffer-size',
221 dest='buffersize', metavar='SIZE', help='size of download buffer (e.g. 1024 or 16k) (default is %default)', default="1024")
222 general.add_option('--no-resize-buffer',
223 action='store_true', dest='noresizebuffer',
224 help='do not automatically adjust the buffer size. By default, the buffer size is automatically resized from an initial value of SIZE.', default=False)
225 general.add_option('--dump-user-agent',
226 action='store_true', dest='dump_user_agent',
227 help='display the current browser identification', default=False)
228 general.add_option('--user-agent',
229 dest='user_agent', help='specify a custom user agent', metavar='UA')
230 general.add_option('--list-extractors',
231 action='store_true', dest='list_extractors',
232 help='List all supported extractors and the URLs they would handle', default=False)
233 general.add_option('--test', action='store_true', dest='test', default=False, help=optparse.SUPPRESS_HELP)
235 selection.add_option('--playlist-start',
236 dest='playliststart', metavar='NUMBER', help='playlist video to start at (default is %default)', default=1)
237 selection.add_option('--playlist-end',
238 dest='playlistend', metavar='NUMBER', help='playlist video to end at (default is last)', default=-1)
239 selection.add_option('--match-title', dest='matchtitle', metavar='REGEX',help='download only matching titles (regex or caseless sub-string)')
240 selection.add_option('--reject-title', dest='rejecttitle', metavar='REGEX',help='skip download for matching titles (regex or caseless sub-string)')
241 selection.add_option('--max-downloads', metavar='NUMBER', dest='max_downloads', help='Abort after downloading NUMBER files', default=None)
243 authentication.add_option('-u', '--username',
244 dest='username', metavar='USERNAME', help='account username')
245 authentication.add_option('-p', '--password',
246 dest='password', metavar='PASSWORD', help='account password')
247 authentication.add_option('-n', '--netrc',
248 action='store_true', dest='usenetrc', help='use .netrc authentication data', default=False)
251 video_format.add_option('-f', '--format',
252 action='store', dest='format', metavar='FORMAT', help='video format code')
253 video_format.add_option('--all-formats',
254 action='store_const', dest='format', help='download all available video formats', const='all')
255 video_format.add_option('--prefer-free-formats',
256 action='store_true', dest='prefer_free_formats', default=False, help='prefer free video formats unless a specific one is requested')
257 video_format.add_option('--max-quality',
258 action='store', dest='format_limit', metavar='FORMAT', help='highest quality format to download')
259 video_format.add_option('-F', '--list-formats',
260 action='store_true', dest='listformats', help='list all available formats (currently youtube only)')
261 video_format.add_option('--write-srt',
262 action='store_true', dest='writesubtitles',
263 help='write video closed captions to a .srt file (currently youtube only)', default=False)
264 video_format.add_option('--srt-lang',
265 action='store', dest='subtitleslang', metavar='LANG',
266 help='language of the closed captions to download (optional) use IETF language tags like \'en\'')
269 verbosity.add_option('-q', '--quiet',
270 action='store_true', dest='quiet', help='activates quiet mode', default=False)
271 verbosity.add_option('-s', '--simulate',
272 action='store_true', dest='simulate', help='do not download the video and do not write anything to disk', default=False)
273 verbosity.add_option('--skip-download',
274 action='store_true', dest='skip_download', help='do not download the video', default=False)
275 verbosity.add_option('-g', '--get-url',
276 action='store_true', dest='geturl', help='simulate, quiet but print URL', default=False)
277 verbosity.add_option('-e', '--get-title',
278 action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
279 verbosity.add_option('--get-thumbnail',
280 action='store_true', dest='getthumbnail',
281 help='simulate, quiet but print thumbnail URL', default=False)
282 verbosity.add_option('--get-description',
283 action='store_true', dest='getdescription',
284 help='simulate, quiet but print video description', default=False)
285 verbosity.add_option('--get-filename',
286 action='store_true', dest='getfilename',
287 help='simulate, quiet but print output filename', default=False)
288 verbosity.add_option('--get-format',
289 action='store_true', dest='getformat',
290 help='simulate, quiet but print output format', default=False)
291 verbosity.add_option('--no-progress',
292 action='store_true', dest='noprogress', help='do not print progress bar', default=False)
293 verbosity.add_option('--console-title',
294 action='store_true', dest='consoletitle',
295 help='display progress in console titlebar', default=False)
296 verbosity.add_option('-v', '--verbose',
297 action='store_true', dest='verbose', help='print various debugging information', default=False)
300 filesystem.add_option('-t', '--title',
301 action='store_true', dest='usetitle', help='use title in file name', default=False)
302 filesystem.add_option('--id',
303 action='store_true', dest='useid', help='use video ID in file name', default=False)
304 filesystem.add_option('-l', '--literal',
305 action='store_true', dest='usetitle', help='[deprecated] alias of --title', default=False)
306 filesystem.add_option('-A', '--auto-number',
307 action='store_true', dest='autonumber',
308 help='number downloaded files starting from 00000', default=False)
309 filesystem.add_option('-o', '--output',
310 dest='outtmpl', metavar='TEMPLATE', help='output filename template. Use %(title)s to get the title, %(uploader)s for the uploader name, %(uploader_id)s for the uploader nickname if different, %(autonumber)s to get an automatically incremented number, %(ext)s for the filename extension, %(upload_date)s for the upload date (YYYYMMDD), %(extractor)s for the provider (youtube, metacafe, etc), %(id)s for the video id and %% for a literal percent. Use - to output to stdout. Can also be used to download to a different directory, for example with -o \'/my/downloads/%(uploader)s/%(title)s-%(id)s.%(ext)s\' .')
311 filesystem.add_option('--restrict-filenames',
312 action='store_true', dest='restrictfilenames',
313 help='Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames', default=False)
314 filesystem.add_option('-a', '--batch-file',
315 dest='batchfile', metavar='FILE', help='file containing URLs to download (\'-\' for stdin)')
316 filesystem.add_option('-w', '--no-overwrites',
317 action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
318 filesystem.add_option('-c', '--continue',
319 action='store_true', dest='continue_dl', help='resume partially downloaded files', default=True)
320 filesystem.add_option('--no-continue',
321 action='store_false', dest='continue_dl',
322 help='do not resume partially downloaded files (restart from beginning)')
323 filesystem.add_option('--cookies',
324 dest='cookiefile', metavar='FILE', help='file to read cookies from and dump cookie jar in')
325 filesystem.add_option('--no-part',
326 action='store_true', dest='nopart', help='do not use .part files', default=False)
327 filesystem.add_option('--no-mtime',
328 action='store_false', dest='updatetime',
329 help='do not use the Last-modified header to set the file modification time', default=True)
330 filesystem.add_option('--write-description',
331 action='store_true', dest='writedescription',
332 help='write video description to a .description file', default=False)
333 filesystem.add_option('--write-info-json',
334 action='store_true', dest='writeinfojson',
335 help='write video metadata to a .info.json file', default=False)
338 postproc.add_option('-x', '--extract-audio', action='store_true', dest='extractaudio', default=False,
339 help='convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)')
340 postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best',
341 help='"best", "aac", "vorbis", "mp3", "m4a", or "wav"; best by default')
342 postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='5',
343 help='ffmpeg/avconv audio quality specification, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default 5)')
344 postproc.add_option('-k', '--keep-video', action='store_true', dest='keepvideo', default=False,
345 help='keeps the video file on disk after the post-processing; the video is erased by default')
346 postproc.add_option('--no-post-overwrites', action='store_true', dest='nopostoverwrites', default=False,
347 help='do not overwrite post-processed files; the post-processed files are overwritten by default')
350 parser.add_option_group(general)
351 parser.add_option_group(selection)
352 parser.add_option_group(filesystem)
353 parser.add_option_group(verbosity)
354 parser.add_option_group(video_format)
355 parser.add_option_group(authentication)
356 parser.add_option_group(postproc)
358 xdg_config_home = os.environ.get('XDG_CONFIG_HOME')
360 userConf = os.path.join(xdg_config_home, 'youtube-dl.conf')
362 userConf = os.path.join(os.path.expanduser('~'), '.config', 'youtube-dl.conf')
363 argv = _readOptions('/etc/youtube-dl.conf') + _readOptions(userConf) + sys.argv[1:]
364 opts, args = parser.parse_args(argv)
366 return parser, opts, args
368 def gen_extractors():
369 """ Return a list of an instance of every supported extractor.
370 The order does matter; the first extractor matched is the one handling the URL.
397 StanfordOpenClassroomIE(),
410 parser, opts, args = parseOpts()
412 # Open appropriate CookieJar
413 if opts.cookiefile is None:
414 jar = compat_cookiejar.CookieJar()
417 jar = compat_cookiejar.MozillaCookieJar(opts.cookiefile)
418 if os.path.isfile(opts.cookiefile) and os.access(opts.cookiefile, os.R_OK):
420 except (IOError, OSError) as err:
421 sys.exit(u'ERROR: unable to open cookie file')
423 if opts.user_agent is not None:
424 std_headers['User-Agent'] = opts.user_agent
427 if opts.dump_user_agent:
428 print(std_headers['User-Agent'])
431 # Batch file verification
433 if opts.batchfile is not None:
435 if opts.batchfile == '-':
438 batchfd = open(opts.batchfile, 'r')
439 batchurls = batchfd.readlines()
440 batchurls = [x.strip() for x in batchurls]
441 batchurls = [x for x in batchurls if len(x) > 0 and not re.search(r'^[#/;]', x)]
443 sys.exit(u'ERROR: batch file could not be read')
444 all_urls = batchurls + args
445 all_urls = [url.strip() for url in all_urls]
447 # General configuration
448 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
449 proxy_handler = compat_urllib_request.ProxyHandler()
450 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
451 compat_urllib_request.install_opener(opener)
452 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
454 extractors = gen_extractors()
456 if opts.list_extractors:
457 for ie in extractors:
458 print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
459 matchedUrls = [url for url in all_urls if ie.suitable(url)]
460 all_urls = [url for url in all_urls if url not in matchedUrls]
461 for mu in matchedUrls:
465 # Conflicting, missing and erroneous options
466 if opts.usenetrc and (opts.username is not None or opts.password is not None):
467 parser.error(u'using .netrc conflicts with giving username/password')
468 if opts.password is not None and opts.username is None:
469 parser.error(u'account username missing')
470 if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
471 parser.error(u'using output template conflicts with using title, video ID or auto number')
472 if opts.usetitle and opts.useid:
473 parser.error(u'using title conflicts with using video ID')
474 if opts.username is not None and opts.password is None:
475 opts.password = getpass.getpass(u'Type account password and press return:')
476 if opts.ratelimit is not None:
477 numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
478 if numeric_limit is None:
479 parser.error(u'invalid rate limit specified')
480 opts.ratelimit = numeric_limit
481 if opts.retries is not None:
483 opts.retries = int(opts.retries)
484 except (TypeError, ValueError) as err:
485 parser.error(u'invalid retry count specified')
486 if opts.buffersize is not None:
487 numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
488 if numeric_buffersize is None:
489 parser.error(u'invalid buffer size specified')
490 opts.buffersize = numeric_buffersize
492 opts.playliststart = int(opts.playliststart)
493 if opts.playliststart <= 0:
494 raise ValueError(u'Playlist start must be positive')
495 except (TypeError, ValueError) as err:
496 parser.error(u'invalid playlist start number specified')
498 opts.playlistend = int(opts.playlistend)
499 if opts.playlistend != -1 and (opts.playlistend <= 0 or opts.playlistend < opts.playliststart):
500 raise ValueError(u'Playlist end must be greater than playlist start')
501 except (TypeError, ValueError) as err:
502 parser.error(u'invalid playlist end number specified')
503 if opts.extractaudio:
504 if opts.audioformat not in ['best', 'aac', 'mp3', 'vorbis', 'm4a', 'wav']:
505 parser.error(u'invalid audio format specified')
506 if opts.audioquality:
507 opts.audioquality = opts.audioquality.strip('k').strip('K')
508 if not opts.audioquality.isdigit():
509 parser.error(u'invalid audio quality specified')
511 if sys.version_info < (3,):
512 # In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
513 if opts.outtmpl is not None:
514 opts.outtmpl = opts.outtmpl.decode(preferredencoding())
515 outtmpl =((opts.outtmpl is not None and opts.outtmpl)
516 or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
517 or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
518 or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
519 or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
520 or (opts.useid and u'%(id)s.%(ext)s')
521 or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
522 or u'%(id)s.%(ext)s')
524 fd = FileDownloader({
525 'usenetrc': opts.usenetrc,
526 'username': opts.username,
527 'password': opts.password,
528 'quiet': (opts.quiet or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat),
529 'forceurl': opts.geturl,
530 'forcetitle': opts.gettitle,
531 'forcethumbnail': opts.getthumbnail,
532 'forcedescription': opts.getdescription,
533 'forcefilename': opts.getfilename,
534 'forceformat': opts.getformat,
535 'simulate': opts.simulate,
536 'skip_download': (opts.skip_download or opts.simulate or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat),
537 'format': opts.format,
538 'format_limit': opts.format_limit,
539 'listformats': opts.listformats,
541 'restrictfilenames': opts.restrictfilenames,
542 'ignoreerrors': opts.ignoreerrors,
543 'ratelimit': opts.ratelimit,
544 'nooverwrites': opts.nooverwrites,
545 'retries': opts.retries,
546 'buffersize': opts.buffersize,
547 'noresizebuffer': opts.noresizebuffer,
548 'continuedl': opts.continue_dl,
549 'noprogress': opts.noprogress,
550 'playliststart': opts.playliststart,
551 'playlistend': opts.playlistend,
552 'logtostderr': opts.outtmpl == '-',
553 'consoletitle': opts.consoletitle,
554 'nopart': opts.nopart,
555 'updatetime': opts.updatetime,
556 'writedescription': opts.writedescription,
557 'writeinfojson': opts.writeinfojson,
558 'writesubtitles': opts.writesubtitles,
559 'subtitleslang': opts.subtitleslang,
560 'matchtitle': opts.matchtitle,
561 'rejecttitle': opts.rejecttitle,
562 'max_downloads': opts.max_downloads,
563 'prefer_free_formats': opts.prefer_free_formats,
564 'verbose': opts.verbose,
569 fd.to_screen(u'[debug] Proxy map: ' + str(proxy_handler.proxies))
571 for extractor in extractors:
572 fd.add_info_extractor(extractor)
575 if opts.extractaudio:
576 fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, keepvideo=opts.keepvideo, nopostoverwrites=opts.nopostoverwrites))
580 updateSelf(fd, sys.argv[0])
583 if len(all_urls) < 1:
584 if not opts.update_self:
585 parser.error(u'you must provide at least one URL')
590 retcode = fd.download(all_urls)
591 except MaxDownloadsReached:
592 fd.to_screen(u'--max-download limit reached, aborting.')
595 # Dump cookie jar if requested
596 if opts.cookiefile is not None:
599 except (IOError, OSError) as err:
600 sys.exit(u'ERROR: unable to save cookie jar')
607 except DownloadError:
609 except SameFileError:
610 sys.exit(u'ERROR: fixed output name but more than one file to download')
611 except KeyboardInterrupt:
612 sys.exit(u'\nERROR: Interrupted by user')