A feature-rich command-line audio/video downloader
Save and restore console title (Fixes #1782)
Philipp Hagemeisterbdde425cbe01
diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.pyindex 6e5ae44d35..4e28f91203 100644--- a/youtube_dl/YoutubeDL.py+++ b/youtube_dl/YoutubeDL.py@@ -213,6 +213,25 @@ class YoutubeDL(object):elif 'TERM' in os.environ:self.to_screen('\033]0;%s\007' % message, skip_eol=True)+ def save_console_title(self):+ if not self.params.get('consoletitle', False):+ return+ if 'TERM' in os.environ:+ self.to_screen('\033[22t')++ def restore_console_title(self):+ if not self.params.get('consoletitle', False):+ return+ if 'TERM' in os.environ:+ self.to_screen('\033[23t')++ def __enter__(self):+ self.save_console_title()+ return self++ def __exit__(self, *args):+ self.restore_console_title()+def fixed_template(self):"""Checks if the output template is fixed."""return (re.search(u'(?u)%\\(.+?\\)s', self.params['outtmpl']) is None)diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.pyindex 4dee487ab2..af4c9c5c46 100644--- a/youtube_dl/__init__.py+++ b/youtube_dl/__init__.py@@ -603,8 +603,7 @@ def _real_main(argv=None):u' file! Use "%%(ext)s" instead of %r' %determine_ext(outtmpl, u''))- # YoutubeDL- ydl = YoutubeDL({+ ydl_opts = {'usenetrc': opts.usenetrc,'username': opts.username,'password': opts.password,@@ -667,61 +666,63 @@ def _real_main(argv=None):'youtube_print_sig_code': opts.youtube_print_sig_code,'age_limit': opts.age_limit,'download_archive': opts.download_archive,- })+ }- if opts.verbose:- write_string(u'[debug] youtube-dl version ' + __version__ + u'\n')- try:- sp = subprocess.Popen(- ['git', 'rev-parse', '--short', 'HEAD'],- stdout=subprocess.PIPE, stderr=subprocess.PIPE,- cwd=os.path.dirname(os.path.abspath(__file__)))- out, err = sp.communicate()- out = out.decode().strip()- if re.match('[0-9a-f]+', out):- write_string(u'[debug] Git HEAD: ' + out + u'\n')- except:+ with YoutubeDL(ydl_opts) as ydl:+ if opts.verbose:+ write_string(u'[debug] youtube-dl version ' + __version__ + u'\n')try:- sys.exc_clear()+ sp = subprocess.Popen(+ ['git', 'rev-parse', '--short', 'HEAD'],+ stdout=subprocess.PIPE, stderr=subprocess.PIPE,+ cwd=os.path.dirname(os.path.abspath(__file__)))+ out, err = sp.communicate()+ out = out.decode().strip()+ if re.match('[0-9a-f]+', out):+ write_string(u'[debug] Git HEAD: ' + out + u'\n')except:- pass- write_string(u'[debug] Python version %s - %s' %(platform.python_version(), platform_name()) + u'\n')-- proxy_map = {}- for handler in opener.handlers:- if hasattr(handler, 'proxies'):- proxy_map.update(handler.proxies)- write_string(u'[debug] Proxy map: ' + compat_str(proxy_map) + u'\n')-- ydl.add_default_info_extractors()-- # PostProcessors- # Add the metadata pp first, the other pps will copy it- if opts.addmetadata:- ydl.add_post_processor(FFmpegMetadataPP())- if opts.extractaudio:- ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))- if opts.recodevideo:- ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))- if opts.embedsubtitles:- ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))-- # Update version- if opts.update_self:- update_self(ydl.to_screen, opts.verbose)-- # Maybe do nothing- if len(all_urls) < 1:- if not opts.update_self:- parser.error(u'you must provide at least one URL')- else:- sys.exit()+ try:+ sys.exc_clear()+ except:+ pass+ write_string(u'[debug] Python version %s - %s' %+ (platform.python_version(), platform_name()) + u'\n')++ proxy_map = {}+ for handler in opener.handlers:+ if hasattr(handler, 'proxies'):+ proxy_map.update(handler.proxies)+ write_string(u'[debug] Proxy map: ' + compat_str(proxy_map) + u'\n')++ ydl.add_default_info_extractors()++ # PostProcessors+ # Add the metadata pp first, the other pps will copy it+ if opts.addmetadata:+ ydl.add_post_processor(FFmpegMetadataPP())+ if opts.extractaudio:+ ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))+ if opts.recodevideo:+ ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))+ if opts.embedsubtitles:+ ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))++ # Update version+ if opts.update_self:+ update_self(ydl.to_screen, opts.verbose)++ # Maybe do nothing+ if len(all_urls) < 1:+ if not opts.update_self:+ parser.error(u'you must provide at least one URL')+ else:+ sys.exit()- try:- retcode = ydl.download(all_urls)- except MaxDownloadsReached:- ydl.to_screen(u'--max-download limit reached, aborting.')- retcode = 101+ try:+ retcode = ydl.download(all_urls)+ except MaxDownloadsReached:+ ydl.to_screen(u'--max-download limit reached, aborting.')+ retcode = 101# Dump cookie jar if requestedif opts.cookiefile is not None: