Merge remote-tracking branch 'alab1001101/master'
[youtube-dl] / youtube_dl / __init__.py
index 13cf77896e52cf0a546d3663b44fb5d635923997..f7a49e13a8f85a1c4fcb5030078e554416f1df85 100644 (file)
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from __future__ import with_statement
+
 __authors__  = (
        'Ricardo Garcia Gonzalez',
        'Danny Colligan',
@@ -19,7 +21,7 @@ __authors__  = (
        )
 
 __license__ = 'Public Domain'
-__version__ = '2012.02.27'
+__version__ = '2012.11.28'
 
 UPDATE_URL = 'https://raw.github.com/rg3/youtube-dl/master/youtube-dl'
 UPDATE_URL_VERSION = 'https://raw.github.com/rg3/youtube-dl/master/LATEST_VERSION'
@@ -46,7 +48,7 @@ from PostProcessor import *
 def updateSelf(downloader, filename):
        ''' Update the program file with the latest version from the repository '''
        # Note: downloader only used for options
-       
+
        if not os.access(filename, os.W_OK):
                sys.exit('ERROR: no write permissions on %s' % filename)
 
@@ -64,7 +66,7 @@ def updateSelf(downloader, filename):
                directory = os.path.dirname(exe)
                if not os.access(directory, os.W_OK):
                        sys.exit('ERROR: no write permissions on %s' % directory)
-                       
+
                try:
                        urlh = urllib2.urlopen(UPDATE_URL_EXE)
                        newcontent = urlh.read()
@@ -73,20 +75,18 @@ def updateSelf(downloader, filename):
                                outf.write(newcontent)
                except (IOError, OSError), err:
                        sys.exit('ERROR: unable to download latest version')
-                       
+
                try:
                        bat = os.path.join(directory, 'youtube-dl-updater.bat')
                        b = open(bat, 'w')
-                       
-                       print >> b, """
+                       b.write("""
 echo Updating youtube-dl...
 ping 127.0.0.1 -n 5 -w 1000 > NUL
 move /Y "%s.new" "%s"
 del "%s"
-                       """ %(exe, exe, bat)
-                       
+                       \n""" %(exe, exe, bat))
                        b.close()
-                       
+
                        os.startfile(bat)
                except (IOError, OSError), err:
                        sys.exit('ERROR: unable to overwrite current version')
@@ -186,16 +186,23 @@ def parseOpts():
        general.add_option('-r', '--rate-limit',
                        dest='ratelimit', metavar='LIMIT', help='download rate limit (e.g. 50k or 44.6m)')
        general.add_option('-R', '--retries',
-                       dest='retries', metavar='RETRIES', help='number of retries (default is 10)', default=10)
+                       dest='retries', metavar='RETRIES', help='number of retries (default is %default)', default=10)
+       general.add_option('--buffer-size',
+                       dest='buffersize', metavar='SIZE', help='size of download buffer (e.g. 1024 or 16k) (default is %default)', default="1024")
+       general.add_option('--no-resize-buffer',
+                       action='store_true', dest='noresizebuffer',
+                       help='do not automatically adjust the buffer size. By default, the buffer size is automatically resized from an initial value of SIZE.', default=False)
        general.add_option('--dump-user-agent',
                        action='store_true', dest='dump_user_agent',
                        help='display the current browser identification', default=False)
+       general.add_option('--user-agent',
+                       dest='user_agent', help='specify a custom user agent', metavar='UA')
        general.add_option('--list-extractors',
                        action='store_true', dest='list_extractors',
                        help='List all supported extractors and the URLs they would handle', default=False)
 
        selection.add_option('--playlist-start',
-                       dest='playliststart', metavar='NUMBER', help='playlist video to start at (default is 1)', default=1)
+                       dest='playliststart', metavar='NUMBER', help='playlist video to start at (default is %default)', default=1)
        selection.add_option('--playlist-end',
                        dest='playlistend', metavar='NUMBER', help='playlist video to end at (default is last)', default=-1)
        selection.add_option('--match-title', dest='matchtitle', metavar='REGEX',help='download only matching titles (regex or caseless sub-string)')
@@ -261,13 +268,18 @@ def parseOpts():
 
        filesystem.add_option('-t', '--title',
                        action='store_true', dest='usetitle', help='use title in file name', default=False)
+       filesystem.add_option('--id',
+                       action='store_true', dest='useid', help='use video ID in file name', default=False)
        filesystem.add_option('-l', '--literal',
-                       action='store_true', dest='useliteral', help='use literal title in file name', default=False)
+                       action='store_true', dest='usetitle', help='[deprecated] alias of --title', default=False)
        filesystem.add_option('-A', '--auto-number',
                        action='store_true', dest='autonumber',
                        help='number downloaded files starting from 00000', default=False)
        filesystem.add_option('-o', '--output',
-                       dest='outtmpl', metavar='TEMPLATE', help='output filename template. Use %(stitle)s to get the title, %(uploader)s for the uploader name, %(autonumber)s to get an automatically incremented number, %(ext)s for the filename extension, %(upload_date)s for the upload date (YYYYMMDD), and %% for a literal percent. Use - to output to stdout.')
+                       dest='outtmpl', metavar='TEMPLATE', help='output filename template. Use %(title)s to get the title, %(uploader)s for the uploader name, %(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.')
+       filesystem.add_option('--restrict-filenames',
+                       action='store_true', dest='restrictfilenames',
+                       help='Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames', default=False)
        filesystem.add_option('-a', '--batch-file',
                        dest='batchfile', metavar='FILE', help='file containing URLs to download (\'-\' for stdin)')
        filesystem.add_option('-w', '--no-overwrites',
@@ -292,12 +304,12 @@ def parseOpts():
                        help='write video metadata to a .info.json file', default=False)
 
 
-       postproc.add_option('--extract-audio', action='store_true', dest='extractaudio', default=False,
+       postproc.add_option('-x', '--extract-audio', action='store_true', dest='extractaudio', default=False,
                        help='convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)')
        postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best',
                        help='"best", "aac", "vorbis", "mp3", "m4a", or "wav"; best by default')
-       postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='128K',
-                       help='ffmpeg/avconv audio bitrate specification, 128k by default')
+       postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='5',
+                       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)')
        postproc.add_option('-k', '--keep-video', action='store_true', dest='keepvideo', default=False,
                        help='keeps the video file on disk after the post-processing; the video is erased by default')
 
@@ -326,6 +338,7 @@ def gen_extractors():
        """
        return [
                YoutubePlaylistIE(),
+               YoutubeChannelIE(),
                YoutubeUserIE(),
                YoutubeSearchIE(),
                YoutubeIE(),
@@ -351,8 +364,10 @@ def gen_extractors():
                MixcloudIE(),
                StanfordOpenClassroomIE(),
                MTVIE(),
-                ArteTvIE(),
-
+               YoukuIE(),
+               XNXXIE(),
+               GooglePlusIE(),
+               ArteTvIE(),
                GenericIE()
        ]
 
@@ -369,6 +384,9 @@ def _real_main():
                                jar.load()
                except (IOError, OSError), err:
                        sys.exit(u'ERROR: unable to open cookie file')
+       # Set user agent
+       if opts.user_agent is not None:
+               std_headers['User-Agent'] = opts.user_agent
 
        # Dump user agent
        if opts.dump_user_agent:
@@ -414,10 +432,10 @@ def _real_main():
                parser.error(u'using .netrc conflicts with giving username/password')
        if opts.password is not None and opts.username is None:
                parser.error(u'account username missing')
-       if opts.outtmpl is not None and (opts.useliteral or opts.usetitle or opts.autonumber):
-               parser.error(u'using output template conflicts with using title, literal title or auto number')
-       if opts.usetitle and opts.useliteral:
-               parser.error(u'using title conflicts with using literal title')
+       if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
+               parser.error(u'using output template conflicts with using title, video ID or auto number')
+       if opts.usetitle and opts.useid:
+               parser.error(u'using title conflicts with using video ID')
        if opts.username is not None and opts.password is None:
                opts.password = getpass.getpass(u'Type account password and press return:')
        if opts.ratelimit is not None:
@@ -430,6 +448,11 @@ def _real_main():
                        opts.retries = long(opts.retries)
                except (TypeError, ValueError), err:
                        parser.error(u'invalid retry count specified')
+       if opts.buffersize is not None:
+               numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
+               if numeric_buffersize is None:
+                       parser.error(u'invalid buffer size specified')
+               opts.buffersize = numeric_buffersize
        try:
                opts.playliststart = int(opts.playliststart)
                if opts.playliststart <= 0:
@@ -445,6 +468,10 @@ def _real_main():
        if opts.extractaudio:
                if opts.audioformat not in ['best', 'aac', 'mp3', 'vorbis', 'm4a', 'wav']:
                        parser.error(u'invalid audio format specified')
+       if opts.audioquality:
+               opts.audioquality = opts.audioquality.strip('k').strip('K')
+               if not opts.audioquality.isdigit():
+                       parser.error(u'invalid audio quality specified')
 
        # File downloader
        fd = FileDownloader({
@@ -464,19 +491,20 @@ def _real_main():
                'format_limit': opts.format_limit,
                'listformats': opts.listformats,
                'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding()))
-                       or (opts.format == '-1' and opts.usetitle and u'%(stitle)s-%(id)s-%(format)s.%(ext)s')
-                       or (opts.format == '-1' and opts.useliteral and u'%(title)s-%(id)s-%(format)s.%(ext)s')
+                       or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
                        or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
-                       or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(stitle)s-%(id)s.%(ext)s')
-                       or (opts.useliteral and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
-                       or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s')
-                       or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s')
+                       or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
+                       or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
+                       or (opts.useid and u'%(id)s.%(ext)s')
                        or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
                        or u'%(id)s.%(ext)s'),
+               'restrictfilenames': opts.restrictfilenames,
                'ignoreerrors': opts.ignoreerrors,
                'ratelimit': opts.ratelimit,
                'nooverwrites': opts.nooverwrites,
                'retries': opts.retries,
+               'buffersize': opts.buffersize,
+               'noresizebuffer': opts.noresizebuffer,
                'continuedl': opts.continue_dl,
                'noprogress': opts.noprogress,
                'playliststart': opts.playliststart,
@@ -516,7 +544,7 @@ def _real_main():
                        parser.error(u'you must provide at least one URL')
                else:
                        sys.exit()
-       
+
        try:
                retcode = fd.download(all_urls)
        except MaxDownloadsReached: