Do not update if already up-to-date (Closes #166)
[youtube-dl.git] / youtube-dl
index 2fc88b0..34e86e0 100755 (executable)
@@ -15,7 +15,7 @@ __author__  = (
        )
 
 __license__ = 'Public Domain'
-__version__ = '2011.09.15'
+__version__ = '2011.09.18'
 
 UPDATE_URL = 'https://raw.github.com/rg3/youtube-dl/master/youtube-dl'
 
@@ -710,6 +710,8 @@ class FileDownloader(object):
                        print info_dict['description'].encode(preferredencoding(), 'xmlcharrefreplace')
                if self.params.get('forcefilename', False) and filename is not None:
                        print filename.encode(preferredencoding(), 'xmlcharrefreplace')
+               if self.params.get('forceformat', False):
+                       print info_dict['format'].encode(preferredencoding(), 'xmlcharrefreplace')
 
                # Do nothing else if in simulate mode
                if self.params.get('simulate', False):
@@ -773,8 +775,7 @@ class FileDownloader(object):
 
                if not self.params.get('skip_download', False):
                        try:
-                               success,add_data = self._do_download(filename, info_dict['url'].encode('utf-8'), info_dict.get('player_url', None))
-                               info_dict.update(add_data)
+                               success = self._do_download(filename, info_dict)
                        except (OSError, IOError), err:
                                raise UnavailableVideoError
                        except (urllib2.URLError, httplib.HTTPException, socket.error), err:
@@ -863,7 +864,10 @@ class FileDownloader(object):
                        self.trouble(u'\nERROR: rtmpdump exited with code %d' % retval)
                        return False
 
-       def _do_download(self, filename, url, player_url):
+       def _do_download(self, filename, info_dict):
+               url = info_dict['url']
+               player_url = info_dict.get('player_url', None)
+
                # Check file already present
                if self.params.get('continuedl', False) and os.path.isfile(filename) and not self.params.get('nopart', False):
                        self.report_file_already_downloaded(filename)
@@ -875,7 +879,6 @@ class FileDownloader(object):
 
                tmpfilename = self.temp_name(filename)
                stream = None
-               open_mode = 'wb'
 
                # Do not include the Accept-Encoding header
                headers = {'Youtubedl-no-compression': 'True'}
@@ -888,11 +891,14 @@ class FileDownloader(object):
                else:
                        resume_len = 0
 
-               # Request parameters in case of being able to resume
-               if self.params.get('continuedl', False) and resume_len != 0:
-                       self.report_resuming_byte(resume_len)
-                       request.add_header('Range', 'bytes=%d-' % resume_len)
-                       open_mode = 'ab'
+               open_mode = 'wb'
+               if resume_len != 0:
+                       if self.params.get('continuedl', False):
+                               self.report_resuming_byte(resume_len)
+                               request.add_header('Range','bytes=%d-' % resume_len)
+                               open_mode = 'ab'
+                       else:
+                               resume_len = 0
 
                count = 0
                retries = self.params.get('retries', 0)
@@ -994,11 +1000,10 @@ class FileDownloader(object):
                self.try_rename(tmpfilename, filename)
 
                # Update file modification time
-               filetime = None
                if self.params.get('updatetime', True):
-                       filetime = self.try_utime(filename, data.info().get('last-modified', None))
+                       info_dict['filetime'] = self.try_utime(filename, data.info().get('last-modified', None))
 
-               return True, {'filetime': filetime}
+               return True
 
 
 class InfoExtractor(object):
@@ -1318,18 +1323,24 @@ class YoutubeIE(InfoExtractor):
                        if len(existing_formats) == 0:
                                self._downloader.trouble(u'ERROR: no known formats available for video')
                                return
-                       if req_format is None:
+                       if req_format is None or req_format == 'best':
                                video_url_list = [(existing_formats[0], url_map[existing_formats[0]])] # Best quality
                        elif req_format == 'worst':
                                video_url_list = [(existing_formats[len(existing_formats)-1], url_map[existing_formats[len(existing_formats)-1]])] # worst quality
-                       elif req_format == '-1':
+                       elif req_format in ('-1', 'all'):
                                video_url_list = [(f, url_map[f]) for f in existing_formats] # All formats
                        else:
-                               # Specific format
-                               if req_format not in url_map:
+                               # Specific formats. We pick the first in a slash-delimeted sequence.
+                               # For example, if '1/2/3/4' is requested and '2' and '4' are available, we pick '2'.
+                               req_formats = req_format.split('/')
+                               video_url_list = None
+                               for rf in req_formats:
+                                       if rf in url_map:
+                                               video_url_list = [(rf, url_map[rf])]
+                                               break
+                               if video_url_list is None:
                                        self._downloader.trouble(u'ERROR: requested format not available')
                                        return
-                               video_url_list = [(req_format, url_map[req_format])] # Specific format
                else:
                        self._downloader.trouble(u'ERROR: no conn or url_encoded_fmt_stream_map information found in video info')
                        return
@@ -3291,11 +3302,13 @@ class PostProcessor(object):
 
 class FFmpegExtractAudioPP(PostProcessor):
 
-       def __init__(self, downloader=None, preferredcodec=None):
+       def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, keepvideo=False):
                PostProcessor.__init__(self, downloader)
                if preferredcodec is None:
                        preferredcodec = 'best'
                self._preferredcodec = preferredcodec
+               self._preferredquality = preferredquality
+               self._keepvideo = keepvideo
 
        @staticmethod
        def get_audio_codec(path):
@@ -3344,12 +3357,16 @@ class FFmpegExtractAudioPP(PostProcessor):
                                # MP3 otherwise.
                                acodec = 'libmp3lame'
                                extension = 'mp3'
-                               more_opts = ['-ab', '128k']
+                               more_opts = []
+                               if self._preferredquality is not None:
+                                       more_opts += ['-ab', self._preferredquality]
                else:
                        # We convert the audio (lossy)
                        acodec = {'mp3': 'libmp3lame', 'aac': 'aac'}[self._preferredcodec]
                        extension = self._preferredcodec
-                       more_opts = ['-ab', '128k']
+                       more_opts = []
+                       if self._preferredquality is not None:
+                               more_opts += ['-ab', self._preferredquality]
                        if self._preferredcodec == 'aac':
                                more_opts += ['-f', 'adts']
 
@@ -3369,11 +3386,12 @@ class FFmpegExtractAudioPP(PostProcessor):
                        except:
                                self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file')
 
-               try:
-                       os.remove(path)
-               except (IOError, OSError):
-                       self._downloader.to_stderr(u'WARNING: Unable to remove downloaded video file')
-                       return None
+               if not self._keepvideo:
+                       try:
+                               os.remove(path)
+                       except (IOError, OSError):
+                               self._downloader.to_stderr(u'WARNING: Unable to remove downloaded video file')
+                               return None
 
                information['filepath'] = new_path
                return information
@@ -3391,6 +3409,11 @@ def updateSelf(downloader, filename):
                try:
                        urlh = urllib.urlopen(UPDATE_URL)
                        newcontent = urlh.read()
+                       
+                       vmatch = re.search("__version__ = '([^']+)'", newcontent)
+                       if vmatch is not None and vmatch.group(1) == __version__:
+                               downloader.to_screen('youtube-dl is up-to-date (' + __version__ + ')')
+                               return
                finally:
                        urlh.close()
        except (IOError, OSError), err:
@@ -3503,7 +3526,7 @@ def parseOpts():
        video_format.add_option('-f', '--format',
                        action='store', dest='format', metavar='FORMAT', help='video format code')
        video_format.add_option('--all-formats',
-                       action='store_const', dest='format', help='download all available video formats', const='-1')
+                       action='store_const', dest='format', help='download all available video formats', const='all')
        video_format.add_option('--max-quality',
                        action='store', dest='format_limit', metavar='FORMAT', help='highest quality format to download')
 
@@ -3527,6 +3550,9 @@ def parseOpts():
        verbosity.add_option('--get-filename',
                        action='store_true', dest='getfilename',
                        help='simulate, quiet but print output filename', default=False)
+       verbosity.add_option('--get-format',
+                       action='store_true', dest='getformat',
+                       help='simulate, quiet but print output format', default=False)
        verbosity.add_option('--no-progress',
                        action='store_true', dest='noprogress', help='do not print progress bar', default=False)
        verbosity.add_option('--console-title',
@@ -3549,6 +3575,9 @@ def parseOpts():
                        action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
        filesystem.add_option('-c', '--continue',
                        action='store_true', dest='continue_dl', help='resume partially downloaded files', default=False)
+       filesystem.add_option('--no-continue',
+                       action='store_false', dest='continue_dl',
+                       help='do not resume partially downloaded files (restart from beginning)')
        filesystem.add_option('--cookies',
                        dest='cookiefile', metavar='FILE', help='file to dump cookie jar to')
        filesystem.add_option('--no-part',
@@ -3568,6 +3597,10 @@ def parseOpts():
                        help='convert video files to audio-only files (requires ffmpeg and ffprobe)')
        postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best',
                        help='"best", "aac" or "mp3"; best by default')
+       postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='128K',
+                       help='ffmpeg audio bitrate specification, 128k by default')
+       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')
 
 
        parser.add_option_group(general)
@@ -3705,14 +3738,15 @@ def main():
                'usenetrc': opts.usenetrc,
                'username': opts.username,
                'password': opts.password,
-               'quiet': (opts.quiet or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename),
+               'quiet': (opts.quiet or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat),
                'forceurl': opts.geturl,
                'forcetitle': opts.gettitle,
                'forcethumbnail': opts.getthumbnail,
                'forcedescription': opts.getdescription,
                'forcefilename': opts.getfilename,
+               'forceformat': opts.getformat,
                'simulate': opts.simulate,
-               'skip_download': (opts.skip_download or opts.simulate or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename),
+               '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),
                'format': opts.format,
                'format_limit': opts.format_limit,
                'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding()))
@@ -3747,7 +3781,7 @@ def main():
 
        # PostProcessors
        if opts.extractaudio:
-               fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat))
+               fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, keepvideo=opts.keepvideo))
 
        # Update version
        if opts.update_self: