Merge branch 'master' into vimeo
[youtube-dl.git] / youtube-dl
index 9e9be67..240b2bc 100755 (executable)
@@ -858,7 +858,7 @@ class InfoExtractor(object):
 class YoutubeIE(InfoExtractor):
        """Information extractor for youtube.com."""
 
-       _VALID_URL = r'^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)(?:(?:(?:v|embed)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
+       _VALID_URL = r'^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)(?:(?:(?:v|embed|e)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
        _LANG_URL = r'http://www.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
        _LOGIN_URL = 'https://www.youtube.com/signup?next=/&gl=US&hl=en'
        _AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
@@ -1056,7 +1056,7 @@ class YoutubeIE(InfoExtractor):
 
                # upload date
                upload_date = u'NA'
-               mobj = re.search(r'id="eow-date".*?>(.*?)</span>', video_webpage, re.DOTALL)
+               mobj = re.search(r'id="eow-date.*?>(.*?)</span>', video_webpage, re.DOTALL)
                if mobj is not None:
                        upload_date = ' '.join(re.sub(r'[/,-]', r' ', mobj.group(1)).split())
                        format_expressions = ['%d %B %Y', '%B %d %Y', '%b %d %Y']
@@ -1079,7 +1079,7 @@ class YoutubeIE(InfoExtractor):
                # Decide which formats to download
                req_format = self._downloader.params.get('format', None)
 
-               if 'fmt_url_map' in video_info:
+               if 'fmt_url_map' in video_info and len(video_info['fmt_url_map']) >= 1 and ',' in video_info['fmt_url_map'][0]:
                        url_map = dict(tuple(pair.split('|')) for pair in video_info['fmt_url_map'][0].split(','))
                        format_limit = self._downloader.params.get('format_limit', None)
                        if format_limit is not None and format_limit in self._available_formats:
@@ -1720,6 +1720,123 @@ class YahooIE(InfoExtractor):
                        self._downloader.trouble(u'\nERROR: unable to download video')
 
 
+class VimeoIE(InfoExtractor):
+       """Information extractor for vimeo.com."""
+
+       # _VALID_URL matches Vimeo URLs
+       _VALID_URL = r'(?:http://)?(?:(?:www|player).)?vimeo\.com/(?:video/)?([0-9]+)'
+
+       def __init__(self, downloader=None):
+               InfoExtractor.__init__(self, downloader)
+
+       @staticmethod
+       def suitable(url):
+               return (re.match(VimeoIE._VALID_URL, url) is not None)
+
+       def report_download_webpage(self, video_id):
+               """Report webpage download."""
+               self._downloader.to_screen(u'[video.vimeo] %s: Downloading webpage' % video_id)
+
+       def report_extraction(self, video_id):
+               """Report information extraction."""
+               self._downloader.to_screen(u'[video.vimeo] %s: Extracting information' % video_id)
+
+       def _real_initialize(self):
+               return
+
+       def _real_extract(self, url, new_video=True):
+               # Extract ID from URL
+               mobj = re.match(self._VALID_URL, url)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
+                       return
+
+               # At this point we have a new video
+               self._downloader.increment_downloads()
+               video_id = mobj.group(1)
+               video_extension = 'flv' # FIXME
+
+               # Retrieve video webpage to extract further information
+               request = urllib2.Request("http://vimeo.com/moogaloop/load/clip:%s" % video_id, None, std_headers)
+               try:
+                       self.report_download_webpage(video_id)
+                       webpage = urllib2.urlopen(request).read()
+               except (urllib2.URLError, httplib.HTTPException, socket.error), err:
+                       self._downloader.trouble(u'ERROR: Unable to retrieve video webpage: %s' % str(err))
+                       return
+
+               # Now we begin extracting as much information as we can from what we
+               # retrieved. First we extract the information common to all extractors,
+               # and latter we extract those that are Vimeo specific.
+               self.report_extraction(video_id)
+
+               # Extract title
+               mobj = re.search(r'<caption>(.*?)</caption>', webpage)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: unable to extract video title')
+                       return
+               video_title = mobj.group(1).decode('utf-8')
+               simple_title = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', video_title)
+
+               # Extract uploader
+               mobj = re.search(r'<uploader_url>http://vimeo.com/(.*?)</uploader_url>', webpage)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: unable to extract video uploader')
+                       return
+               video_uploader = mobj.group(1).decode('utf-8')
+
+               # Extract video thumbnail
+               mobj = re.search(r'<thumbnail>(.*?)</thumbnail>', webpage)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: unable to extract video thumbnail')
+                       return
+               video_thumbnail = mobj.group(1).decode('utf-8')
+
+               # # Extract video description
+               # mobj = re.search(r'<meta property="og:description" content="(.*)" />', webpage)
+               # if mobj is None:
+               #       self._downloader.trouble(u'ERROR: unable to extract video description')
+               #       return
+               # video_description = mobj.group(1).decode('utf-8')
+               # if not video_description: video_description = 'No description available.'
+               video_description = 'Foo.'
+
+               # Vimeo specific: extract request signature
+               mobj = re.search(r'<request_signature>(.*?)</request_signature>', webpage)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: unable to extract request signature')
+                       return
+               sig = mobj.group(1).decode('utf-8')
+
+               # Vimeo specific: Extract request signature expiration
+               mobj = re.search(r'<request_signature_expires>(.*?)</request_signature_expires>', webpage)
+               if mobj is None:
+                       self._downloader.trouble(u'ERROR: unable to extract request signature expiration')
+                       return
+               sig_exp = mobj.group(1).decode('utf-8')
+
+               video_url = "http://vimeo.com/moogaloop/play/clip:%s/%s/%s" % (video_id, sig, sig_exp)
+
+               try:
+                       # Process video information
+                       self._downloader.process_info({
+                               'id':           video_id.decode('utf-8'),
+                               'url':          video_url,
+                               'uploader':     video_uploader,
+                               'upload_date':  u'NA',
+                               'title':        video_title,
+                               'stitle':       simple_title,
+                               'ext':          video_extension.decode('utf-8'),
+                               'thumbnail':    video_thumbnail.decode('utf-8'),
+                               'description':  video_description,
+                               'thumbnail':    video_thumbnail,
+                               'description':  video_description,
+                               'player_url':   None,
+                       })
+               except UnavailableVideoError:
+                       self._downloader.trouble(u'ERROR: unable to download video')
+
+
 class GenericIE(InfoExtractor):
        """Generic last-resort information extractor."""
 
