From 87cbd21323195cacb0febc7898a7fdc74ed97f9b Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 19:05:35 +0100 Subject: [PATCH 1/7] Fix date parsing for YouTube (patch by Drake Wyrm) --- youtube-dl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube-dl b/youtube-dl index 0dd872e50..2e04c05b0 100755 --- a/youtube-dl +++ b/youtube-dl @@ -1059,7 +1059,7 @@ class YoutubeIE(InfoExtractor): mobj = re.search(r'id="eow-date".*?>(.*?)', 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'] + format_expressions = ['%d %B %Y', '%B %d %Y', '%b %d %Y'] for expression in format_expressions: try: upload_date = datetime.datetime.strptime(upload_date, expression).strftime('%Y%m%d') From 3072fab115b3c89322edc906a8f88f997e46dedd Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 19:06:58 +0100 Subject: [PATCH 2/7] Add an audio extracting PostProcessor using ffmpeg (closes #2) --- youtube-dl | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/youtube-dl b/youtube-dl index 2e04c05b0..b0981da0d 100755 --- a/youtube-dl +++ b/youtube-dl @@ -2609,6 +2609,85 @@ class PostProcessor(object): """ return information # by default, do nothing +class FFmpegExtractAudioPP(PostProcessor): + + def __init__(self, downloader=None, preferredcodec=None): + PostProcessor.__init__(self, downloader) + if preferredcodec is None: + preferredcodec = 'best' + self._preferredcodec = preferredcodec + + @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: + return None + audio_codec = None + for line in output.split('\n'): + if line.startswith('codec_name='): + audio_codec = line.split('=')[1].strip() + elif line.strip() == 'codec_type=audio' and audio_codec is not None: + return audio_codec + return None + + @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) + return (ret == 0) + except (IOError, OSError): + return False + + def run(self, information): + path = information['filepath'] + + filecodec = self.get_audio_codec(path) + if filecodec is None: + self._downloader.to_stderr(u'WARNING: no audio codec found in file') + return None + + more_opts = [] + if self._preferredcodec == 'best' or self._preferredcodec == filecodec: + if filecodec == 'aac' or filecodec == 'mp3': + # Lossless if possible + acodec = 'copy' + extension = filecodec + if filecodec == 'aac': + more_opts = ['-f', 'adts'] + else: + # MP3 otherwise. + acodec = 'libmp3lame' + extension = 'mp3' + more_opts = ['-ab', '128k'] + else: + # We convert the audio (lossy) + acodec = {'mp3': 'libmp3lame', 'aac': 'aac'}[self._preferredcodec] + extension = self._preferredcodec + more_opts = ['-ab', '128k'] + if self._preferredcodec == 'aac': + more_opts += ['-f', 'adts'] + + (prefix, ext) = os.path.splitext(path) + new_path = prefix + '.' + extension + self._downloader.to_screen(u'[ffmpeg] Destination: %s' % new_path) + status = self.run_ffmpeg(path, new_path, acodec, more_opts) + + if not status: + self._downloader.to_stderr(u'WARNING: error running ffmpeg' % ret) + return None + + 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 + ### MAIN PROGRAM ### if __name__ == '__main__': try: @@ -2733,6 +2812,13 @@ if __name__ == '__main__': help='do not use the Last-modified header to set the file modification time', default=True) parser.add_option_group(filesystem) + postproc = optparse.OptionGroup(parser, 'Post-processing Options') + postproc.add_option('--extract-audio', action='store_true', dest='extractaudio', default=False, + 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') + parser.add_option_group(postproc) + (opts, args) = parser.parse_args() # Open appropriate CookieJar @@ -2804,6 +2890,9 @@ if __name__ == '__main__': raise ValueError except (TypeError, ValueError), err: parser.error(u'invalid playlist end number specified') + if opts.extractaudio: + if opts.audioformat not in ['best', 'aac', 'mp3']: + parser.error(u'invalid audio format specified') # Information extractors youtube_ie = YoutubeIE() @@ -2876,6 +2965,10 @@ if __name__ == '__main__': # fallback if none of the others work fd.add_info_extractor(generic_ie) + # PostProcessors + if opts.extractaudio: + fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat)) + # Update version if opts.update_self: update_self(fd, sys.argv[0]) From afd233c05c983dac0ea9cab4544e1cec3f31f990 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 20:11:53 +0100 Subject: [PATCH 3/7] Update User-Agent string --- youtube-dl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube-dl b/youtube-dl index b0981da0d..c45113efe 100755 --- a/youtube-dl +++ b/youtube-dl @@ -38,7 +38,7 @@ except ImportError: from cgi import parse_qs std_headers = { - 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:2.0b10) Gecko/20100101 Firefox/4.0b10', + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:2.0b11) Gecko/20100101 Firefox/4.0b11', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', From c0768454547d86d16f10b9f2ec9e3bb13b210f1d Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 20:12:32 +0100 Subject: [PATCH 4/7] Bump version number --- LATEST_VERSION | 2 +- youtube-dl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LATEST_VERSION b/LATEST_VERSION index 4ab209346..b8b45af99 100644 --- a/LATEST_VERSION +++ b/LATEST_VERSION @@ -1 +1 @@ -2011.01.30 +2011.02.25 diff --git a/youtube-dl b/youtube-dl index c45113efe..f8271a7fe 100755 --- a/youtube-dl +++ b/youtube-dl @@ -2720,7 +2720,7 @@ if __name__ == '__main__': # Parse command line parser = optparse.OptionParser( usage='Usage: %prog [options] url...', - version='2011.01.30', + version='2011.02.25', conflict_handler='resolve', ) From 1bd9258272dd4884b5c159ae77e20ae75176e8d1 Mon Sep 17 00:00:00 2001 From: Idan Kamara Date: Fri, 25 Feb 2011 22:30:22 +0200 Subject: [PATCH 5/7] Fix stderr print when ffmpeg fails --- youtube-dl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube-dl b/youtube-dl index f8271a7fe..9e9be6778 100755 --- a/youtube-dl +++ b/youtube-dl @@ -2676,7 +2676,7 @@ class FFmpegExtractAudioPP(PostProcessor): status = self.run_ffmpeg(path, new_path, acodec, more_opts) if not status: - self._downloader.to_stderr(u'WARNING: error running ffmpeg' % ret) + self._downloader.to_stderr(u'WARNING: error running ffmpeg') return None try: From da273188f33ace3b48290a8cf35d36033a6fa960 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 21:53:26 +0100 Subject: [PATCH 6/7] Catch possible exceptions when running ffprobe --- youtube-dl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/youtube-dl b/youtube-dl index 9e9be6778..617ac1339 100755 --- a/youtube-dl +++ b/youtube-dl @@ -2619,10 +2619,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: + handle = subprocess.Popen(['ffprobe', '-show_streams', path], + 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'): @@ -2646,7 +2649,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 = [] From 820eedcb504acb9666ed589c3ed8cb1a641d0fd1 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Date: Fri, 25 Feb 2011 21:54:16 +0100 Subject: [PATCH 7/7] Bump version number --- LATEST_VERSION | 2 +- youtube-dl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LATEST_VERSION b/LATEST_VERSION index b8b45af99..4851877b4 100644 --- a/LATEST_VERSION +++ b/LATEST_VERSION @@ -1 +1 @@ -2011.02.25 +2011.02.25b diff --git a/youtube-dl b/youtube-dl index 617ac1339..072a91970 100755 --- a/youtube-dl +++ b/youtube-dl @@ -2723,7 +2723,7 @@ if __name__ == '__main__': # Parse command line parser = optparse.OptionParser( usage='Usage: %prog [options] url...', - version='2011.02.25', + version='2011.02.25b', conflict_handler='resolve', )