@@ -2619,10 +2736,13 @@ class FFmpegExtractAudioPP(PostProcessor):
 
        @staticmethod
        def get_audio_codec(path):
-               handle = subprocess.Popen(['ffprobe', '-show_streams', path],
-                               stderr=file(os.path.devnull, 'w'), stdout=subprocess.PIPE)
-               output = handle.communicate()[0]
-               if handle.wait() != 0:
+               try:
+                       cmd = ['ffprobe', '-show_streams', '--', path]
+                       handle = subprocess.Popen(cmd, stderr=file(os.path.devnull, 'w'), stdout=subprocess.PIPE)
+                       output = handle.communicate()[0]
+                       if handle.wait() != 0:
+                               return None
+               except (IOError, OSError):
                        return None
                audio_codec = None
                for line in output.split('\n'):
@@ -2635,8 +2755,8 @@ class FFmpegExtractAudioPP(PostProcessor):
        @staticmethod
        def run_ffmpeg(path, out_path, codec, more_opts):
                try:
-                       ret = subprocess.call(['ffmpeg', '-y', '-i', path, '-vn', '-acodec', codec] + more_opts + [out_path],
-                                       stdout=file(os.path.devnull, 'w'), stderr=subprocess.STDOUT)
+                       cmd = ['ffmpeg', '-y', '-i', path, '-vn', '-acodec', codec] + more_opts + ['--', out_path]
+                       ret = subprocess.call(cmd, stdout=file(os.path.devnull, 'w'), stderr=subprocess.STDOUT)
                        return (ret == 0)
                except (IOError, OSError):
                        return False
@@ -2646,7 +2766,7 @@ class FFmpegExtractAudioPP(PostProcessor):
 
                filecodec = self.get_audio_codec(path)
                if filecodec is None:
-                       self._downloader.to_stderr(u'WARNING: no audio codec found in file')
+                       self._downloader.to_stderr(u'WARNING: unable to obtain file audio codec with ffprobe')
                        return None
 
                more_opts = []
@@ -2720,7 +2840,7 @@ if __name__ == '__main__':
                # Parse command line
                parser = optparse.OptionParser(
                        usage='Usage: %prog [options] url...',
-                       version='2011.02.25',
+                       version='2011.03.29',
                        conflict_handler='resolve',
                )
 
@@ -2895,6 +3015,7 @@ if __name__ == '__main__':
                                parser.error(u'invalid audio format specified')
 
                # Information extractors
+               vimeo_ie = VimeoIE()
                youtube_ie = YoutubeIE()
                metacafe_ie = MetacafeIE(youtube_ie)
                dailymotion_ie = DailymotionIE()
@@ -2947,6 +3068,7 @@ if __name__ == '__main__':
                        'nopart': opts.nopart,
                        'updatetime': opts.updatetime,
                        })
+               fd.add_info_extractor(vimeo_ie)
                fd.add_info_extractor(youtube_search_ie)
                fd.add_info_extractor(youtube_pl_ie)
                fd.add_info_extractor(youtube_user_ie)